• 导航栏返回带的数据


    代码:

    import 'package:flutter/material.dart';

    void main(List<String> args) {
    runApp(MaterialApp(
    title: '导航返回数据',
    home: FirstPage(),
    ));
    }

    class FirstPage extends StatelessWidget{
    @override
    Widget build(BuildContext context) {
    return Scaffold(
    appBar: AppBar(title: Text('首页'),),
    body: Center(
    child: RouteButton(),
    ),
    );
    }
    }
    class RouteButton extends StatelessWidget {

    @override
    Widget build(BuildContext context) {
    return RaisedButton(
    child: Text('去旅行'),
    onPressed: (){
    _goToChooseTripLine(context);
    },
    );
    }
     
    _goToChooseTripLine(BuildContext context) async{

    final result = await Navigator.push(context, MaterialPageRoute(
    builder: (context) => Trip()
    ));
    Scaffold.of(context).showSnackBar(SnackBar(content: Text('$result')));
    }
     
    }


    class Trip 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,'北京到西藏 4天3晚 5000元');
    },
    ),

    RaisedButton(
    child: Text('北京到上海'),
    onPressed: (){
    Navigator.pop(context,'北京到上海 5天4晚 4000元');
    },
    )
    ],
    ),
    )
    );
    }
    }
     
    总结:

    //导航带回数据

    //异步请求

    _变量名(BuildContext context) async{

    异步等待导航栏压栈的结果—类似于OC的block

    final 变量2= await Navigator.push(context,MaterialPageRoute(builder:(context)=>类名());

    //toast

    scaffold.of(context).showSnackBar(SnackBar(content:Text(‘$变量2’)))

    }

    //类似于函数 不过这个函数写成了一个变量

    onPressed:(){

    _变量名(context);

    }

    onPressed:(){

    Navigator.pop(context,’xxxxx’)//xxxx就是导航栏需要传回去的数据

    }

  • 相关阅读:
    [VirtaulBox]网络连接设置
    LeetCode
    LeetCode
    LeetCode
    LeetCode-37.Sudok Solver
    LeetCode-36.Valid Sudoku
    LeetCode-52.N-Queen II
    LeetCode-51.N-Queens
    LeetCode-22.Generate Parentheses
    LeetCode-111.Mininum Depth of Binary Tree
  • 原文地址:https://www.cnblogs.com/pp-pping/p/12166968.html
Copyright © 2020-2023  润新知