




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
第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本身是一個(gè)空的Mixin:
mixin
RepositoryProviderSingleChildWidget
on
SingleChildWidget
{}
,注釋上寫著其用途是為了方便MultiRepositoryProvider推斷RepositoryProvider的類型設(shè)計(jì)??梢钥吹綄?shí)際上RepositoryProvider就是Provider,只是將靜態(tài)方法of的listen參數(shù)默認(rèn)設(shè)置為false了,也就是不監(jiān)聽狀態(tài)對象的變化。我們在子組件中通過兩種方式訪問共享對象:
//
方式1
context.readT()
//
方式2
RepositoryProvider.ofT(context)
如果有多個(gè)對象需要共享,可以使用MultiRepositoryProvider,使用方式也和MultiProvider相同:
MultiRepositoryProvider(
providers:
[
RepositoryProviderRepositoryA(
create:
(context)
=
RepositoryA(),
),
RepositoryProviderRepositoryB(
create:
(context)
=
RepositoryB(),
),
RepositoryProviderRepositoryC(
create:
(context)
=
RepositoryC(),
),
child:
ChildA(),
RepositoryProvider應(yīng)用
回顧一下我們之前使用BlocBuilder仿掘金個(gè)人主頁的代碼,在里面我們頁面分成了三個(gè)部分:
頭像及背景圖:_getBannerWithAvatar;個(gè)人資料:_getPersonalProfile;個(gè)人數(shù)據(jù)統(tǒng)計(jì):_getPersonalStatistic。
分別使用了三個(gè)構(gòu)建組件的函數(shù)完成。對應(yīng)的界面如下所示:
PersonalEntity
personalProfile
=
personalResponse.personalProfile!;
return
Stack(
children:
[
CustomScrollView(
slivers:
[
_getBannerWithAvatar(context,
personalProfile),
_getPersonalProfile(personalProfile),
_getPersonalStatistic(personalProfile),
],
),
//
...
],
);
},
//...
可以看到,每個(gè)函數(shù)都需要把personalProfile這個(gè)對象通過函數(shù)的參數(shù)傳遞,而如果函數(shù)中的組件還有下級組件需要這個(gè)對象,還需要繼續(xù)往下傳遞。這要是需要修改對象傳值的方式,需要沿著組件樹逐級修改,維護(hù)起來會很不方便。我們改造一下,將三個(gè)函數(shù)構(gòu)建組件分別換成自定義的Widget,并且將個(gè)人統(tǒng)計(jì)區(qū)換成兩級組件,改造后的組件樹如下所示(省略了裝飾類的層級)。
組件層級
拆解完之后,我們就可以簡化personalProfile的傳值了。
RepositoryProvider.value(
child:
CustomScrollView(
slivers:
[
const
BannerWithAvatar(),
const
PersonalProfile(),
const
PersonalStatistic(),
],
value:
personalProfile,
//
...
這里使用value模式是因?yàn)閜ersonalProfile已經(jīng)被創(chuàng)建了。然后在需要使用personalProfile的地方,使用context.readPersonalEntity()就可以從RepositoryProvider中取出personalProfile對象了,從而使得各個(gè)子組件無需再傳遞該對象。以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,
),
),
]);
可以看到整個(gè)代碼更簡潔也更易于維護(hù)了。
總結(jié)
本篇介紹了RepositoryProvider的使用,實(shí)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 醫(yī)院信息系統(tǒng)的安全審計(jì)與監(jiān)控策略
- 醫(yī)保政策下的醫(yī)療信息化建設(shè)
- 醫(yī)療人才隊(duì)伍的國際化培養(yǎng)路徑
- 醫(yī)療信息管理系統(tǒng)的架構(gòu)設(shè)計(jì)與優(yōu)化策略
- 醫(yī)療信息化建設(shè)中的網(wǎng)絡(luò)安全防護(hù)
- 醫(yī)療設(shè)備安全監(jiān)管的培訓(xùn)課程設(shè)計(jì)
- 現(xiàn)場管理培訓(xùn)心得體會模版
- 新質(zhì)生產(chǎn)力表述
- 代理拓客合同范例
- 倉庫地面改造合同范例
- 《尹定邦設(shè)計(jì)學(xué)概論》試題及答案
- 堅(jiān)持以人民為中心發(fā)展思想
- 球形網(wǎng)架屋面板安裝專項(xiàng)施工方案
- 三新背景下高中化學(xué)高效課堂構(gòu)建策略研究
- 新高考高中物理競賽專題1力學(xué)50題競賽真題強(qiáng)化訓(xùn)練解析版
- GB/T 6184-20001型全金屬六角鎖緊螺母
- 消毒供應(yīng)室??评碚摽荚囶}庫(單選、多選共500題)
- 行政執(zhí)法講座課件
- DB32T 2197-2022 水文自動測報(bào)系統(tǒng)數(shù)據(jù)傳輸規(guī)約(修訂)
- 心肺交互作用-
- 幼兒園中班課件:《預(yù)防感冒》
評論
0/150
提交評論