Guest User

Untitled

a guest
Jan 19th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. import 'dart:math';
  2.  
  3. import 'package:flutter/material.dart';
  4. import 'package:meta/meta.dart';
  5.  
  6. void main() {
  7. runApp(new AppContainer());
  8. }
  9.  
  10. class AppContextData {
  11. final String appName;
  12. final int value;
  13. final ThemeData themeData;
  14.  
  15. const AppContextData({this.appName, this.value, this.themeData});
  16.  
  17. AppContextData withName(String newAppName) {
  18. return new AppContextData(
  19. appName: newAppName, value: value, themeData: themeData);
  20. }
  21.  
  22. AppContextData increment() {
  23. return new AppContextData(
  24. appName: appName, value: value + 1, themeData: themeData);
  25. }
  26.  
  27. AppContextData withTheme(ThemeData themeData) {
  28. return new AppContextData(
  29. appName: appName, value: value, themeData: themeData);
  30. }
  31. }
  32.  
  33. class AppContext extends InheritedWidget {
  34. final AppContextData appContextData;
  35. final Function(String title) updateTitle;
  36. final Function() increment;
  37. final Function() changeTheme;
  38.  
  39. AppContext({
  40. Key key,
  41. @required this.appContextData,
  42. @required this.updateTitle,
  43. @required this.increment,
  44. @required this.changeTheme,
  45. @required Widget child,
  46. })
  47. : super(key: key, child: child);
  48.  
  49. static AppContext of(BuildContext context) {
  50. return context.inheritFromWidgetOfExactType(AppContext);
  51. }
  52.  
  53. @override
  54. bool updateShouldNotify(AppContext old) =>
  55. appContextData != old.appContextData;
  56. }
  57.  
  58. class AppContainer extends StatefulWidget {
  59. @override
  60. _AppContainerState createState() => new _AppContainerState();
  61. }
  62.  
  63. class _AppContainerState extends State<AppContainer> {
  64. AppContextData appContextData;
  65.  
  66. @override
  67. void initState() {
  68. appContextData = new AppContextData(
  69. appName: "mon titre",
  70. value: 0,
  71. themeData: new ThemeData(
  72. primarySwatch: Colors.blue,
  73. ));
  74.  
  75. super.initState();
  76. }
  77.  
  78. @override
  79. Widget build(BuildContext context) {
  80. return new AppContext(
  81. appContextData: appContextData,
  82. updateTitle: _updateTitle,
  83. increment: _increment,
  84. changeTheme: _changeTheme,
  85. child: new MyApp());
  86. }
  87.  
  88. void _updateTitle(String title) {
  89. setState(() {
  90. appContextData = appContextData.withName(title);
  91. });
  92. }
  93.  
  94. void _increment() {
  95. setState(() {
  96. appContextData = appContextData.increment();
  97. });
  98. }
  99.  
  100. void _changeTheme() {
  101. setState(() {
  102. Random rnd = new Random();
  103. var index = rnd.nextInt(Colors.primaries.length);
  104. appContextData = appContextData.withTheme(new ThemeData(
  105. primarySwatch: Colors.primaries[index],
  106. ));
  107. });
  108. }
  109. }
  110.  
  111. class MyApp extends StatelessWidget {
  112. @override
  113. Widget build(BuildContext context) {
  114. final appContext = AppContext.of(context);
  115. final appContextData = appContext.appContextData;
  116.  
  117. return new MaterialApp(
  118. title: 'Flutter Demo',
  119. theme: appContextData.themeData,
  120. home: new MyHomePage(),
  121. );
  122. }
  123. }
  124.  
  125. class MyHomePage extends StatelessWidget {
  126. @override
  127. Widget build(BuildContext context) {
  128. final appContext = AppContext.of(context);
  129. final appContextData = appContext.appContextData;
  130.  
  131. return new Scaffold(
  132. appBar: new AppBar(
  133. title: new Text(appContextData.appName),
  134. ),
  135. body: new Center(
  136. child: new Column(
  137. mainAxisAlignment: MainAxisAlignment.center,
  138. children: <Widget>[
  139. new Text(
  140. 'You have pushed the button this many times:',
  141. ),
  142. new Text(
  143. '${appContextData.value}',
  144. style: Theme.of(context).textTheme.display1,
  145. ),
  146. new Text("Set new title below:"),
  147. new TextField(onChanged: appContext.updateTitle),
  148. new RaisedButton(
  149. onPressed: appContext.changeTheme,
  150. child: new Text("change theme"),
  151. )
  152. ],
  153. ),
  154. ),
  155. floatingActionButton: new FloatingActionButton(
  156. onPressed: appContext.increment,
  157. tooltip: 'Increment',
  158. child: new Icon(Icons.add),
  159. ),
  160. );
  161. }
  162. }
Add Comment
Please, Sign In to add comment