main.dart 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import 'package:flutter/material.dart';
  2. import 'package:english_words/english_words.dart';
  3. void main() {
  4. runApp(MyApp());
  5. }
  6. class MyApp extends StatelessWidget {
  7. // This widget is the root of your application.
  8. @override
  9. Widget build(BuildContext context) {
  10. return MaterialApp(
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. // This is the theme of your application.
  14. //
  15. // Try running your application with "flutter run". You'll see the
  16. // application has a blue toolbar. Then, without quitting the app, try
  17. // changing the primarySwatch below to Colors.green and then invoke
  18. // "hot reload" (press "r" in the console where you ran "flutter run",
  19. // or simply save your changes to "hot reload" in a Flutter IDE).
  20. // Notice that the counter didn't reset back to zero; the application
  21. // is not restarted.
  22. primarySwatch: Colors.blue,
  23. // This makes the visual density adapt to the platform that you run
  24. // the app on. For desktop platforms, the controls will be smaller and
  25. // closer together (more dense) than on mobile platforms.
  26. visualDensity: VisualDensity.adaptivePlatformDensity,
  27. ),
  28. // home: MyHomePage(title: 'Flutter Demo Home Page'),
  29. routes: {
  30. "new_page": (context) => NewRoute(),
  31. "/": (context) => MyHomePage(title: '首页'), //注册首页路由
  32. },
  33. onGenerateRoute: (RouteSettings settings) {
  34. return MaterialPageRoute(builder: (context) {
  35. String routeName = settings.name;
  36. print(routeName);
  37. // 如果访问的路由页需要登录,但当前未登录,则直接返回登录页路由,
  38. // 引导用户登录;其它情况则正常打开路由。
  39. });
  40. });
  41. // 路由钩子
  42. }
  43. }
  44. class MyHomePage extends StatefulWidget {
  45. MyHomePage({Key key, this.title}) : super(key: key);
  46. // This widget is the home page of your application. It is stateful, meaning
  47. // that it has a State object (defined below) that contains fields that affect
  48. // how it looks.
  49. // This class is the configuration for the state. It holds the values (in this
  50. // case the title) provided by the parent (in this case the App widget) and
  51. // used by the build method of the State. Fields in a Widget subclass are
  52. // always marked "final".
  53. final String title;
  54. @override
  55. _MyHomePageState createState() => _MyHomePageState();
  56. }
  57. class _MyHomePageState extends State<MyHomePage> {
  58. int _counter = 0;
  59. // 111
  60. void _incrementCounter() {
  61. setState(() {
  62. // This call to setState tells the Flutter framework that something has
  63. // changed in this State, which causes it to rerun the build method below
  64. // so that the display can reflect the updated values. If we changed
  65. // _counter without calling setState(), then the build method would not be
  66. // called again, and so nothing would appear to happen.
  67. _counter++;
  68. });
  69. }
  70. @override
  71. Widget build(BuildContext context) {
  72. // This method is rerun every time setState is called, for instance as done
  73. // by the _incrementCounter method above.
  74. //
  75. // The Flutter framework has been optimized to make rerunning build methods
  76. // fast, so that you can just rebuild anything that needs updating rather
  77. // than having to individually change instances of widgets.
  78. return Scaffold(
  79. appBar: AppBar(
  80. // Here we take the value from the MyHomePage object that was created by
  81. // the App.build method, and use it to set our appbar title.
  82. title: Text(widget.title),
  83. ),
  84. body: Center(
  85. // Center is a layout widget. It takes a single child and positions it
  86. // in the middle of the parent.
  87. child: Column(
  88. // Column is also a layout widget. It takes a list of children and
  89. // arranges them vertically. By default, it sizes itself to fit its
  90. // children horizontally, and tries to be as tall as its parent.
  91. //
  92. // Invoke "debug painting" (press "p" in the console, choose the
  93. // "Toggle Debug Paint" action from the Flutter Inspector in Android
  94. // Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
  95. // to see the wireframe for each widget.
  96. //
  97. // Column has various properties to control how it sizes itself and
  98. // how it positions its children. Here we use mainAxisAlignment to
  99. // center the children vertically; the main axis here is the vertical
  100. // axis because Columns are vertical (the cross axis would be
  101. // horizontal).
  102. mainAxisAlignment: MainAxisAlignment.center,
  103. children: <Widget>[
  104. Text(
  105. '你已经按了很多次了开始吧:',
  106. ),
  107. Text(
  108. '$_counter',
  109. style: Theme.of(context).textTheme.headline4,
  110. ),
  111. FlatButton(
  112. child: Text("open new route"),
  113. textColor: Colors.red,
  114. onPressed: () {
  115. // 普通路由导航
  116. // Navigator.pushNamed(context, "new_page");
  117. // 在打开路由时传递参数
  118. Navigator.of(context).pushNamed("new_page", arguments: "hi");
  119. //导航到新路由
  120. // Navigator.push(
  121. // context,
  122. // MaterialPageRoute(
  123. // builder: (context) {
  124. // return NewRoute();
  125. // },
  126. // maintainState: false,
  127. // fullscreenDialog: false));
  128. },
  129. ),
  130. RouterTestRoute(),
  131. RandomWordsWidget()
  132. ],
  133. ),
  134. ),
  135. floatingActionButton: FloatingActionButton(
  136. onPressed: _incrementCounter,
  137. tooltip: 'Increment',
  138. child: Icon(Icons.add),
  139. ), // This trailing comma makes auto-formatting nicer for build methods.
  140. );
  141. }
  142. }
  143. // 新的路由
  144. class NewRoute extends StatelessWidget {
  145. @override
  146. Widget build(BuildContext context) {
  147. //获取路由参数
  148. var args = ModalRoute.of(context).settings.arguments;
  149. return Scaffold(
  150. appBar: AppBar(
  151. title: Text("New route" + args),
  152. ),
  153. body: Center(
  154. child: Column(
  155. children: [
  156. Text("This is new route" + args),
  157. RandomWordsWidget(),
  158. Image(
  159. image: AssetImage("images/avatar.png"),
  160. width: 100.0,
  161. color: Colors.blue,
  162. colorBlendMode: BlendMode.difference,
  163. ),
  164. // Image(
  165. // image: NetworkImage(
  166. // "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4"),
  167. // width: 100.0,
  168. // color: Colors.blue,
  169. // // colorBlendMode: BlendMode.difference,
  170. // ),
  171. // Image.network(
  172. // "https://avatars2.githubusercontent.com/u/20411648?s=460&v=4",
  173. // width: 100.0,
  174. // )
  175. ],
  176. )),
  177. );
  178. }
  179. }
  180. class TipRoute extends StatelessWidget {
  181. TipRoute({
  182. Key key,
  183. @required this.text, // 接收一个text参数
  184. }) : super(key: key);
  185. final String text;
  186. @override
  187. Widget build(BuildContext context) {
  188. return Scaffold(
  189. appBar: AppBar(
  190. title: Text("提示"),
  191. ),
  192. body: Padding(
  193. padding: EdgeInsets.all(18),
  194. child: Center(
  195. child: Column(
  196. children: <Widget>[
  197. Text(text),
  198. RaisedButton(
  199. onPressed: () => Navigator.pop(context, "我是返回值"),
  200. child: Text("返回"),
  201. )
  202. ],
  203. ),
  204. ),
  205. ),
  206. );
  207. }
  208. }
  209. class RouterTestRoute extends StatelessWidget {
  210. @override
  211. Widget build(BuildContext context) {
  212. return Center(
  213. child: RaisedButton(
  214. onPressed: () async {
  215. // 打开`TipRoute`,并等待返回结果
  216. var result = await Navigator.push(
  217. context,
  218. MaterialPageRoute(
  219. builder: (context) {
  220. return TipRoute(
  221. // 路由参数
  222. text: "我是提示xxxx",
  223. );
  224. },
  225. ),
  226. );
  227. //输出`TipRoute`路由返回结果
  228. print("路由返回值: $result");
  229. },
  230. child: Text("打开提示页"),
  231. ),
  232. );
  233. }
  234. }
  235. class RandomWordsWidget extends StatelessWidget {
  236. @override
  237. Widget build(BuildContext context) {
  238. // 生成随机字符串
  239. final wordPair = new WordPair.random();
  240. return Padding(
  241. padding: const EdgeInsets.all(8.0),
  242. child: new Text(wordPair.toString()),
  243. );
  244. }
  245. }