-
Notifications
You must be signed in to change notification settings - Fork 0
/
third_screen.dart
145 lines (126 loc) · 4.08 KB
/
third_screen.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import 'package:flutter/material.dart';
import 'package:state_set/state_set.dart';
import 'package:example/src/home/1/home_screen.dart';
import 'package:example/src/home/2/second_screen.dart';
import 'package:example/src/app/menu/app_menu.dart';
class ThirdScreen extends StatefulWidget {
/// Singleton pattern. No need for multiple instances.
factory ThirdScreen({Key key}) => _this ??= ThirdScreen._(key: key);
const ThirdScreen._({Key key}) : super(key: key);
static ThirdScreen _this;
/// Access to the State object's functionality.
void onPressed() {
/// Always retrieve the 'latest' State object
/// If a new Key was supplied to the Widget, it's recreate the State object.
// Don't use ??= to save on performance. The State object may be recreated.
final _ThirdScreenState state = StateSet.to<_ThirdScreenState>();
state?.onPressed();
}
@override
_ThirdScreenState createState() => _ThirdScreenState();
}
class _ThirdScreenState extends State<ThirdScreen> with StateSet {
/// A mutable data field.
int _counter = 0;
void onPressed() {
setState(() {});
_counter++;
}
/// Raised buttons used in the popupmenu and the screen itself.
final List<RaisedButton> buttons = [];
@override
void initState() {
super.initState();
buttons.addAll([
homeScreenCounter,
secondScreenCounter,
resetHome,
homeScreen,
secondScreen,
]);
}
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Third Screen'),
actions: [
AppMenu(buttons: buttons),
],
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text('You have pushed the button this many times:'),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: onPressed,
child: const Icon(Icons.add),
),
persistentFooterButtons: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
homeScreenCounter,
secondScreenCounter,
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
resetHome,
homeScreen,
secondScreen,
],
),
],
);
RaisedButton get homeScreenCounter => RaisedButton(
onPressed: () {
final HomeScreen home = HomeScreen();
home?.onPressed();
/// Retrieves the State object of the specified StatefulWidget.
final State state = StateSet.of<HomeScreen>();
state?.setState(() {});
},
child: const Text('Home Counter'),
);
RaisedButton get secondScreenCounter => RaisedButton(
onPressed: () {
final second = SecondScreen();
second?.onPressed();
/// Of course, you could call setState() function in the
/// onPressed() function above. This is merely to demonstrate
/// you have access to an 'external' State object.
final state = StateSet.of<SecondScreen>();
state?.setState(() {});
},
child: const Text('Second Counter'),
);
RaisedButton get resetHome => RaisedButton(
onPressed: () {
/// Always returns the 'first' State object of the App.
final State appState = StateSet.root;
appState?.setState(() {});
},
child: const Text('Reset Home'),
);
RaisedButton get homeScreen => RaisedButton(
onPressed: () {
Navigator.popUntil(context, ModalRoute.withName('/'));
},
child: const Text('Home Screen'),
);
RaisedButton get secondScreen => RaisedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Second Screen'),
);
}