zoukankan      html  css  js  c++  java
  • Flutter页面跳转返回数据

    Dart中的异步请求和等待和ES6中的方法很像,直接使用async...await就可以实现。

    核心代码:

    _navigateToAddress(BuildContext context) async{ //async是启用异步方法
    
        final result = await Navigator.push(//等待
            context,
            MaterialPageRoute(builder: (context)=> Address())
        );
    
        Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));
      }

    示例:

    import 'package:flutter/material.dart';
    
    void main(){
      runApp(MaterialApp(
          title:'页面跳转返回数据',
          home:FirstPage()
      ));
    }
    
    class FirstPage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            appBar:AppBar(title:Text("商品列表")),
            body:Center(
              child: RouteButton(),
            )
        );
      }
    }
    
    //跳转的Button
    class RouteButton extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return RaisedButton(
          onPressed:(){
            _navigateToAddress(context);
          },
          child: Text('选择地址'),
        );
      }
    
      _navigateToAddress(BuildContext context) async{ //async是启用异步方法
    
        final result = await Navigator.push(//等待
            context,
            MaterialPageRoute(builder: (context)=> Address())
        );
    
        Scaffold.of(context).showSnackBar(SnackBar(content:Text('$result')));
      }
    }
    
    class Address extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar:AppBar(
              title:Text('地址列表')
          ),
          body:Center(
              child:Column(
                children: <Widget>[
                  RaisedButton(
                    child: Text('地址一'),
                    onPressed: (){
                      Navigator.pop(context,'地址一:北京');
                    },
                  ) ,
                  RaisedButton(
                    child: Text('地址二'),
                    onPressed: (){
                      Navigator.pop(context,'地址二:上海');
                    },
                  ) ,
                ],
              )
          ) ,
        );
      }
    }
  • 相关阅读:
    「开始,DP专题」
    ZOJ3612 Median treap
    [HNOI2004]宠物收养所 treap
    SIRO Challenge 状态压缩 + DP 未解
    SPOJ3273 Order statistic set treap模板
    HNU2[I题]Omar Loves Candies 贪心
    HNU1[B题] DP,数位DP
    HNU1[F题] 栈模拟计算
    Node.js权威指南 (6)
    瘦下来之后你会遇见不一样的自己
  • 原文地址:https://www.cnblogs.com/loaderman/p/11250889.html
Copyright © 2011-2022 走看看