main.dart 7.6 KB

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