




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第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)對(duì)象的變化。我們?cè)谧咏M件中通過兩種方式訪問共享對(duì)象:
//
方式1
context.readT()
//
方式2
RepositoryProvider.ofT(context)
如果有多個(gè)對(duì)象需要共享,可以使用MultiRepositoryProvider,使用方式也和MultiProvider相同:
MultiRepositoryProvider(
providers:
[
RepositoryProviderRepositoryA(
create:
(context)
=
RepositoryA(),
),
RepositoryProviderRepositoryB(
create:
(context)
=
RepositoryB(),
),
RepositoryProviderRepositoryC(
create:
(context)
=
RepositoryC(),
),
child:
ChildA(),
RepositoryProvider應(yīng)用
回顧一下我們之前使用BlocBuilder仿掘金個(gè)人主頁(yè)的代碼,在里面我們頁(yè)面分成了三個(gè)部分:
頭像及背景圖:_getBannerWithAvatar;個(gè)人資料:_getPersonalProfile;個(gè)人數(shù)據(jù)統(tǒng)計(jì):_getPersonalStatistic。
分別使用了三個(gè)構(gòu)建組件的函數(shù)完成。對(duì)應(yīng)的界面如下所示:
PersonalEntity
personalProfile
=
personalResponse.personalProfile!;
return
Stack(
children:
[
CustomScrollView(
slivers:
[
_getBannerWithAvatar(context,
personalProfile),
_getPersonalProfile(personalProfile),
_getPersonalStatistic(personalProfile),
],
),
//
...
],
);
},
//...
可以看到,每個(gè)函數(shù)都需要把personalProfile這個(gè)對(duì)象通過函數(shù)的參數(shù)傳遞,而如果函數(shù)中的組件還有下級(jí)組件需要這個(gè)對(duì)象,還需要繼續(xù)往下傳遞。這要是需要修改對(duì)象傳值的方式,需要沿著組件樹逐級(jí)修改,維護(hù)起來會(huì)很不方便。我們改造一下,將三個(gè)函數(shù)構(gòu)建組件分別換成自定義的Widget,并且將個(gè)人統(tǒng)計(jì)區(qū)換成兩級(jí)組件,改造后的組件樹如下所示(省略了裝飾類的層級(jí))。
組件層級(jí)
拆解完之后,我們就可以簡(jiǎn)化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對(duì)象了,從而使得各個(gè)子組件無需再傳遞該對(duì)象。以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è)代碼更簡(jiǎn)潔也更易于維護(hù)了。
總結(jié)
本篇介紹了RepositoryProvider的使用,實(shí)
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 小學(xué)生體育教育與心理健康
- 心理學(xué)認(rèn)知心理學(xué)試題集萃
- 低空經(jīng)濟(jì)與智能物流技術(shù)的結(jié)合
- DB14-T 3380-2025 露地鮮食葡萄促早熟栽培技術(shù)規(guī)程
- 旅客入住登記表格
- 投資合作協(xié)議及章程書
- 個(gè)人資產(chǎn)狀況統(tǒng)計(jì)表收入、支出3個(gè)
- 推動(dòng)綠色金融與環(huán)保產(chǎn)業(yè)深度融合的實(shí)施路徑
- 食品安全管理標(biāo)準(zhǔn)及操作規(guī)程
- DB6103-T93-2025獼猴桃高溫?zé)岷︻A(yù)警等級(jí)劃分規(guī)范
- 靜脈治療管理課件
- 《Python語(yǔ)言程序設(shè)計(jì)》課程標(biāo)準(zhǔn)
- 磚混廠房改鋼結(jié)構(gòu)施工方案
- 2022年失業(yè)保險(xiǎn)基金績(jī)效評(píng)價(jià)報(bào)告(最終稿)
- 叉車安全程序管理實(shí)施細(xì)則
- 帶編入伍考試題
- 商丘市睢陽(yáng)牧原農(nóng)牧有限公司四場(chǎng)生豬養(yǎng)殖項(xiàng)目環(huán)境影響報(bào)告
- 腸外營(yíng)養(yǎng)靜脈輸注途徑的規(guī)范應(yīng)用
- 《觸不可及》影視鑒賞課件
- 當(dāng)代社會(huì)思潮辨析-課件
- 神經(jīng)外科疾病診療規(guī)范診療指南診療常規(guī)2022版
評(píng)論
0/150
提交評(píng)論