当前位置:网站首页>使用GetX构建更优雅的Flutter页面结构
使用GetX构建更优雅的Flutter页面结构
2022-06-22 21:33:00 【InfoQ】
前言
if...elseswitchWidgetif (PersonalController.to.loadingStatus == LoadingStatus.loading) {
return Center(
child: Text('加载中...'),
);
}
if (PersonalController.to.loadingStatus == LoadingStatus.failed) {
return Center(
child: Text('请求失败'),
);
}
// 正常状态
PersonalEntity personalProfile = PersonalController.to.personalProfile;
return Stack(
...
);
StateMixinStateMixin
StateMixinmixinRxStatus.loading():加载中;
RxStatus.success():加载成功;
RxStatus.error([String? message]):加载失败,可以携带一个错误信息message;
RxStatus.empty():无数据。
class XXXController extends GetxController
with StateMixin<T> {
}
class PersonalMixinController extends GetxController
with StateMixin<PersonalEntity> {
}
StateMixinchangevoid change(T? newState, {RxStatus? status})
newStatestatusWidgetGetView
WidgetcontrollerGetViewGetViewStatelessWidgetcontrollergetabstract class GetView<T> extends StatelessWidget {
const GetView({Key? key}) : super(key: key);
final String? tag = null;
T get controller => GetInstance().find<T>(tag: tag)!;
@override
Widget build(BuildContext context);
}
GetViewcontroller.obxcontroller.obxRxStatusWidget obx(
NotifierBuilder<T?> widget, {
Widget Function(String? error)? onError,
Widget? onLoading,
Widget? onEmpty,
})
- NotifierBuilder<T?> widget:实际就是一个携带状态变量,返回正常状态界面的函数,NotifierBuilder<T?>的定义如下。通过这个方法可以使用状态变量构建正常界面。
typedef NotifierBuilder<T> = Widget Function(T state);
onError:错误时对应的Widget构建函数,可以使用错误信息error。
onLoading:加载时对应的Widget;
onEmpty:数据为空时的Widget。
changeRxStatusif...elseswitchclass PersonalHomePageMixin extends GetView<PersonalMixinController> {
PersonalHomePageMixin({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return controller.obx(
(personalEntity) => _PersonalHomePage(personalProfile: personalEntity!),
onLoading: Center(
child: CircularProgressIndicator(),
),
onError: (error) => Center(
child: Text(error!),
),
onEmpty: Center(
child: Text('暂无数据'),
),
);
}
}
class PersonalMixinController extends GetxController
with StateMixin<PersonalEntity> {
final String userId;
PersonalMixinController({required this.userId});
@override
void onReady() {
getPersonalProfile(userId);
super.onReady();
}
void getPersonalProfile(String userId) async {
change(null, status: RxStatus.loading());
var personalProfile = await JuejinService().getPersonalProfile(userId);
if (personalProfile != null) {
change(personalProfile, status: RxStatus.success());
} else {
change(null, status: RxStatus.error('获取个人信息失败'));
}
}
}
Controller 的构建
Get.lazyPut<PersonalMixinController>(
() => PersonalMixinController(userId: '70787819648695'),
);
总结
GetXStateMixincontroller.obxRxStatusif...elseswitchGetXcontroller
边栏推荐
- os. When the command line parameter in args[1:] is empty, the internal statement will not be executed
- Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution
- 程序员接私活兼职选择
- 弱电转职业网工难不难?华为售前工程师分享亲身经历
- MySQL8.0轻松完成GTID主从复制
- tp5.1解决跨域
- 【ARM】讯为rk3568开发板lvds屏设置横屏显示
- Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience
- js防止PC端复制正确的链接
- Use smart doc to automatically generate interface documents
猜你喜欢

c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试

【GO】Go数组和切片(动态数组)

C language greedy snake

【首发】Redis系列2:数据持久化提高可用性

MySQL-Seconds_behind_master 的精度误差

Safe and reliable! Tianyi cloud data security management platform passed the evaluation

C sqlsugar, hisql, FreeSQL ORM framework all-round performance test vs. sqlserver performance test

Digital data depth | about software self-control, source code left, no code right

MySQL8.0轻松完成GTID主从复制

KunlunDB查询优化(三)排序下推
随机推荐
OJ daily practice - Verifying substring
冒泡排序 指针
Express、路由(Route)、Request对象、Response对象、中间件、EJS模板
WebRTC系列-网络传输之4Connection排序
Es5 object extension methods //call, apply and bind
Unity: use ray to detect objects
【首发】Redis系列2:数据持久化提高可用性
OJ每日一练——过滤多余的空格
【GO】Go Modules入門
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
Is it safe to make an appointment to pay new debts? Is it reliable?
优化——线性规划
xml转义字符对照表
阻止别人使用浏览器调试
OJ daily practice - class dining
os. When the command line parameter in args[1:] is empty, the internal statement will not be executed
js防止PC端复制正确的链接
从类、API、框架三个层面学习设计可复用软件的具体技术学习心得
[go] getting started with go modules
防抖&节流 加强版