Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題_第1頁(yè)
Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題_第2頁(yè)
Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題_第3頁(yè)
Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題_第4頁(yè)
Flutter使用RepositoryProvider解決跨組件傳值問(wèn)題_第5頁(yè)
已閱讀5頁(yè),還剩2頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(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

{}

,注釋上寫(xiě)著其用途是為了方便MultiRepositoryProvider推斷RepositoryProvider的類(lèi)型設(shè)計(jì)??梢钥吹綄?shí)際上RepositoryProvider就是Provider,只是將靜態(tài)方法of的listen參數(shù)默認(rèn)設(shè)置為false了,也就是不監(jiān)聽(tīng)狀態(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ì)象傳值的方式,需要沿著組件樹(shù)逐級(jí)修改,維護(hù)起來(lái)會(huì)很不方便。我們改造一下,將三個(gè)函數(shù)構(gòu)建組件分別換成自定義的Widget,并且將個(gè)人統(tǒng)計(jì)區(qū)換成兩級(jí)組件,改造后的組件樹(shù)如下所示(省略了裝飾類(lèi)的層級(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ì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論