欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)文章

  • Android讀寫(xiě)文件工具類詳解

    Android讀寫(xiě)文件工具類詳解

    這篇文章主要為大家詳細(xì)介紹了Android讀寫(xiě)文件工具類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-03-03
  • Android如何快速適配暗黑模式詳解

    Android如何快速適配暗黑模式詳解

    微信在前段時(shí)間的更新中也實(shí)現(xiàn)了暗黑模式,而蘋(píng)果系統(tǒng)也早就支持暗黑模式,Android也一樣可以實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Android如何快速適配暗黑模式的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • android開(kāi)發(fā)教程之文本框加滾動(dòng)條scrollview

    android開(kāi)發(fā)教程之文本框加滾動(dòng)條scrollview

    EditText與TextView加上滾動(dòng)條其實(shí)很簡(jiǎn)單,只需要在文本輸入框或者文本顯示框上面加上滾動(dòng)條控件即可
    2014-02-02
  • Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    Android仿微信錄音功能(錄音后的raw文件轉(zhuǎn)mp3文件)

    這篇文章主要介紹了Android中仿微信錄音功能錄音后的raw文件轉(zhuǎn)mp3文件,本文通過(guò)實(shí)例代碼給大家講解的非常詳細(xì),需要的朋友可以參考下
    2019-11-11
  • Android端代碼量非常小的分頁(yè)加載庫(kù)

    Android端代碼量非常小的分頁(yè)加載庫(kù)

    這篇文章主要給大家介紹了關(guān)于Android端代碼量非常小的分頁(yè)加載庫(kù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Android ListView詳解

    Android ListView詳解

    listview控件在項(xiàng)目開(kāi)發(fā)過(guò)程中經(jīng)常會(huì)用到,本文給大家分享android listview相關(guān)知識(shí),感興趣的朋友一起學(xué)習(xí)吧
    2015-12-12
  • Android中實(shí)現(xiàn)多線程操作的幾種方式

    Android中實(shí)現(xiàn)多線程操作的幾種方式

    多線程一直是一個(gè)老大難的問(wèn)題,首先因?yàn)樗y以理解,其次在實(shí)際工作中我們需要面對(duì)的關(guān)于線程安全問(wèn)題也并不常見(jiàn),今天就來(lái)總結(jié)一下實(shí)現(xiàn)多線程的幾種方式,感興趣的可以了解一下
    2021-06-06
  • 使用 Swift 語(yǔ)言編寫(xiě) Android 應(yīng)用入門

    使用 Swift 語(yǔ)言編寫(xiě) Android 應(yīng)用入門

    為了能順利使用這份向?qū)?,你需要? 1. 可以編譯Swift源碼的Linux環(huán)境。stdlib目前只能在Linux環(huán)境下編譯成安卓可用版本。在嘗試為安卓構(gòu)建之前,確保你能夠參考Swift項(xiàng)目的README為L(zhǎng)inux做編譯。
    2016-04-04
  • Android逐幀動(dòng)畫(huà)實(shí)現(xiàn)代碼

    Android逐幀動(dòng)畫(huà)實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了Android逐幀動(dòng)畫(huà)實(shí)現(xiàn)代碼,可以通過(guò)xml或java代碼實(shí)現(xiàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-01-01
  • Android編程獲取SD卡路徑及剩余容量的方法

    Android編程獲取SD卡路徑及剩余容量的方法

    這篇文章主要介紹了Android編程獲取SD卡路徑及剩余容量的方法,涉及Android針對(duì)SD卡的狀態(tài)判斷,路徑獲取及容量計(jì)算等相關(guān)技巧,需要的朋友可以參考下
    2016-04-04

最新評(píng)論