/
main.dart
68 lines (63 loc) · 1.74 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import 'package:apn_state/apn_state.dart';
import 'package:flutter/material.dart';
void main() {
// Run the app
runApp(MyApp());
}
/// Make a class that holds your state variables
class CounterState extends BaseState<String> {
int counter = 0;
@override
String? convertError(e) {
print(e);
return 'Something went wrong';
}
}
/// Update your state by using events
class IncrementCounterEvent extends BaseStateEvent<CounterState> {
@override
Future<void> handle() async {
state.counter++;
state.notifyListeners();
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
builder: (context, child) {
return BaseConsumerView<CounterState>(
create: () => CounterState(),
builder: (context, state, child) => Scaffold(
appBar: AppBar(
title: Text('State example'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'${state.counter}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () => state.dispatch(IncrementCounterEvent()),
child: Icon(Icons.add),
),
),
);
},
);
}
}