"Fossies" - the Fresh Open Source Software Archive 
Member "flutter-3.7.1/examples/api/lib/widgets/actions/actions.0.dart" (1 Feb 2023, 4729 Bytes) of package /linux/misc/flutter-3.7.1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Dart source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
See also the last
Fossies "Diffs" side-by-side code changes report for "actions.0.dart":
3.3.10_vs_3.7.0.
1 // Copyright 2014 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 /// Flutter code sample for [Actions].
6
7 import 'package:flutter/material.dart';
8
9 void main() => runApp(const MyApp());
10
11 class MyApp extends StatelessWidget {
12 const MyApp({super.key});
13
14 static const String _title = 'Flutter Code Sample';
15
16 @override
17 Widget build(BuildContext context) {
18 return MaterialApp(
19 title: _title,
20 home: Scaffold(
21 appBar: AppBar(title: const Text(_title)),
22 body: const Center(
23 child: MyStatefulWidget(),
24 ),
25 ),
26 );
27 }
28 }
29
30 // A simple model class that notifies listeners when it changes.
31 class Model {
32 ValueNotifier<bool> isDirty = ValueNotifier<bool>(false);
33 ValueNotifier<int> data = ValueNotifier<int>(0);
34
35 int save() {
36 if (isDirty.value) {
37 debugPrint('Saved Data: ${data.value}');
38 isDirty.value = false;
39 }
40 return data.value;
41 }
42
43 void setValue(int newValue) {
44 isDirty.value = data.value != newValue;
45 data.value = newValue;
46 }
47 }
48
49 class ModifyIntent extends Intent {
50 const ModifyIntent(this.value);
51
52 final int value;
53 }
54
55 // An Action that modifies the model by setting it to the value that it gets
56 // from the Intent passed to it when invoked.
57 class ModifyAction extends Action<ModifyIntent> {
58 ModifyAction(this.model);
59
60 final Model model;
61
62 @override
63 void invoke(covariant ModifyIntent intent) {
64 model.setValue(intent.value);
65 }
66 }
67
68 // An intent for saving data.
69 class SaveIntent extends Intent {
70 const SaveIntent();
71 }
72
73 // An Action that saves the data in the model it is created with.
74 class SaveAction extends Action<SaveIntent> {
75 SaveAction(this.model);
76
77 final Model model;
78
79 @override
80 int invoke(covariant SaveIntent intent) => model.save();
81 }
82
83 class SaveButton extends StatefulWidget {
84 const SaveButton(this.valueNotifier, {super.key});
85
86 final ValueNotifier<bool> valueNotifier;
87
88 @override
89 State<SaveButton> createState() => _SaveButtonState();
90 }
91
92 class _SaveButtonState extends State<SaveButton> {
93 int savedValue = 0;
94
95 @override
96 Widget build(BuildContext context) {
97 return AnimatedBuilder(
98 animation: widget.valueNotifier,
99 builder: (BuildContext context, Widget? child) {
100 return TextButton.icon(
101 icon: const Icon(Icons.save),
102 label: Text('$savedValue'),
103 style: ButtonStyle(
104 foregroundColor: MaterialStatePropertyAll<Color>(
105 widget.valueNotifier.value ? Colors.red : Colors.green,
106 ),
107 ),
108 onPressed: () {
109 setState(() {
110 savedValue = Actions.invoke(context, const SaveIntent())! as int;
111 });
112 },
113 );
114 },
115 );
116 }
117 }
118
119 class MyStatefulWidget extends StatefulWidget {
120 const MyStatefulWidget({super.key});
121
122 @override
123 State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
124 }
125
126 class _MyStatefulWidgetState extends State<MyStatefulWidget> {
127 Model model = Model();
128 int count = 0;
129
130 @override
131 Widget build(BuildContext context) {
132 return Actions(
133 actions: <Type, Action<Intent>>{
134 ModifyIntent: ModifyAction(model),
135 SaveIntent: SaveAction(model),
136 },
137 child: Builder(
138 builder: (BuildContext context) {
139 return Row(
140 mainAxisAlignment: MainAxisAlignment.spaceAround,
141 children: <Widget>[
142 const Spacer(),
143 Column(
144 mainAxisAlignment: MainAxisAlignment.center,
145 children: <Widget>[
146 IconButton(
147 icon: const Icon(Icons.exposure_plus_1),
148 onPressed: () {
149 Actions.invoke(context, ModifyIntent(++count));
150 },
151 ),
152 AnimatedBuilder(
153 animation: model.data,
154 builder: (BuildContext context, Widget? child) {
155 return Padding(
156 padding: const EdgeInsets.all(8.0),
157 child: Text('${model.data.value}',
158 style: Theme.of(context).textTheme.headlineMedium),
159 );
160 }),
161 IconButton(
162 icon: const Icon(Icons.exposure_minus_1),
163 onPressed: () {
164 Actions.invoke(context, ModifyIntent(--count));
165 },
166 ),
167 ],
168 ),
169 SaveButton(model.isDirty),
170 const Spacer(),
171 ],
172 );
173 },
174 ),
175 );
176 }
177 }