Flutter Bloc:如何为底部条屏幕和普通屏幕提供一个cubit的实例?
我的应用中有一个导航底栏。 我创建了一个自定义类,用于路由,该类设置所有逻辑,用于导航到应用程序中的所有屏幕,但是底部的bar页面不在此自定义类中。 我有一个问题,为底栏的屏幕提供一个cubit的实例,在自定义类中提供了一个屏幕。 有建议吗?
class UserNavScreen extends StatefulWidget {
static const routeName = 'homepage_screen';
UserNavScreen();
@override
State<UserNavScreen> createState() => _UserNavScreenState();
}
class _UserNavScreenState extends State<UserNavScreen> {
var _index = 2;
final _userPages = [
const UserProfileScreen(),
const UserNotificationScreen(),
BlocProvider<MapCubit>(
create: (context) => MapCubit(
mapRepositery: MapRepositery(
mapProvider: MapProvider(),
),
storesRepositery: StoresRepositery(
storesProvider: StoresProvider(),
),
)..getStoresLocation(),
child: UserMapScreen()),
const UserTopTenScreen(),
const UserHomepageScreen()
];
Widget build(BuildContext context) {
return Scaffold(
body: _userPages[_index],
bottomNavigationBar: _buildNavUser(),
);
}
Widget _buildNavUser() {
return StyleProvider(
style: StyleNavBottomBar(),
child: ConvexAppBar(
style: TabStyle.fixedCircle,
backgroundColor: Colors.white,
color: Colors.grey,
initialActiveIndex: _index,
activeColor: Colors.green,
cornerRadius: 16,
curveSize: 90,
height: 60,
onTap: (index) {
setState(() {
_index = index;
});
},
elevation: 2,
items: const [
TabItem(
icon: Icons.person,
),
TabItem(
icon: Icons.notifications,
),
TabItem(icon: Icons.navigation),
TabItem(
icon: FontAwesomeIcons.medal,
),
TabItem(
icon: Icons.home,
),
],
),
);
}
}
class AppRoute {
Route? onGenerateRoute(RouteSettings routeSettings) {
switch (routeSettings.name) {
case StoreMap.routeName:
final args = routeSettings.arguments as Store;
return MaterialPageRoute(
builder: (context) => StoreMap(store: args),
);
}
}
}
I have a navigation bottom bar in my app.
I created a custom class for routing that sets up all the logic for navigating to all the screens in the app, but the bottom bar pages are not in this custom class.
I have a problem providing a single instance of a cubit for a bottom bar's screen and a screen in the custom class.
Are there any suggestions?
class UserNavScreen extends StatefulWidget {
static const routeName = 'homepage_screen';
UserNavScreen();
@override
State<UserNavScreen> createState() => _UserNavScreenState();
}
class _UserNavScreenState extends State<UserNavScreen> {
var _index = 2;
final _userPages = [
const UserProfileScreen(),
const UserNotificationScreen(),
BlocProvider<MapCubit>(
create: (context) => MapCubit(
mapRepositery: MapRepositery(
mapProvider: MapProvider(),
),
storesRepositery: StoresRepositery(
storesProvider: StoresProvider(),
),
)..getStoresLocation(),
child: UserMapScreen()),
const UserTopTenScreen(),
const UserHomepageScreen()
];
Widget build(BuildContext context) {
return Scaffold(
body: _userPages[_index],
bottomNavigationBar: _buildNavUser(),
);
}
Widget _buildNavUser() {
return StyleProvider(
style: StyleNavBottomBar(),
child: ConvexAppBar(
style: TabStyle.fixedCircle,
backgroundColor: Colors.white,
color: Colors.grey,
initialActiveIndex: _index,
activeColor: Colors.green,
cornerRadius: 16,
curveSize: 90,
height: 60,
onTap: (index) {
setState(() {
_index = index;
});
},
elevation: 2,
items: const [
TabItem(
icon: Icons.person,
),
TabItem(
icon: Icons.notifications,
),
TabItem(icon: Icons.navigation),
TabItem(
icon: FontAwesomeIcons.medal,
),
TabItem(
icon: Icons.home,
),
],
),
);
}
}
class AppRoute {
Route? onGenerateRoute(RouteSettings routeSettings) {
switch (routeSettings.name) {
case StoreMap.routeName:
final args = routeSettings.arguments as Store;
return MaterialPageRoute(
builder: (context) => StoreMap(store: args),
);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我们几乎具有相同的导航设置,我经历的是懒惰地实例化路由类内的肘,然后使用 blocprovider.value.value 将其提供给命名路线,之后提供路由全球使用 repositoryProvider 如下:
现在在 main.dart 中
,我们可以在任何地方访问cubit,尤其是从 usernavscreen ,我们可以在其中进一步提供它到所需的页面:
PS:请注意批准类中的处置方法(我更喜欢将其称为Applouter btw),此方法是在我们确定将永远不会再使用的情况下处置Cubit,在我们的情况下,它是获得的。首先实例化(在usernavscreen内部),这是因为我们有责任关闭由 blocprovider.value.value 提供的cub/bloc。
It seems we have almost identical navigation setup, what I've gone through was to lazily instantiate the cubit inside the routing class, then supply it to the named route using BlocProvider.value, after that provide the routing class globally using RepositoryProvider as follows:
Now in main.dart
Now we can access the cubit anywhere, and specifically from UserNavScreen where we provide it further to the required page:
PS: Notice the dispose method inside the AppRoute class (I prefer calling it AppRouter btw), this method is to dispose the cubit when we're sure it will never be used again, in our case it's where it got first instantiated (inside UserNavScreen), and that is because it's our responsibility to close a cubit/bloc that was provided by BlocProvider.value .