Skip to content

Instantly share code, notes, and snippets.

@ryanlid
Created January 15, 2020 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlid/7c60cac663677a014322f5051d1268a2 to your computer and use it in GitHub Desktop.
Save ryanlid/7c60cac663677a014322f5051d1268a2 to your computer and use it in GitHub Desktop.
路由示例
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