当前位置:网站首页>Using geTx to build a more elegant structure of flutter pages
Using geTx to build a more elegant structure of flutter pages
2022-06-22 23:52:00 【InfoQ】
Preface
if...elseswitchWidgetif (PersonalController.to.loadingStatus == LoadingStatus.loading) {
return Center(
child: Text(' Loading ...'),
);
}
if (PersonalController.to.loadingStatus == LoadingStatus.failed) {
return Center(
child: Text(' request was aborted '),
);
}
// The normal state
PersonalEntity personalProfile = PersonalController.to.personalProfile;
return Stack(
...
);
StateMixinStateMixin
StateMixinmixinRxStatus.loading(): Loading ;
RxStatus.success(): Loading successful ;
RxStatus.error([String? message]): Loading failed , Can carry an error messagemessage;
RxStatus.empty(): No data .
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: It's actually a variable that carries a state , Functions that return to the normal interface ,NotifierBuilder<T?> Is defined as follows . With this method, you can use state variables to build a normal interface .
typedef NotifierBuilder<T> = Widget Function(T state);
onError: Corresponding to the errorWidgetBuilding functions , Error messages can be usederror.
onLoading: Corresponding to when loadingWidget;
onEmpty: When the data is emptyWidget.
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(' Temporarily no data '),
),
);
}
}
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(' Failed to obtain personal information '));
}
}
}
Controller The construction of
Get.lazyPut<PersonalMixinController>(
() => PersonalMixinController(userId: '70787819648695'),
);
summary
GetXStateMixincontroller.obxRxStatusif...elseswitchGetXcontroller
边栏推荐
- DML:Data Manipulation Language 数据操纵语言
- [arm] it is reported that horizontal display is set for LVDS screen of rk3568 development board
- DCC888 :SSA (static single assignment form)
- 2. interface (calculator)
- Fibonacci sequence set
- Smart data won two annual awards at the second isig China Industrial Intelligence Conference
- Kunlundb query optimization (II) project and filter push down
- PHP7.3报错undefined function simplexml_load_string()
- 【GO】Go Modules入門
- Ecmascript6 new features
猜你喜欢

Kunlun distributed database sequence function and its implementation mechanism

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

OLAP - Druid introduction

flutter外包,承接flutter项目

【首发】请求一下子太多了,数据库危

考过HCIP依然转行失败,职业网工最看重的到底是什么

2022天梯赛-全国总决赛复盘赛

KunlunDB备份和恢复

C language greedy snake

昆仑分布式数据库Sequence功能及其实现机制
随机推荐
OJ daily practice - spanning 2020
Longest word in output string
PHP7.3报错undefined function simplexml_load_string()
冒泡排序 指针
OJ每日一练——单词的长度
【GO】go多态
MySQL-Seconds_behind_master 的精度误差
WebRTC系列-网络传输之4Connection排序
Is it difficult to turn weak current into professional network worker? Huawei pre-sales engineers share their own experience
包管理工具--NPM、--CNPM、 --Yarn、 --CYarn
Ecmascript6 new features
昆仑分布式数据库独特的变量读写功能介绍
Enjoy high-performance computing! Here comes the Tianyi cloud HPC solution
在一条DML语句中插入/更新/删除/获取几百万行数据,你会特别注意什么?
OJ每日一练——跨越2020
Web Caching Technology
OJ daily practice - delete word suffixes
Tp5.1 upload excel file and read its contents
Webrtc series - 4connection sorting of network transmission
#yyds干货盘点# 解决剑指offer:把二叉树打印成多行