Flutter實戰(zhàn)--用Getx組織項目結構

用了兩年的flutter紧帕,有了一些心得盔然,不虛頭巴腦,只求實戰(zhàn)有用是嗜,以供學習或使用flutter的小伙伴參考轻纪,學習尚淺,如有不正確的地方還望各路大神指正叠纷,以免誤人子弟刻帚,在此拜謝~(原創(chuàng)不易,轉發(fā)請標注來源和作者)

注意:無特殊說明涩嚣,flutter版本為3.0+

講完了基礎工具的封裝崇众,那么我們從今天來看下實戰(zhàn)中如何組織項目結構。

一.什么是Getx

兩年多以前航厚,決定使用Flutter對舊項目進行改造時候顷歌,在諸多Flutter框架中(當時有比較流行的Provider狀態(tài)管理框架,咸魚開源Fish Redux和BloC)選擇了尚不成熟的Getx幔睬,當時的Github的star量只有幾百和使用人數(shù)都相對較少(現(xiàn)在Getx的star超過7.3k)眯漩,算是見證了它的成長,可見是金子總會被發(fā)現(xiàn)。

GetX 是 Flutter 上的一個輕量且強大的解決方案:高性能的狀態(tài)管理赦抖、智能的依賴注入和便捷的路由管理舱卡。這些也是它的特色

廢話不多說,我們看下如何在實戰(zhàn)中使用Getx队萤。


二.使用Getx進行路由管理

Getx提供了一個GetMaterialApp轮锥,這個是對MaterialApp的一個封裝,可以便捷的配置項目要尔,我們通常這樣使用

GetMaterialApp(
onInit: () async {
//項目初始化進行配置
},
debugShowCheckedModeBanner: false,
// theme: appThemeData,
locale: const Locale('zh', 'CN'),
fallbackLocale: const Locale('en', 'US'),
// 添加一個回調語言選項舍杜,以備上面指定的語言翻譯不存在
defaultTransition: Transition.rightToLeft,
getPages: Pages.pages,
localizationsDelegates: const [
GlobalMaterialLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
],
supportedLocales: const [Locale('en', 'US'), Locale('zh', 'CN')],
initialRoute: Routes.splashPage,
navigatorObservers: [FlutterSmartDialog.observer],
builder: FlutterSmartDialog.init(
toastBuilder: (String msg) {
return customToastWidget(msg);
},
loadingBuilder: (String msg) {
return customLoadingWidget(msg);
},
),
)

其中有getPages和initialRoute,我么知道一個配置項目所有路由頁面赵辕,一個是項目初始化跳轉的頁面既绩,一般是閃屏頁。

我們知道通常app有非常多的頁面还惠,那么怎么更好的組織頁面呢熬词?我們可以根據(jù)模塊進行頁面劃分。所以我們的Pages可以這樣拆寫

abstract class AppPages {
static final pages = [
...UsersPages.pages,//用戶相關頁面
...OrdersPages.pages,//訂單相關頁面
...GoodsPages.pages,//商品相關頁面
...CustomerPages.pages,//客戶相關頁面
...
];
}

那么在新建的頁面中,例如UserPages中可以這樣寫吸重,那么就把路由拆解開來了

abstract class UsersPages {
static final pages = [
GetPage(
name: Routes.splashPage,
page: () => SplashView(),
binding: SplashBinding(),
),
GetPage(
name: Routes.login,
page: () => LoginView(),
binding: LoginBinding(),
)

}

三.使用Getx Bindings依賴注入

不管是寫web前端互拾,還是Android或者ios,面對復雜的頁面我們都希望頁面(UI)和實現(xiàn)(邏輯)進行分離嚎幸,那么常用的模式有MVC颜矿,MVVM等進行抽離,那么在Flutter中我們可以用同樣的方式組織項目嫉晶。

如上圖骑疆,一個閃屏頁面,我們分為bingdings替废,controllers箍铭,views,widget椎镣。

bingdings:綁定controller诈火,動態(tài)注入

controllers:頁面的邏輯實現(xiàn)

views:頁面的主要UI

widget:頁面用到的組件,頁面比較復雜時候抽離出來的組件


四.使用Getx進行狀態(tài)管理

常用的的是GetBuilder 和ObxValue

1.如果你監(jiān)聽的是一個值的改變状答,那么ObxValue就是一個神器

RxInt count = 0.obs;

當count改變的時候冷守,頁面中使用

Obx(()=>Text(count.value))

就可以直接刷新頁面

2.GetBuilder

GetBuilder<TestController>(
id: 'count',
builder: (ctl) {

return Text(ctl.count);

}

那么只需要在count改變的時候使用

update(['count'])

看源碼我們知道,update的底層的實現(xiàn)原理惊科,就是實現(xiàn)了ValueListenable拍摇,ChangeNotifier,ValueNotifier

/// An object that maintains a list of listeners.
///
/// The listeners are typically used to notify clients that the object has been
/// updated.
///
/// There are two variants of this interface:
///
/// * [ValueListenable], an interface that augments the [Listenable] interface
/// with the concept of a _current value_.
///
/// * [Animation], an interface that augments the [ValueListenable] interface
/// to add the concept of direction (forward or reverse).
///
/// Many classes in the Flutter API use or implement these interfaces. The
/// following subclasses are especially relevant:
///
/// * [ChangeNotifier], which can be subclassed or mixed in to create objects
/// that implement the [Listenable] interface.
///
/// * [ValueNotifier], which implements the [ValueListenable] interface with
/// a mutable value that triggers the notifications when modified.
///
/// The terms "notify clients", "send notifications", "trigger notifications",
/// and "fire notifications" are used interchangeably.
///
/// See also:
///
/// * [AnimatedBuilder], a widget that uses a builder callback to rebuild
/// whenever a given [Listenable] triggers its notifications. This widget is
/// commonly used with [Animation] subclasses, hence its name, but is by no
/// means limited to animations, as it can be used with any [Listenable]. It
/// is a subclass of [AnimatedWidget], which can be used to create widgets
/// that are driven from a [Listenable].
/// * [ValueListenableBuilder], a widget that uses a builder callback to
/// rebuild whenever a [ValueListenable] object triggers its notifications,
/// providing the builder with the value of the object.
/// * [InheritedNotifier], an abstract superclass for widgets that use a
/// [Listenable]'s notifications to trigger rebuilds in descendant widgets
/// that declare a dependency on them, using the [InheritedWidget] mechanism.
/// * [Listenable.merge], which creates a [Listenable] that triggers
/// notifications whenever any of a list of other [Listenable]s trigger their
/// notifications.
abstract class Listenable {


?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末馆截,一起剝皮案震驚了整個濱河市充活,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖混卵,帶你破解...
    沈念sama閱讀 219,039評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件映穗,死亡現(xiàn)場離奇詭異,居然都是意外死亡淮菠,警方通過查閱死者的電腦和手機男公,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,426評論 3 395
  • 文/潘曉璐 我一進店門荤堪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來合陵,“玉大人,你說我怎么就攤上這事澄阳∮抵” “怎么了?”我有些...
    開封第一講書人閱讀 165,417評論 0 356
  • 文/不壞的土叔 我叫張陵碎赢,是天一觀的道長低剔。 經常有香客問我,道長肮塞,這世上最難降的妖魔是什么襟齿? 我笑而不...
    開封第一講書人閱讀 58,868評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮枕赵,結果婚禮上猜欺,老公的妹妹穿的比我還像新娘。我一直安慰自己拷窜,他們只是感情好开皿,可當我...
    茶點故事閱讀 67,892評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著篮昧,像睡著了一般赋荆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上懊昨,一...
    開封第一講書人閱讀 51,692評論 1 305
  • 那天枚尼,我揣著相機與錄音,去河邊找鬼瘪校。 笑死喧半,一個胖子當著我的面吹牛,可吹牛的內容都是我干的材义。 我是一名探鬼主播均抽,決...
    沈念sama閱讀 40,416評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼其掂!你這毒婦竟也來了油挥?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 39,326評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎深寥,沒想到半個月后攘乒,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,782評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡惋鹅,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,957評論 3 337
  • 正文 我和宋清朗相戀三年则酝,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片闰集。...
    茶點故事閱讀 40,102評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡沽讹,死狀恐怖,靈堂內的尸體忽然破棺而出武鲁,到底是詐尸還是另有隱情爽雄,我是刑警寧澤,帶...
    沈念sama閱讀 35,790評論 5 346
  • 正文 年R本政府宣布沐鼠,位于F島的核電站挚瘟,受9級特大地震影響,放射性物質發(fā)生泄漏饲梭。R本人自食惡果不足惜乘盖,卻給世界環(huán)境...
    茶點故事閱讀 41,442評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望憔涉。 院中可真熱鬧订框,春花似錦、人聲如沸监氢。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,996評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽浪腐。三九已至纵揍,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間议街,已是汗流浹背泽谨。 一陣腳步聲響...
    開封第一講書人閱讀 33,113評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留特漩,地道東北人吧雹。 一個月前我還...
    沈念sama閱讀 48,332評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像涂身,于是被迫代替她去往敵國和親雄卷。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,044評論 2 355

推薦閱讀更多精彩內容