main.dart

  1. //main.dart
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter01/views/Detail.dart';
  4. import 'package:flutter01/views/Home.dart';
  5. void main() {
  6. runApp(MyApp());
  7. }
  8. class MyApp extends StatelessWidget {
  9. @override
  10. Widget build(BuildContext context) {
  11. return MaterialApp(
  12. title: "Flutter",
  13. routes: {
  14. "detail":(context)=>MyDetail(),
  15. "home":(context)=>MyHome()
  16. },
  17. initialRoute: "home",
  18. //初始化路由
  19. );
  20. }
  21. }

Home.dart

  1. //Home.dart
  2. import 'package:flutter/material.dart';
  3. class MyHome extends StatefulWidget {
  4. @override
  5. _MyHomeState createState() => _MyHomeState();
  6. }
  7. class _MyHomeState extends State<MyHome> {
  8. @override
  9. Widget build(BuildContext context) {
  10. return Scaffold(
  11. appBar: AppBar(
  12. title: Text('Title'),
  13. ),
  14. body: Row(
  15. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  16. children: <Widget>[
  17. RaisedButton(child: Text("go-Detail"),onPressed: getDetail,)
  18. ],
  19. ),
  20. );
  21. }
  22. getDetail(){
  23. Navigator.pushNamed(context, "detail");
  24. }
  25. }

Detail.dart

  1. //Detail.dart
  2. import 'package:flutter/material.dart';
  3. class MyDetail extends StatelessWidget {
  4. @override
  5. Widget build(BuildContext context) {
  6. return Scaffold(
  7. appBar: AppBar(
  8. title: Text('Title'),
  9. ),
  10. body: Container(
  11. child: Text("详情页"),
  12. ),
  13. );
  14. }
  15. }