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

下載本文檔

版權(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)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論