




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
第Flutter使用RepositoryProvider解決跨組件傳值問題return
Provider.ofT(context,
listen:
listen);
}
on
ProviderNotFoundException
catch
(e)
{
if
(e.valueType
!=
T)
rethrow;
throw
FlutterError(
'''
RepositoryProvider.of()
called
with
a
context
that
does
not
contain
a
repository
of
type
$T.
No
ancestor
could
be
found
starting
from
the
context
that
was
passed
to
RepositoryProvider.of$T().
This
can
happen
if
the
context
you
used
comes
from
a
widget
above
the
RepositoryProvider.
The
context
used
was:
$context
''',
);
}
RepositoryProviderSingleChildWidget本身是一個空的Mixin:
mixin
RepositoryProviderSingleChildWidget
on
SingleChildWidget
{}
,注釋上寫著其用途是為了方便MultiRepositoryProvider推斷RepositoryProvider的類型設計??梢钥吹綄嶋H上RepositoryProvider就是Provider,只是將靜態(tài)方法of的listen參數默認設置為false了,也就是不監(jiān)聽狀態(tài)對象的變化。我們在子組件中通過兩種方式訪問共享對象:
//
方式1
context.readT()
//
方式2
RepositoryProvider.ofT(context)
如果有多個對象需要共享,可以使用MultiRepositoryProvider,使用方式也和MultiProvider相同:
MultiRepositoryProvider(
providers:
[
RepositoryProviderRepositoryA(
create:
(context)
=
RepositoryA(),
),
RepositoryProviderRepositoryB(
create:
(context)
=
RepositoryB(),
),
RepositoryProviderRepositoryC(
create:
(context)
=
RepositoryC(),
),
child:
ChildA(),
RepositoryProvider應用
回顧一下我們之前使用BlocBuilder仿掘金個人主頁的代碼,在里面我們頁面分成了三個部分:
頭像及背景圖:_getBannerWithAvatar;個人資料:_getPersonalProfile;個人數據統(tǒng)計:_getPersonalStatistic。
分別使用了三個構建組件的函數完成。對應的界面如下所示:
PersonalEntity
personalProfile
=
personalResponse.personalProfile!;
return
Stack(
children:
[
CustomScrollView(
slivers:
[
_getBannerWithAvatar(context,
personalProfile),
_getPersonalProfile(personalProfile),
_getPersonalStatistic(personalProfile),
],
),
//
...
],
);
},
//...
可以看到,每個函數都需要把personalProfile這個對象通過函數的參數傳遞,而如果函數中的組件還有下級組件需要這個對象,還需要繼續(xù)往下傳遞。這要是需要修改對象傳值的方式,需要沿著組件樹逐級修改,維護起來會很不方便。我們改造一下,將三個函數構建組件分別換成自定義的Widget,并且將個人統(tǒng)計區(qū)換成兩級組件,改造后的組件樹如下所示(省略了裝飾類的層級)。
組件層級
拆解完之后,我們就可以簡化personalProfile的傳值了。
RepositoryProvider.value(
child:
CustomScrollView(
slivers:
[
const
BannerWithAvatar(),
const
PersonalProfile(),
const
PersonalStatistic(),
],
value:
personalProfile,
//
...
這里使用value模式是因為personalProfile已經被創(chuàng)建了。然后在需要使用personalProfile的地方,使用context.readPersonalEntity()就可以從RepositoryProvider中取出personalProfile對象了,從而使得各個子組件無需再傳遞該對象。以BannerWithAvatar為例,如下所示:
class
BannerWithAvatar
extends
StatelessWidget
{
final
double
bannerHeight
=
230;
final
double
imageHeight
=
180;
final
double
avatarRadius
=
45;
final
double
avatarBorderSize
=
4;
const
BannerWithAvatar({Key
key})
:
super(key:
key);
@override
Widget
build(BuildContext
context)
{
return
SliverToBoxAdapter(
child:
Container(
height:
bannerHeight,
color:
Colors.white70,
alignment:
Alignment.topLeft,
child:
Stack(
children:
[
Container(
height:
bannerHeight,
),
Positioned(
top:
0,
left:
0,
child:
CachedNetworkImage(
imageUrl:
'/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=688497718,308119011fm=26gp=0.jpg',
height:
imageHeight,
width:
MediaQuery.of(context).size.width,
fit:
BoxFit.fill,
),
),
Positioned(
left:
20,
top:
imageHeight
-
avatarRadius
-
avatarBorderSize,
child:
_getAvatar(
context.readPersonalEntity().avatar,
avatarRadius
*
2,
avatarBorderSize,
),
),
],
),
),
);
Widget
_getAvatar(String
avatarUrl,
double
size,
double
borderSize)
{
return
Stack(alignment:
Alignment.center,
children:
[
Container(
width:
size
+
borderSize
*
2,
height:
size
+
borderSize
*
2,
clipBehavior:
Clip.antiAlias,
decoration:
BoxDecoration(
color:
Colors.white,
borderRadius:
BorderRadius.circular(size
/
2
+
borderSize),
),
),
Container(
width:
size,
height:
size,
clipBehavior:
Clip.antiAlias,
decoration:
BoxDecoration(
color:
Colors.black,
borderRadius:
BorderRadius.circular(size
/
2),
),
child:
CachedNetworkImage(
imageUrl:
avatarUrl,
height:
size,
width:
size,
fit:
BoxFit.fill,
),
),
]);
可以看到整個代碼更簡潔也更易于維護了。
總結
本篇介紹了RepositoryProvider的使用,實
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 東城風淋室施工方案
- 脈絡膜水腫的健康宣教
- 機場彩涂板采購方案
- 高處作業(yè)安全施工方案
- 校服-生產實施方案
- 商業(yè)園區(qū)招租方案
- 物業(yè)規(guī)車場管理方案
- 社區(qū)項目落地評估方案
- 有效醫(yī)院美容方案
- 復式樓梯整改方案
- 港口裝卸作業(yè)培訓
- 2025年湖北省武漢市中考數學真題(無答案)
- 鉗工考試試題及答案
- 呼倫貝爾農墾集團有限公司招聘筆試題庫2025
- 醫(yī)院檢驗科實驗室生物安全程序文件SOP
- QC降低礦山法圍巖隧道爆破超挖量
- 2023年5月FDA口服速釋制劑根據BCS分類系統(tǒng)的生物利用度與生物等效性研究及生物等效性豁免
- 校園文化建設方案(共60張PPT)
- 藍色海洋經濟海事航海漁業(yè)水產養(yǎng)殖港口碼頭海運PPT模板
- 不飽和聚酯樹脂化學品安全技術說明書MSDS
- 機動車排放檢驗比對試驗報告
評論
0/150
提交評論