flutter中普通路由的实现

学习flutter的第三天,普通跳转带参数跳到第二个页面,并且返回参数

class RouteDemo extends StatefulWidget {
  const RouteDemo({Key? key}) : super(key: key);

  @override
  State createState() => _RouteDemoState();
}

class _RouteDemoState extends State {
  @override
  Widget build(BuildContext context) {

    return TextButton(onPressed: () async{
    var result= await Navigator.of(context).push(
         MaterialPageRoute(builder: (context){
           return const TwoRoute(
             title: "第二个页面",
             age: 100,
           );
         })
      );
    print("result:$result");

    }, child:const Text("跳转"));
  }
}

使用异步和await获取第二个页面的数据的原因是因为如果同步加载的话,print会在跳转之后就执行,加异步的话会等后面的页面执行之后再执行
接收参数的页面

class TwoRoute extends StatelessWidget {
  final String title;
  final int age;
  const  TwoRoute({Key? key,
  required this.title,required this.age}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("第二个页面"),
      ),
      body: Center(
        child: Column(
          children: [
            Text("title>>>>$title"),
            Text("data age:::$age"),
            ElevatedButton(
                onPressed: (){
                  Navigator.of(context).pop("todo 返回");
                },
                child: Text("返回"))
          ],
        ),
      ),
    );
  }
}


你可能感兴趣的:(flutter中普通路由的实现)