Flutter 中的命名路由跳转传值

发布于 5 个月前 作者 phonegap100 210 次浏览 来自 分享

官方文档:https://flutter.dev/docs/cookbook/navigation/navigate-with-arguments Flutter 中的命名路由跳转传值完整代码如下,如果想获取更多内容请访问:https://www.itying.com/goods-1101.html

1、Flutter 中的命名路由跳转传值第一步-配置路由:


import 'package:flutter/material.dart';
import 'pages/Tabs.dart';
import 'pages/Search.dart';
import 'pages/Form.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final routes = {
    '/': (contxt) => Tabs(),
    '/search': (contxt) => SearchPage(),
    '/form': (context, {arguments}) => FormPage(arguments: arguments),
  };

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Tabs(),
        onGenerateRoute: (RouteSettings settings) {
          // 统一处理
          final String name = settings.name;
          final Function pageContentBuilder = this.routes[name];
          if (pageContentBuilder != null) {
            if (settings.arguments != null) {
              final Route route = MaterialPageRoute(
                  builder: (context) => pageContentBuilder(context,
                      arguments: settings.arguments));
              return route;
            } else {
              final Route route = MaterialPageRoute(
                  builder: (context) => pageContentBuilder(context));
              return route;
            }
          }
        });
  }
}

2、Flutter 中的命名路由跳转传值第二步 跳转传值

RaisedButton(
          child: Text("跳转到表单演示页面"),
          onPressed: (){
            Navigator.pushNamed(context, '/form',arguments: {
              "id":20
            });                
          },
          color: Theme.of(context).accentColor,
          textTheme: ButtonTextTheme.primary
)

3、Flutter 中的命名路由跳转传值第三步-接收参数

import 'package:flutter/material.dart';

class FormPage extends StatelessWidget {
  final Map arguments;
  FormPage({this.arguments});
 
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("搜索"),
      ),
      body:Text("我是一个表单页面 ${arguments != null ? arguments['id'] : '0'}")

    );
  }
}
回到顶部