当前位置:网站首页>Using repositoryprovider to simplify the value passing of parent-child components
Using repositoryprovider to simplify the value passing of parent-child components
2022-07-05 19:36:00 【InfoQ】
Preface
- Constructors pass values : The parent component passes the objects required by the child component to the child component through the constructor ;
- Singleton object : Building singleton objects , Make the parent and child components use the same object ;
- Containers : Store objects in containers , When the parent and child components are used, they are directly obtained from the container .
RepositoryProviderProviderRepositoryProvider Definition
RepositoryProviderProviderBlocBlocRepositoryProvidercreatevaluecreatevalueRepositoryProviderclass RepositoryProvider<T> extends Provider<T>
with RepositoryProviderSingleChildWidget {
RepositoryProvider({
Key? key,
required Create<T> create,
Widget? child,
bool? lazy,
}) : super(
key: key,
create: create,
dispose: (_, __) {},
child: child,
lazy: lazy,
);
RepositoryProvider.value({
Key? key,
required T value,
Widget? child,
}) : super.value(
key: key,
value: value,
child: child,
);
static T of<T>(BuildContext context, {bool listen = false}) {
try {
return Provider.of<T>(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
''',
);
}
}
}
mixin RepositoryProviderSingleChildWidget on SingleChildWidget {}
MultiRepositoryProviderRepositoryProviderRepositoryProviderProvideroflistenfalse// The way 1
context.read<T>()
// The way 2
RepositoryProvider.of<T>(context)
MultiRepositoryProviderMultiRepositoryProvider(
providers: [
RepositoryProvider<RepositoryA>(
create: (context) => RepositoryA(),
),
RepositoryProvider<RepositoryB>(
create: (context) => RepositoryB(),
),
RepositoryProvider<RepositoryC>(
create: (context) => RepositoryC(),
),
],
child: ChildA(),
)
RepositoryProvider application
- Avatar and background :
_getBannerWithAvatar;
- The personal data :
_getPersonalProfile;
- Personal data statistics :
_getPersonalStatistic.

PersonalEntity personalProfile = personalResponse.personalProfile!;
return Stack(
children: [
CustomScrollView(
slivers: [
_getBannerWithAvatar(context, personalProfile),
_getPersonalProfile(personalProfile),
_getPersonalStatistic(personalProfile),
],
),
// ...
],
);
},
//...
personalProfile
personalProfileRepositoryProvider.value(
child: CustomScrollView(
slivers: [
const BannerWithAvatar(),
const PersonalProfile(),
const PersonalStatistic(),
],
),
value: personalProfile,
),
// ...
valuepersonalProfilepersonalProfilecontext.read<PersonalEntity>()RepositoryProviderpersonalProfileclass 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:
'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=688497718,308119011&fm=26&gp=0.jpg',
height: imageHeight,
width: MediaQuery.of(context).size.width,
fit: BoxFit.fill,
),
),
Positioned(
left: 20,
top: imageHeight - avatarRadius - avatarBorderSize,
child: _getAvatar(
context.read<PersonalEntity>().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,
),
),
]);
}
}
summary
RepositoryProviderRepositoryProviderProviderRepositoryProvidercontextRepositoryProvider.of
边栏推荐
- 司空见惯 - 英雄扫雷鼠
- Worthy of being a boss, byte Daniel spent eight months on another masterpiece
- What do software test engineers do? How about the prospect of treatment?
- 安信证券在网上开户安全吗?
- JAD installation, configuration and integration idea
- HiEngine:可媲美本地的云原生内存数据库引擎
- What is the core value of testing?
- Hiengine: comparable to the local cloud native memory database engine
- 城链科技数字化创新战略峰会圆满召开
- The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)
猜你喜欢

不愧是大佬,字节大牛耗时八个月又一力作
The problem of returning the longtext field in MySQL and its solution

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)

That's awesome. It's enough to read this article

Decision tree and random forest

面试官:Redis中集合数据类型的内部实现方式是什么?

UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术

测试外包公司怎么样?

全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
随机推荐
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
Add data to excel small and medium-sized cases through poi
JAD installation, configuration and integration idea
【硬核干货】数据分析哪家强?选Pandas还是选SQL
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
Tutoriel de téléchargement et d'installation du progiciel fuzor 2020
集合
That's awesome. It's enough to read this article
What are general items
Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
shell编程基础(第9篇:循环)
完爆面试官,一线互联网企业高级Android工程师面试题大全
众昂矿业:2022年全球萤石行业市场供给现状分析
The problem of returning the longtext field in MySQL and its solution
毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
打新债在哪里操作开户是更安全可靠的呢
软件测试工程师是做什么的?待遇前景怎么样?
redis集群模拟消息队列
Android interview, Android audio and video development