zoukankan      html  css  js  c++  java
  • 命名路由、命名路由传值

    获取上一个页面pop的返回参数 Navigator.pop(context, {'title': '123789'});

    在跳转的地方

    onTap: () async{
    var result = await Navigator.pushNamed(context, '/avatar');
    print(result);
    },

    修改完成后, 最好重启项目 flutter run

    route.dart里面

    import 'package:flutter/material.dart';
    import '../home.dart';
    import '../other/searchPage.dart';

    ////配置路由
    var routes = {
    '/':(context) => Home(),
    '/search':(context, { arguments }) => SearchPage(arguments: arguments),
    };

    //固定写法
    var onGenerateRoute=(RouteSettings settings) {
    final String name = settings.name;
    final Function pageContentBuilder = 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;
    }
    }
    };


    main.dart里面
    import 'package:flutter/material.dart';
    import 'app/routers/route.dart';;

    void main() {
    runApp(MyApp());
    }

    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
    initialRoute: '/',
    // home: Home(),
    onGenerateRoute: onGenerateRoute
    );
    }
    }

    发送传参的地方
    Navigator.pushNamed(context, '/search', arguments: {
    'title': '13213'
    });

    接收的地方 无状态的组件接收 StatelessWidget
    class SearchPage extends StatelessWidget{
    var arguments; // 要接收别的组件传过来的值 只能是arguments
      SearchPage({arguments});
    @override
    Widget build(BuildContext context) {
    // TODO: implement buildr
    return Container(
    child: Text('12321321大大${arguments != null ? arguments["title"] : ""}'),
        );
    }
    }


    接收的地方 有状态的组件接收 StatefulWidget
    class ProductInfoPage extends StatefulWidget {
    final Map arguments;
    ProductInfoPage({Key key,arguments}) : super(key: key);

    _ProductInfoPageState createState() => _ProductInfoPageState(arguments: arguments);
    }

    class _ProductInfoPageState extends State<ProductInfoPage> {
    Map arguments;
    _ProductInfoPageState({arguments});
    @override
    Widget build(BuildContext context) {
    return null;
    }
    }
     
  • 相关阅读:
    SVN中建立项目
    Dojo 学习笔记 之 Dojo hitch&partial
    Javascript的“上下文”(context)
    栅格那点儿事(四E)
    栅格那点儿事(四D)
    栅格那点儿事(四C)
    Sqoop Import数据库时中文乱码解决方案
    Sqoop架构
    Sqoop环境安装
    Sqoop概述
  • 原文地址:https://www.cnblogs.com/zhaofeis/p/12339850.html
Copyright © 2011-2022 走看看