当前位置:网站首页>Flutter:剥离StatefulWidget——简化页面开发、跳转以及传值
Flutter:剥离StatefulWidget——简化页面开发、跳转以及传值
2022-06-22 12:02:00 【静水流深zz】
伊始
前几日一位大佬考我,说:
flutter页面开发需要写StatefulWidget和State,Android只需要Activity,
如何简化这种开发方式?
我答曰:
activity还需要写xml呢~
在大佬的静默中,四周浮现起热烈的掌声…
…
深夜,天桥下的我辗转反侧、难以入眠…
‘ 难道,真的可以? ’…
经过反复推敲打磨后,这个‘轮子’还确实有点儿圆,‘滚’起来还挺顺溜。
天桥之下难免简陋,如有不足还请海涵,万望指点~ 嘿嘿
构思
我们一般开发页面A,结构如下:
class A extends StatefulWidget{
}
class AState extends State<A>{
}
同时为了解耦和对路由统一做控制,我们采取的页面跳转方式(即静态路由)是:
Navigator.of(context).pushName('/a')
此种跳转方式的缺点就是页面传值需要map形式,不方便的同时,还容易输错Key,就算使用注解依然无法避免。
反复观察和思考得出了下面这样一张结构图
结构图

对各模块做一下简单介绍
BaseState
abstract class BaseState<T extends StatefulWidget> extends State<T>{}
对page和view通用功能的封装
WidgetState
abstract class WidgetState extends BaseState with WidgetGenerator{}
对自定义view的通用功能封装
PageState
abstract class PageState extends BaseState with WidgetGenerator,RouteAware{}
对自定义Page的通用功能封装
RouteAware对路由观测,你可以埋点或者记录等等
WidgetGenerator
mixin WidgetGenerator on BaseState implements _RouteGenerator,_NavigateActor{}
生成widget并为widget装配功能
_RouteGenerator 生成Route(可带过渡动画)功能
PageRoute<T> buildRoute<T>(Widget page, String routeName, {PageAnimation animation, Object args}) {
....
}
_NavigateActor 路由的各种push和pop操作,你也可以拓展
Future push<T extends PageState>(T page,{PageAnimation animation});
Future pushAndRemoveUntil<T extends PageState>(T page,{PageAnimation animation,RoutePredicate predicate});
Future pushReplacement<T extends Object,TO extends PageState>(TO page, {PageAnimation animation, T result });
void pop<T extends Object>({T result,});
void popUntil({RoutePredicate predicate});
bool canPop();
各模块就介绍完毕了,下面介绍一下使用方法。
如何使用
页面的开发
当我们需要开发页面A时,如下:
clas A extends PageState{
Widget build(BuildContext context){...}
}
就这么简单。
页面传值
当我们想向B页面传一个值/返回一个值到A页面时,如下:
clas B extends PageState{
final var value;
B(this.value);
Widget build(BuildContext context){...}
}
A跳到B
A页面内:push(B('你的值'))
.then((value)=>'B返回值=$value');
B退到A
B页面内:pop(result:'返回给A的值');
pushAndRemoveUntil的使用
如果我从A到B到C,然后C到D页面的时候我想移除B和C,操作如下:
在C页面
push(D(),predicate: (route)=>route.settings.name == '$A')
D页面
当你pop()后,你会发现到了A页面
生成widget
我并不需要跳转,但是需要将我的页面/View生成widget,操作如下:
你的页面/View.generateWidget({Key key}) 就可以生成一个widget了
如果需要key,还可以加上一个
最后
我已经将框架内的Demo置换为现有的开发方式,同时新增加了针对性的Demo并进行了反复的测试,总体来说确实大幅度提升了开发效率,避免了key值出错的问题,另外在替换过程中也没有遇到兼容性问题,
不过这个依然算是初版。
大家可以在下面的框架中使用,看看有啥不足或者bug告诉我,非常感谢。
Bedrock框架
边栏推荐
猜你喜欢

Share 7 free super clear video resource stations! Both editing and collection are practical enough to cry!

Reddit产品主管:为Web3创作者准备的NFT会员实用指南

Vue混写Minxin、虚拟Dom/ref、动态组件、缓存keep
![[cloud native | kubernetes] Introduction to kubernetes (I)](/img/e0/c75e72fc3636b3de8f5caded19a883.png)
[cloud native | kubernetes] Introduction to kubernetes (I)

Redis - 4. Three new data types

【李宏毅】机器学习深度学习笔记 -- 训练模型通关攻略

0007-Reverse Integer(整数反转)

Redis - 3、发布和订阅

Vue mixed write Minxin, virtual dom/ref, dynamic components, cache keep

When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
随机推荐
View the GStreamer plug-in
【云原生 | Kubernetes篇】Kubernetes简介(一)
Handling methods of ogg-01431, 01003 and 01151
ncnn的使用(初学必看)
SQl、Oracle剔除重复记录的语句
仿网易云音乐的滑动冲突处理效果
OpenCV调用usb摄像头出现“select timeout”解决方法
When the tiflash function is pushed down, it must be known that it will become a tiflash contributor in ten minutes
TIS教程02-模型
C#中,数组、List、ArrayList、Directory、LinkedList的区别与性能
磁盘rejecting I/O to offline device故障导致4TB生产数据库无法访问
Overview of SAP marketing cloud functions (II)
Error: unresolved variable $bus and "typeerror: cannot read property '$on' of undefined“
Markov chain, hidden Markov model
关于buildroot编译问题--功能配置了,但未编译进内核
Redis - 9. Persistent AOF (append onlyfile)
TiFlash 函数下推必知必会丨十分钟成为 TiFlash Contributor
The input input box can only input an array between 0 and 100, with two decimal places reserved
[high frequency written test questions] 513 Find the value in the lower left corner of the tree
第1章 自然语言处理和深度学习概述