flutter页面跳转并传值

页面跳转传值

废话不多说上代码

InkWell可以给任意Widget添加点击事件,我们在这里通过ontap事件点击跳转到子页面
then是页面跳过去,回来后就会执行then里面的方法,子页面传过来的值,then里面的匿名方法会接收到
push后面的 <参数类型> 有了then就要和then里面方法的参数类型一样,没有then就不需要加参数类型

 InkWell(
                onTap: () {
    
    
                  print('我跳了');
                  Navigator.push<List>(
                          context,
                          MaterialPageRoute(
                              builder: (context) => checkBoxList()))
                      .then((List data) {
    
    
                    if (data != null) {
    
    
                       setState(() {
    
    
                        popData = data;
                      });
                    } 
                  });
                },
                child: _commonInputBox('imgs/Subcontractor.png', '工种', false),
              ),

我们通过按钮的点击事件,POP跳转页面,并把listData这个数组传给父页面,完成页面的跳转传值

class _checkBoxListState extends State<checkBoxList> {
    
    
  bool groupValue = false;
  @override
  Widget build(BuildContext context) {
    
    
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text('复选列表'),
        actions: <Widget>[
          RaisedButton(
            child: Text('保存'),
            onPressed: (){
    
    
              Navigator.pop(context, listData);
              print(listData);
            },
          )
        ],
      ),
      body: itemList(listData),
    );
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_44275763/article/details/105701017