-
Notifications
You must be signed in to change notification settings - Fork 2
/
home_page.dart
119 lines (97 loc) · 3.72 KB
/
home_page.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
// Copyright 2022 Andrious Solutions Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:example/src/controller.dart';
import 'package:example/src/view.dart';
/// The Home page
class HomePage extends StatefulWidget {
/// With a constant constructor, instantiated once.
const HomePage({Key? key, this.title = 'Flutter Demo'}) : super(key: key);
/// Fields in a StatefulWidget should always be "final".
final String title;
@override
State<StatefulWidget> createState() => _HomePageState();
}
class _HomePageState extends StateX<HomePage> {
/// Let the 'business logic' run in a Controller
_HomePageState() : super(Controller()) {
/// Acquire a reference to the passed Controller.
con = controller as Controller;
}
late Controller con;
@override
void initState() {
/// Look inside the parent function and see it calls
/// all it's Controllers if any.
super.initState();
var con = controller;
con = controllerByType<Controller>();
con = controllerById(con?.identifier);
/// Retrieve the 'app level' State object
appState = rootState!;
/// You're able to retrieve the Controller(s) from other State objects.
con = appState.controller;
con = appState.controllerByType<Controller>();
con = appState.controllerById(con?.identifier);
/// You're able to retrieve the 'past' Controller(s)
con = controllerByType<AppController>();
con = appState.controllerByType<AppController>();
}
late AppStateX appState;
/// Build the 'child' Widget passed to the InheritedWidget.
@override
Widget buildWidget(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
/// SetState class is like a setState() function but called only when the App's InheritedWidget is rebuilt.
child: SetState(
builder: (context, dataObject) => Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
/// Display the App's data object if it has something to display
/// Note, the Stat Controllers has access to the same 'dataObject'
if (dataObject != null &&
dataObject == con.dataObject &&
con.dataObject is String)
Padding(
padding: const EdgeInsets.all(30),
child: Text(
dataObject as String,
key: const Key('greetings'),
style: TextStyle(
color: Colors.red,
fontSize:
Theme.of(context).textTheme.headline4!.fontSize,
),
),
),
Text(
'You have pushed the button this many times:',
style: Theme.of(context).textTheme.bodyText2,
),
Text(
'${con.count}',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
),
floatingActionButton: FloatingActionButton(
key: const Key('+'),
/// Refresh only the Text widget containing the counter.
onPressed: () => con.incrementCounter(),
/// You can have the Controller called the interface (the View).
// onPressed: con.onPressed,
child: const Icon(Icons.add),
),
);
/// Supply an error handler for Unit Testing.
@override
void onError(FlutterErrorDetails details) {
/// Error is now handled.
super.onError(details);
}
}