Flutter 局部路由實(shí)現(xiàn)詳解
Flutter是借鑒React的開發(fā)思想實(shí)現(xiàn)的,在子組件的插槽上,React有this.props.children,Vue有<slot></slot>。
當(dāng)然Flutter也有類似的Widget,那就是Navigator,不過是以router的形式實(shí)現(xiàn)(像<router-view></router-view>???)。
Navigator的使用無非3個屬性
- initialRoute: 初始路由
- onGenerateRoute: 匹配路由
- onUnknownRoute: 404
在實(shí)現(xiàn)層面
首先:Navigator的高度為infinity。如果直接父級非最上級也是infinity會產(chǎn)生異常,例如,Scaffold -> Column -> Navigator。所以:Navigator需要附件限制高度,例如:Scaffold -> Column -> Container(height: 300) -> Navigator
然后:在onGenerateRoute屬性中,使用第一個BuildContext參數(shù),能夠在MaterialApp未設(shè)置route的情況下使用Navigator.pushNamed(nContext, '/efg');跳到對應(yīng)的子路由中。
最后:Navigator執(zhí)行尋找路由順序是 initialRoute -> onGenerateRoute -> onUnknownRoute,這個和React的Route是類似的。
最后附上源碼
import 'package:flutter/material.dart';
class NavigatorPage extends StatefulWidget {
@override
_NavigatorPageState createState() => _NavigatorPageState();
}
class _NavigatorPageState extends State<NavigatorPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Navigator'),
),
body: Column(
children: <Widget>[
Text('Navigator的高度為infinity'),
Text('如果直接父級非最上級也是infinity會產(chǎn)生異常'),
Container(
height: 333,
color: Colors.amber.withAlpha(111),
child: Navigator( // Navigator
initialRoute: '/abc',
onGenerateRoute: (val) {
RoutePageBuilder builder;
switch (val.name) {
case '/abc':
builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Column(
// 并沒有在 MaterialApp 中設(shè)定 /efg 路由
// 因?yàn)镹avigator的特性 使用nContext 可以跳轉(zhuǎn) /efg
children: <Widget>[
Text('呵呵呵'),
RaisedButton(
child: Text('去 /efg'),
onPressed: () {
Navigator.pushNamed(nContext, '/efg');
},
)
],
);
break;
case '/efg':
builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Row(
children: <Widget>[
RaisedButton(
child: Text('去 /hhh'),
onPressed: () {
Navigator.pushNamed(nContext, '/hhh');
},
)
],
);
break;
default:
builder = (BuildContext nContext, Animation<double> animation, Animation<double> secondaryAnimation) => Center(
child: RaisedButton(
child: Text('去 /abc'),
onPressed: () {
Navigator.pushNamed(nContext, '/abc');
},
)
);
}
return PageRouteBuilder(
pageBuilder: builder,
// transitionDuration: const Duration(milliseconds: 0),
);
},
onUnknownRoute: (val) {
print(val);
},
observers: <NavigatorObserver>[]
),
),
Text('Navigator執(zhí)行尋找路由順序'),
Text('initialRoute'),
Text('onGenerateRoute'),
Text('onUnknownRoute'),
],
),
);
}
}
項(xiàng)目地址: https://github.com/zhongmeizhi/fultter-example-app
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android Zipalign工具優(yōu)化Android APK應(yīng)用
本文主要介紹Android Zipalign工具優(yōu)化Android APK應(yīng)用,這里整理了相關(guān)資料及簡單優(yōu)化實(shí)例,有需要的小伙伴可以參考下2016-09-09
Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法
android 讀取json數(shù)據(jù),下面小編給大家整理有關(guān)Android中Json數(shù)據(jù)讀取與創(chuàng)建的方法,需要的朋友可以參考下2015-08-08
解決Error:All flavors must now belong to a named flavor dimens
這篇文章主要介紹了解決Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com,需要的朋友可以參考下2017-11-11
Kotlin Option與Either及Result實(shí)現(xiàn)異常處理詳解
Kotlin異常處理,異常是在程序運(yùn)行時可能發(fā)生的不必要的問題,并突然終止您的程序。異常處理是一個過程,使用它可以防止程序出現(xiàn)可能破壞我們代碼的異常2022-12-12
詳解Android studio如何導(dǎo)入jar包方法
這篇內(nèi)容主要給大家詳細(xì)說明了如何導(dǎo)入jar包,以及Android studio遇到的各種問題和解決辦法。2017-12-12
Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條功能
這篇文章主要介紹了Android 自定義View實(shí)現(xiàn)多節(jié)點(diǎn)進(jìn)度條,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建
這篇文章主要給大家介紹的是Android 架構(gòu)之?dāng)?shù)據(jù)庫框架搭建,在本篇中,將會讓你一點(diǎn)一滴從無到有創(chuàng)建一個不再為數(shù)據(jù)庫而煩惱的框架。需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09

