Created
January 15, 2020 14:19
-
-
Save ryanlid/7c60cac663677a014322f5051d1268a2 to your computer and use it in GitHub Desktop.
路由示例
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
void main() => runApp((MyApp())); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
home: MyHomePage(), | |
title: "MaterialApp示例", | |
routes: { | |
'/first': (BuildContext context) => FirstPage(), | |
'/second': (BuildContext context) => SecondPage() | |
}, | |
initialRoute: '/first', | |
); | |
} | |
} | |
class MyHomePage extends StatefulWidget { | |
@override | |
_MyHomePageState createState() => _MyHomePageState(); | |
} | |
class _MyHomePageState extends State<MyHomePage> { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("MaterialApp 示例"), | |
), | |
body: Center( | |
child: RaisedButton( | |
onPressed: () { | |
Navigator.pushNamed(context, '/first'); | |
}, | |
child: Text( | |
'主页', | |
style: TextStyle(fontSize: 28.0), | |
), | |
), | |
), | |
); | |
} | |
} | |
class FirstPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("第一屏"), | |
), | |
body: Center( | |
child: RaisedButton( | |
onPressed: () { | |
Navigator.of(context).pushNamed('/second'); | |
}, | |
child: Text('进入第二屏'), | |
), | |
), | |
); | |
} | |
} | |
class SecondPage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text("第二屏"), | |
), | |
body: Center( | |
child: RaisedButton( | |
onPressed: () { | |
Navigator.of(context).pop(); | |
}, | |
child: Text('返回上一屏'), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment