import 'package:flutter/material.dart';
import 'NewRoute.dart';
// 写法①
// >>>>>>>>>>>>> runApp写法① >>>>>>>>>>>>>
// void main() {
// runApp(new MaterialApp(
// title: '1.MaterialApp in main',
// home: new HelloWorldPage(),
// ));
// }
// <<<<<<<<<<<<< runApp写法① <<<<<<<<<<<<<
// 写法②
// >>>>>>>>>>>>> runApp写法② >>>>>>>>>>>>>
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '2.MaterialApp in MyApp',
// 页面跳转的方式②:注册路由表
// 详情查看[官网路由管理](https://book.flutterchina.club/chapter2/flutter_router.html)
// 缺点:路由传递的参数无法动态修改(如果路由有参数)
routes: {
"new_page": (context) => NewRoute(),
},
home: HelloWorldPage(),
);
}
}
// <<<<<<<<<<<<< runApp写法② <<<<<<<<<<<<<
class HelloWorldPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
//Scaffold是Material中主要的布局组件.
return new Scaffold(
appBar: new AppBar(
leading: new IconButton(
icon: new Icon(Icons.menu),
tooltip: 'Navigation menu',
onPressed: null,
),
title: new Text('MaterialApp Example title'),
actions: <Widget>[
new IconButton(
icon: new Icon(Icons.search),
tooltip: 'Search',
onPressed: null,
),
],
),
//body占屏幕的大部分
body: new Center(
child: new Text('Hello, world2!'),
),
floatingActionButton: new FloatingActionButton(
tooltip: 'Add', // used by assistive technologies
child: new Icon(Icons.add),
// 1、无方法
// 写法①
// onPressed: null,
// 写法②
// onPressed: () {}
// 写法③
// onPressed: () => {}
// 2、有方法,但该方法无参数
// 写法①
// onPressed: printLog,
// 写法②
// onPressed: () {
// printLog();
// },
// 3、有方法,且该方法有参数
// onPressed:() {
// printText("Hello world");
// }
// 4.页面跳转方式
// 写法①
// onPressed: () {
// goNextPage(context);
// },
// 写法②
// onPressed: () {
// Navigator.pushNamed(context, "new_page");
// },
// 写法③
onPressed: () {
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new NewRoute();
}));
},
)
);
}
}
void printLog() {
print("This is printLog Method");
}
void printText($text) {
print("The text is " + $text);
}
void goNextPage(context) {
// 导航到新路由
Navigator.push(context, new MaterialPageRoute(builder: (context) {
return new NewRoute();
}));
}