Flutter?GetPageRoute實(shí)現(xiàn)嵌套導(dǎo)航學(xué)習(xí)
1. 嵌套導(dǎo)航-GetPageRoute
本文主要介紹在Getx下快速實(shí)現(xiàn)一個(gè)嵌套導(dǎo)航
嵌套導(dǎo)航顧名思義,我們導(dǎo)航頁面中嵌套一個(gè)獨(dú)立的路由,效果如下
點(diǎn)擊跳轉(zhuǎn)
代碼如下,也是比較簡單
return Scaffold( appBar: AppBar(title: const Text('嵌套導(dǎo)航'),), body: Navigator( key: Get.nestedKey(1), // create a key by index initialRoute: '/', onGenerateRoute: (settings) { if (settings.name == '/') { return GetPageRoute( page: () => Scaffold( appBar: AppBar( title: const Text("首頁"), backgroundColor: Colors.blue ), body: Center( child: ElevatedButton( onPressed: () { Get.toNamed('/second', id:1); // navigate by your nested route by index }, child: const Text("跳轉(zhuǎn)下一頁"), ), ), ), ); } else if (settings.name == '/second') { return GetPageRoute( page: () => Center( child: Scaffold( appBar: AppBar( title: const Text("第二頁"),backgroundColor: Colors.blue ), body: const Center( child: Text("第二頁") ), ), ), ); } } ), );
通過Navigator
這個(gè)widget把我們的路由放入新的導(dǎo)航中,通過onGenerateRoute
來區(qū)分頁面的路由跳轉(zhuǎn),key使用的是Get.nestedKey(1)
來區(qū)分具體頁面。GetPageRoute
創(chuàng)建路由頁面
2. 自定義拓展
我們也可以添加占位圖,用于存放一些廣告頁
Column( children: [ Container( color: Colors.amberAccent, height: 100, child: const Center(child: Text('我是輪播圖')), ), Expanded( child: Navigator())]
這里使用Column
進(jìn)行包裹,Expanded
撐開下部分。
3. 使用bottomNavigationBar
class NestedNavigatePage extends StatelessWidget { const NestedNavigatePage({Key? key}) : super(key: key); @override Widget build(BuildContext context) { final logic = Get.find<NestedNavigateLogic>(); final state = Get.find<NestedNavigateLogic>().state; return Scaffold( appBar: AppBar(title: const Text('嵌套導(dǎo)航'),), body: Column( children: [ Container( color: Colors.amberAccent, height: 100, child: const Center(child: Text('我是輪播圖')), ), Expanded( child: Navigator( key: Get.nestedKey(1), // create a key by index initialRoute: '/home', onGenerateRoute: logic.onGenerateRoute, ), ), ], ), bottomNavigationBar:Obx(() => BottomNavigationBar( items: const [ BottomNavigationBarItem(icon: Icon(Icons.home),label: '首頁'), BottomNavigationBarItem(icon: Icon(Icons.list),label: '列表'), BottomNavigationBarItem(icon: Icon(Icons.people),label:'我的'), ], currentIndex: state.selectIndex.value, onTap: logic.selectTabBarIndex, selectedItemColor: Colors.red, )), ); } }
state
中定義數(shù)據(jù)
class NestedNavigateState { var selectIndex = 0.obs; List<String> pages = ['/home','/list','/mine']; NestedNavigateState() { ///Initialize variables } }
logic
中實(shí)現(xiàn)邏輯
import 'package:flutter/material.dart'; import 'package:get/get.dart'; import 'nested_navigate_state.dart'; class NestedNavigateLogic extends GetxController { final NestedNavigateState state = NestedNavigateState(); selectTabBarIndex( int index){ state.selectIndex.value = index; Get.toNamed(state.pages[index],id: 1); } Route? onGenerateRoute(RouteSettings settings) { return GetPageRoute( settings: settings, page: () => page(settings.name!), transition: Transition.leftToRightWithFade, ); } Widget page(String title) { return Center( child: Scaffold( // appBar: AppBar( // title: Text(title), backgroundColor: Colors.blue // ), body: Center( child: Text(title) ) )); } }
點(diǎn)擊通過obx
自動(dòng)響應(yīng)
4.小結(jié)
我們通過GetPageRoute
可以進(jìn)行導(dǎo)航嵌套,方便我們實(shí)現(xiàn)一些特定需求。同時(shí)我們可以配合bottomNavigationBar
實(shí)現(xiàn)tabbr效果。 創(chuàng)建平行導(dǎo)航堆棧可能是危險(xiǎn)的。
理想的情況是不要使用NestedNavigators
,或者盡量少用。如果你的項(xiàng)目需要它,請(qǐng)繼續(xù),但請(qǐng)記住,在內(nèi)存中保持多個(gè)導(dǎo)航堆??赡懿皇且粋€(gè)好主意。
以上就是Flutter GetPageRoute實(shí)現(xiàn)嵌套導(dǎo)航學(xué)習(xí)的詳細(xì)內(nèi)容,更多關(guān)于Flutter GetPageRoute嵌套導(dǎo)航的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互
有了JSPatch,我們便可以在iOS App開發(fā)中令JavaScript代碼調(diào)用原生的Objective-C屬性和方法等,下面就來詳細(xì)看一下如何在iOS中使用JSPatch框架使Objective-C與JavaScript代碼交互2016-06-06iOS支付寶、微信、銀聯(lián)支付集成封裝調(diào)用(下)
本篇文章通過代碼實(shí)例給大家講述了iOS支付寶、微信、銀聯(lián)支付集成封裝調(diào)用,對(duì)此有需要的朋友可以測(cè)試參考下。2018-04-04iOS用UITextField切換明文/密文顯示時(shí)末尾空白的問題解決
最近在工作中遇到一個(gè)問題,利用UITextField切換明文/密文顯示時(shí)發(fā)現(xiàn)字符串后面會(huì)出現(xiàn)一段空白,所以下面這篇文章主要給大家介紹了iOS用UITextField切換明文/密文顯示時(shí)末尾空白問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-05-05個(gè)人對(duì)于異步和多線程的關(guān)系的理解分享
異步和多線程并不是一個(gè)同等關(guān)系,異步是最終目的,多線程只是我們實(shí)現(xiàn)異步的一種手段。異步是當(dāng)一個(gè)調(diào)用請(qǐng)求發(fā)送給被調(diào)用者,而調(diào)用者不用等待其結(jié)果的返回而可以做其它的事情。2014-08-08iOS開發(fā)中UIWebView的加載本地?cái)?shù)據(jù)的三種方式
這篇文章主要介紹了iOS開發(fā)中UIWebView的加載本地?cái)?shù)據(jù)的三種方式,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件
這篇文章主要介紹了iOS開發(fā)中使用Quartz2D繪圖及自定義UIImageView控件的方法,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2015-11-11iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫詳解
這篇文章主要給大家介紹了關(guān)于iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12