版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題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件中通過(guò)兩種方式訪問(wèn)共享對(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ì)象通過(guò)函數(shù)的參數(shù)傳遞,而如果函數(shù)中的組件還有下級(jí)組件需要這個(gè)對(duì)象,還需要繼續(xù)往下傳遞。這要是需要修改對(duì)象傳值的方式,需要沿著組件樹逐級(jí)修改,維護(hù)起來(lái)會(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è)子組件無(wú)需再傳遞該對(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. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝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ù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 黑龍江省哈爾濱市德強(qiáng)高級(jí)中學(xué)2025-2026學(xué)年高二(上)期末物理試卷(Ⅱ卷)(含答案)
- 甘肅省武威市2025-2026學(xué)年高三(上)期末物理試卷(含答案)
- 2025~2026學(xué)年山東濟(jì)南市天橋區(qū)八年級(jí)語(yǔ)文第一學(xué)期期末考試試題(含答案)
- 危險(xiǎn)化學(xué)品試題及答案
- 部編人教版六年級(jí)數(shù)學(xué)上冊(cè)期末考試題含答案
- 2022~2023民政行業(yè)職業(yè)鑒定考試題庫(kù)及答案第256期
- 2023年房屋建筑學(xué)考試復(fù)習(xí)題及參考答案
- 2022~2023糧油食品檢驗(yàn)人員考試題庫(kù)及答案解析第101期
- 變頻器應(yīng)用技術(shù)要點(diǎn)
- 三峽新能源考試題及答案
- 數(shù)字孿生方案
- 金融領(lǐng)域人工智能算法應(yīng)用倫理與安全評(píng)規(guī)范
- 機(jī)動(dòng)車駕校安全培訓(xùn)課件
- 2025年役前訓(xùn)練考試題庫(kù)及答案
- 2024VADOD臨床實(shí)踐指南:耳鳴的管理課件
- 2025廣東潮州府城文化旅游投資集團(tuán)有限公司下屬企業(yè)副總經(jīng)理崗位招聘1人筆試歷年備考題庫(kù)附帶答案詳解2套試卷
- 城市軌道交通服務(wù)與管理崗位面試技巧
- 2025年公務(wù)員多省聯(lián)考《申論》題(陜西A卷)及參考答案
- 《允許一切發(fā)生》讀書感悟
- 續(xù)保團(tuán)購(gòu)會(huì)活動(dòng)方案
- 產(chǎn)品設(shè)計(jì)需求與評(píng)審表
評(píng)論
0/150
提交評(píng)論