-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInheritedWidget.dart
More file actions
96 lines (85 loc) · 2.28 KB
/
InheritedWidget.dart
File metadata and controls
96 lines (85 loc) · 2.28 KB
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
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Timer',
theme: new ThemeData(
primaryColor: Colors.grey.shade800,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomePageState createState() {
return new MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> {
int _seconds = 1;
@override
Widget build(BuildContext context) {
return new MyInheritedWidget(
secondsToDisplay: _seconds,
child: Scaffold(
appBar: AppBar(
title: Text("title"),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
MyTextWidget(), //just update this widget
Divider(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
IconButton(
icon: Icon(Icons.add_circle),
onPressed: _addPressed,
iconSize: 150.0,
),
IconButton(
icon: Icon(Icons.remove_circle),
onPressed: () => print("to be implemented"),
iconSize: 150.0,
),
],
)
],
),
),
);
}
void _addPressed() {
setState(() {
_seconds++;
});
}
}
class MyTextWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
final MyInheritedWidget inheritedWidget = MyInheritedWidget.of(context);
return Text(
inheritedWidget.secondsToDisplay.toString(),
textScaleFactor: 5.0,
);
}
}
class MyInheritedWidget extends InheritedWidget {
final int secondsToDisplay;
MyInheritedWidget({
Key key,
@required this.secondsToDisplay,
@required Widget child,
}) : super(key: key, child: child);
static MyInheritedWidget of(BuildContext context) {
return context.dependOnInheritedWidgetOfExactType<MyInheritedWidget>();
}
@override
bool updateShouldNotify(MyInheritedWidget oldWidget) =>
secondsToDisplay != oldWidget.secondsToDisplay;
}