美文网首页
flutter 命名路由的传值与接收

flutter 命名路由的传值与接收

作者: 彬彬彬boboc | 来源:发表于2019-12-23 17:45 被阅读0次

定义命名路由

//引入两个页面组件
import 'pages/homePage/seach.dart';
import 'pages/homePage/edit.dart';

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    
    return MaterialApp(
      home: Tabs(),
      //定义命名路由
      routes: {
        '/seach': (context)=>Seach(),
        '/edit': (context)=>Edit(),
      },
    );
  }
}

传值

Navigator.of(context).pushNamed('/edit',arguments: "我是传给编辑页面的");

接收

使用:ModalRoute.of(context).settings.arguments

class _EditState extends State<Edit> {
  String title;
  @override
  Widget build(BuildContext context) {
    setState(() {
      this.title = ModalRoute.of(context).settings.arguments;
    });
    return Scaffold(
      appBar: AppBar(title: Text(this.title)),
      body: Text("设置内容"),
    );
  }
}

相关文章

网友评论

      本文标题:flutter 命名路由的传值与接收

      本文链接:https://www.haomeiwen.com/subject/zjctoctx.html