当前位置:网站首页>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
边栏推荐
- 14、用户、组和权限(14)
- 全网最全的低代码/无代码平台盘点:简道云、伙伴云、明道云、轻流、速融云、集简云、Treelab、钉钉·宜搭、腾讯云·微搭、智能云·爱速搭、百数云
- 如何实现游戏中的在线计时器和离线计时器
- 建议收藏,我的腾讯Android面试经历分享
- 毫米波雷达人体感应器,智能感知静止存在,人体存在检测应用
- [hard core dry goods] which company is better in data analysis? Choose pandas or SQL
- Android面试,android音视频开发
- Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
- The city chain technology Digital Innovation Strategy Summit was successfully held
- 成功入职百度月薪35K,2022Android开发面试解答
猜你喜欢

Postman核心功能解析-参数化和测试报告

建议收藏,我的腾讯Android面试经历分享

集合

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

40000 word Wenshuo operator new & operator delete

14、用户、组和权限(14)

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

Two pits exported using easyexcel template (map empty data columns are disordered and nested objects are not supported)

Fuzor 2020 software installation package download and installation tutorial

aggregate
随机推荐
Django使用mysqlclient服务连接并写入数据库的操作过程
PG基础篇--逻辑结构管理(用户及权限管理)
[hard core dry goods] which company is better in data analysis? Choose pandas or SQL
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
How MySQL queries and modifies JSON data
Explain in detail the functions and underlying implementation logic of the groups sets statement in SQL
Decision tree and random forest
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
What does software testing do? What are the requirements for learning?
IBM大面积辞退40岁+的员工,掌握这十个搜索技巧让你的工作效率至上提高十倍
C application interface development foundation - form control (6) - menu bar, toolbar and status bar controls
40000 word Wenshuo operator new & operator delete
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
Android面试,android音视频开发
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
Debezium系列之:修改源码支持drop foreign key if exists fk
word如何转换成pdf?word转pdf简单的方法分享!
Common - Hero Minesweeper
PHP uses ueditor to upload pictures and add watermarks
Fundamentals of shell programming (Chapter 9: loop)