flutter 页面跳转

》一惯发风格,不多哔哔直接上代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Navigator'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
          child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          TextButton(
            child: Text("点击跳转到NewRoute页面"),
            onPressed: () {
              //导航到新路由
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) {
                  return aaa();
                }),
              );
            },
          ),
          TextButton(
            onPressed: () {
              //导航到新路由
              Navigator.push(context, MaterialPageRoute(builder: ((context) {
                return bbb();
              })));
            },
            child: Text(
              "点击跳转到第二个界面",
              style: TextStyle(fontSize: 20.0, color: Colors.red),
            ),
          ),
          TextButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(
                  builder: (context) {
                    return ccc();
                  },
                ));
              },
              child: Text(
                "点击跳转到第三个界面",
                style: TextStyle(
                  fontSize: 25.0,
                  color: Colors.black54,
                ),
              ))
        ],
      )),
    );
  }
}

class aaa extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("新界面"),
      ),
      body: Center(
        child: Text("这是跳转过来的界面"),
      ),
    );
  }
}

class bbb extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("新界面2"),
        ),
        body:Center(
        child: Text("这是跳转过来的界面2"),
      ),);
  }
}

class ccc extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("新界面3"),
        ),
        body: Center(
        child: Text("这是跳转过来的界面3"),
      ),);
  }
}

猜你喜欢

转载自blog.csdn.net/f234344435/article/details/126968639