/
main.dart
47 lines (42 loc) · 1.17 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
import 'package:flutter/material.dart';
import 'package:flutter_state_notifier/flutter_state_notifier.dart';
import 'package:provider/provider.dart';
import 'package:state_notifier/state_notifier.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: StateNotifierProvider<CounterNotifier, CounterState>(
create: (_) => CounterNotifier(),
child: HomePage(),
),
);
}
}
class CounterState {
CounterState(this.count);
int count = 0;
}
class CounterNotifier extends StateNotifier<CounterState> {
CounterNotifier() : super(CounterState(0));
void increment() {
state = CounterState(++state.count);
}
}
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Minimum State Notifier Example'),
),
body: Text(
context.select<CounterState, int>((state) => state.count).toString(),
),
floatingActionButton: FloatingActionButton(
onPressed: () => context.read<CounterNotifier>().increment(),
),
);
}
}