当前位置:网站首页>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
边栏推荐
- 不愧是大佬,字节大牛耗时八个月又一力作
- Debezium系列之:IDEA集成词法和语法分析ANTLR,查看debezium支持的ddl、dml等语句
- Common - Hero Minesweeper
- Android interview, Android audio and video development
- shell编程基础(第9篇:循环)
- 完爆面试官,一线互联网企业高级Android工程师面试题大全
- 大厂面试必备技能,2022Android不死我不倒
- Realizing deep learning framework from zero -- LSTM from theory to practice [practice]
- Webuploader file upload drag upload progress monitoring type control upload result monitoring control
- Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
猜你喜欢

How to apply smart contracts more wisely in 2022?

C application interface development foundation - form control (5) - grouping control

大厂面试必备技能,2022Android不死我不倒

The relationship between temperature measurement and imaging accuracy of ifd-x micro infrared imager (module)

Common interview questions in Android, 2022 golden nine silver ten Android factory interview questions hit

UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
PHP利用ueditor实现上传图片添加水印
![[hard core dry goods] which company is better in data analysis? Choose pandas or SQL](/img/70/a79c4a1724c11e208814de2d9cf553.png)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL

Fuzor 2020 software installation package download and installation tutorial

ELK分布式日志分析系统部署(华为云)
随机推荐
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
PG基础篇--逻辑结构管理(用户及权限管理)
【obs】libobs-winrt :CreateDispatcherQueueController
使用easyexcel模板导出的两个坑(Map空数据列错乱和不支持嵌套对象)
数据库 逻辑处理功能
Which securities company is better and which platform is safer for mobile account opening
太牛了,看这篇足矣了
aggregate
Go语言 | 03 数组、指针、切片用法
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
After the company went bankrupt, the blackstones came
大厂面试必备技能,2022Android不死我不倒
Information / data
IBM has laid off 40 + year-old employees in a large area. Mastering these ten search skills will improve your work efficiency ten times
Debezium系列之:修改源码支持drop foreign key if exists fk
如何实现游戏中的在线计时器和离线计时器
[OBS] qstring's UTF-8 Chinese conversion to blog printing UTF-8 char*
再忙不能忘安全
四万字长文说operator new & operator delete