Add tests for actions.0.dart API example. (#148678)
This PR contributes to https://github.com/flutter/flutter/issues/130459 ### Description - Updates `examples/api/lib/widgets/actions/actions.0.dart` to meet the latest API examples structure - Adds tests for `examples/api/lib/widgets/actions/actions.0.dart`
This commit is contained in:
committed by
GitHub
parent
ea7cf54b42
commit
112e8b5509
@@ -26,8 +26,8 @@ class ActionsExampleApp extends StatelessWidget {
|
||||
|
||||
// A simple model class that notifies listeners when it changes.
|
||||
class Model {
|
||||
ValueNotifier<bool> isDirty = ValueNotifier<bool>(false);
|
||||
ValueNotifier<int> data = ValueNotifier<int>(0);
|
||||
final ValueNotifier<bool> isDirty = ValueNotifier<bool>(false);
|
||||
final ValueNotifier<int> data = ValueNotifier<int>(0);
|
||||
|
||||
int save() {
|
||||
if (isDirty.value) {
|
||||
@@ -41,6 +41,11 @@ class Model {
|
||||
isDirty.value = data.value != newValue;
|
||||
data.value = newValue;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
isDirty.dispose();
|
||||
data.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
class ModifyIntent extends Intent {
|
||||
@@ -87,7 +92,7 @@ class SaveButton extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _SaveButtonState extends State<SaveButton> {
|
||||
int savedValue = 0;
|
||||
int _savedValue = 0;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -96,7 +101,7 @@ class _SaveButtonState extends State<SaveButton> {
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return TextButton.icon(
|
||||
icon: const Icon(Icons.save),
|
||||
label: Text('$savedValue'),
|
||||
label: Text('$_savedValue'),
|
||||
style: ButtonStyle(
|
||||
foregroundColor: MaterialStatePropertyAll<Color>(
|
||||
widget.valueNotifier.value ? Colors.red : Colors.green,
|
||||
@@ -104,7 +109,7 @@ class _SaveButtonState extends State<SaveButton> {
|
||||
),
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
savedValue = Actions.invoke(context, const SaveIntent())! as int;
|
||||
_savedValue = Actions.invoke(context, const SaveIntent())! as int;
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -121,15 +126,21 @@ class ActionsExample extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ActionsExampleState extends State<ActionsExample> {
|
||||
Model model = Model();
|
||||
int count = 0;
|
||||
final Model _model = Model();
|
||||
int _count = 0;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_model.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Actions(
|
||||
actions: <Type, Action<Intent>>{
|
||||
ModifyIntent: ModifyAction(model),
|
||||
SaveIntent: SaveAction(model),
|
||||
ModifyIntent: ModifyAction(_model),
|
||||
SaveIntent: SaveAction(_model),
|
||||
},
|
||||
child: Builder(
|
||||
builder: (BuildContext context) {
|
||||
@@ -143,26 +154,30 @@ class _ActionsExampleState extends State<ActionsExample> {
|
||||
IconButton(
|
||||
icon: const Icon(Icons.exposure_plus_1),
|
||||
onPressed: () {
|
||||
Actions.invoke(context, ModifyIntent(++count));
|
||||
Actions.invoke(context, ModifyIntent(++_count));
|
||||
},
|
||||
),
|
||||
ListenableBuilder(
|
||||
listenable: model.data,
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text('${model.data.value}', style: Theme.of(context).textTheme.headlineMedium),
|
||||
);
|
||||
}),
|
||||
listenable: _model.data,
|
||||
builder: (BuildContext context, Widget? child) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(8.0),
|
||||
child: Text(
|
||||
'Value: ${_model.data.value}',
|
||||
style: Theme.of(context).textTheme.headlineMedium,
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
IconButton(
|
||||
icon: const Icon(Icons.exposure_minus_1),
|
||||
onPressed: () {
|
||||
Actions.invoke(context, ModifyIntent(--count));
|
||||
Actions.invoke(context, ModifyIntent(--_count));
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
SaveButton(model.isDirty),
|
||||
SaveButton(_model.isDirty),
|
||||
const Spacer(),
|
||||
],
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user