最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Flutter Navigator路由傳參的實(shí)現(xiàn)

 更新時(shí)間:2022年04月22日 09:21:52   作者:WEB前端李志杰  
本文主要介紹了Flutter Navigator路由傳參的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Flutter中的默認(rèn)導(dǎo)航分成兩種,一種是命名的路由,一種是構(gòu)建路由。

一、命名路由傳參

應(yīng)用入口處定義路由表

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false, // 隱藏預(yù)覽中的debug
      title: 'Flutter Demo',
      routes: {
        '/': (context) => const HomePage(),
        "menu": (context) => const MenuPage()
      },
    );
  }
}
// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登錄"),
      ),
      body: ElevatedButton(
        onPressed: () async {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          var result = await Navigator.pushNamed(context, 'menu',
              arguments: {'name': 'title'});
          print(result);
        },
        child: const Text('登錄'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text('菜單' + argumentsData.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context, {'name': "Navigator.pop傳參"});
        },
        child: const Text("返回"),
      ),
    );
  }
}

二、構(gòu)建路由傳參

從HomePage頁(yè)面跳轉(zhuǎn)MenuPage頁(yè)面時(shí),攜帶參數(shù)

第一種方式:

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage ({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登錄"),
      ),
      body: ElevatedButton(
        onPressed: () {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const MenuPage(
                title: '菜單123',
              ), // 需要跳轉(zhuǎn)的頁(yè)面
            ), // 修改路由的名稱、信息等
          );
        },
        child: const Text('登錄'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  // 定義接收的字段
  final String title;
  const MenuPage({Key? key, required this.title}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: const Text("返回"),
      ),
    );
  }
}

第二種方式:

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage({Key? key}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登錄"),
      ),
      body: ElevatedButton(
        onPressed: () {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          Navigator.push(
            context,
            MaterialPageRoute(
                builder: (context) => const MenuPage(),
                // 修改路由的名稱、信息等
                settings: const RouteSettings(
                    name: '菜單', arguments: {"name": '123'}) // 需要跳轉(zhuǎn)的頁(yè)面
                ),
          );
        },
        child: const Text('登錄'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    dynamic argumentsData = ModalRoute.of(context)?.settings.arguments;
    return Scaffold(
      appBar: AppBar(
        title: Text('菜單' + argumentsData.toString()),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context);
        },
        child: const Text("返回"),
      ),
    );
  }
}

從MenuPage頁(yè)面返回HomePage頁(yè)面時(shí),攜帶參數(shù)

// 定義HomePage
class HomePage extends StatelessWidget {
  const HomePage ({Key? key}) : super(key: key);;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("登錄"),
      ),
      body: ElevatedButton(
        onPressed: () async {
          // 實(shí)現(xiàn)路由跳轉(zhuǎn)
          var result = await Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) => const MenuPage(),
            ),
          );
          print(result);
        },
        child: const Text('登錄'),
      ),
    );
  }
}
// 定義MenuPage
class MenuPage extends StatelessWidget {
  const MenuPage({Key? key}) : super(key: key);
  @override
  // 接收傳參
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('菜單'),
      ),
      body: ElevatedButton(
        onPressed: () {
          Navigator.pop(context, {'name': "Navigator.pop傳參"});
        },
        child: const Text("返回"),
      ),
    );
  }
}

到此這篇關(guān)于Flutter Navigator路由傳參的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Flutter Navigator路由傳參內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論

辉南县| 郓城县| 托克托县| 洪泽县| 和林格尔县| 酒泉市| 拉孜县| 吴忠市| 柳江县| 太仓市| 富川| 南康市| 普定县| 浏阳市| 西吉县| 郸城县| 新邵县| 金平| 青铜峡市| 乌拉特中旗| 呼伦贝尔市| 冀州市| 黎平县| 陵川县| 将乐县| 丽江市| 大宁县| 穆棱市| 黎川县| 晋宁县| 绍兴县| 建宁县| 乌海市| 双城市| 英超| 宜州市| 惠来县| 牡丹江市| 顺义区| 高邮市| 马龙县|