Flutter之页面间传参

例如,Manage页面传参给EditQuiz页面,

Manage页面写法:

onPressed: (){
  Navigator.of(context).push(MaterialPageRoute(builder: (_) => EditQuiz(id:id))));
}

EditQuiz页面写法:

class EditQuiz extends StatefulWidget {
  int id;
  EditQuiz({this.id});

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

EditQuiz页面调用:

print(widget.id);

若EditQuiz页面回传参数给Manage页面,然后执行Manage页面的reload函数。操作如下:

Manage页面写法:

onPressed: (){
  Navigator.of(context).push(MaterialPageRoute(builder: (_) => EditQuiz(id:id))).then((val) => val!=null?(val ? reload() : null):null);
}

EditQuiz页面写法:

Navigator.pop(context,true);

即可。

posted @ 2021-06-17 22:31  罗毅豪  阅读(762)  评论(0编辑  收藏  举报