如何解决Flutter访问底部导航父级的上下文
问题 每当我按下ThemeSwitcher内的复选框时,就会发生类似这样的疯狂事情: enter image description here
我正在尝试获取MainPage.dart上下文并将其传递给SettingsPage.dart中名为ThemeSwitcher的小部件
我正在使用此插件:https://pub.dev/packages/animated_theme_switcher
1-有main.dart类,其中包含整个应用程序的ThemeProvider
2- MainPage.dart类,用于保存4页之间的导航
3-包含一个ThemeSwitchingAreawidget和ThemeSwitcher小部件的SettingsPage.dart
main.dart
library(data.table)
setDT(x) #converting x to data.table
x[,.(Time,value = cells_alive / cells_alive[which(Time == 0)]),treatment]
#output
treatment Time value
1: 1 0 1.000000000
2: 1 30 0.940000000
3: 1 60 0.200000000
4: 1 180 0.040000000
5: 2 0 1.000000000
6: 2 30 0.651260504
7: 2 60 0.207983193
8: 2 180 0.004201681
MainPage.dart
import 'package:Flutter/material.dart';
import 'InitPage.dart';
import 'package:animated_theme_switcher/animated_theme_switcher.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
MyAppState createState() => MyAppState();
}
class MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
final isPlatformDark =
WidgetsBinding.instance.window.platformBrightness == Brightness.dark;
final initTheme = isPlatformDark ? darkTheme : lightTheme;
return ThemeProvider(
initTheme: initTheme,child: Builder(builder: (context) {
return MaterialApp(
title: 'Flutter Demo',theme: ThemeProvider.of(context),home: InitPage(),);
}),);
}
}
final lightTheme = ThemeData(
brightness: Brightness.light,primaryColor: Colors.white,accentColor: Colors.greenAccent,bottomAppBarColor: Colors.greenAccent,hintColor: Colors.yellowAccent,textTheme: TextTheme(
title: TextStyle(
color: Colors.white,),);
final darkTheme = ThemeData(
brightness: Brightness.dark,primaryColor: Colors.black,accentColor: Colors.blueAccent,hintColor: Colors.deepOrangeAccent,bottomAppBarColor: Colors.grey,);
SettingsPage.dart
import 'package:Fekra/constants.dart';
import 'package:move_to_background/move_to_background.dart';
import 'package:Flutter/material.dart';
import 'package:Flutter_svg/Flutter_svg.dart';
import 'HomePage.dart';
import 'ProfilePage.dart';
import 'NotificationsPage.dart';
import 'SettingsPage.dart';
class MainPage extends StatefulWidget {
@override
MainPageState createState() => new MainPageState();
}
class MainPageState extends State<MainPage>
with SingleTickerProviderStateMixin {
int current_index = 0;
int notCount = 6;
SvgPicture home1 = new SvgPicture.asset('assets/home1_icon.svg',color:grey,width: 27,height: 27);
SvgPicture home2 = new SvgPicture.asset('assets/home2_icon.svg',color: green,height: 27);
SvgPicture profile1 = new SvgPicture.asset('assets/profile1_icon.svg',color: grey,height: 27);
SvgPicture profile2 = new SvgPicture.asset('assets/profile2_icon.svg',height: 27);
SvgPicture notifications1 = new SvgPicture.asset('assets/notifications1_icon.svg',height: 27);
SvgPicture notifications2 = new SvgPicture.asset('assets/notifications2_icon.svg',height: 27);
SvgPicture settings1 = new SvgPicture.asset('assets/settings1_icon.svg',height: 27);
SvgPicture settings2 = new SvgPicture.asset('assets/settings2_icon.svg',height: 27);
final List<Widget> children = [
HomePage(),ProfilePage(),NotificationsPage(),SettingsPage(),];
TabController tabController;
@override
void initState() {
super.initState();
tabController = TabController(vsync: this,length: children.length);
}
void dispose() {
tabController.dispose();
super.dispose();
}
Future<bool> onBack() {
bool value = false;
setState(() {
if (current_index != 0) {
current_index = 0;
value = false;
} else {
MovetoBackground.moveTaskToBack();
}
});
return Future<bool>.value(value);
}
onTapped(int index) {
setState(() {
current_index = index;
if(current_index == 2) {
notCount = 0;
}
});
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: onBack,child: new Scaffold(
body: Column(children: <Widget>[
Expanded(child:
IndexedStack(
index: current_index,children: children,)),Divider(
thickness: 0.3,indent: 0,endindent: 0,height: 0),]),bottomNavigationBar: BottomNavigationBar(
onTap: onTapped,backgroundColor: Colors.white,elevation: 0,currentIndex: current_index,type: BottomNavigationBarType.fixed,items: [
BottomNavigationBarItem(
icon: current_index == 0 ?
Container(
alignment: AlignmentDirectional.bottomCenter,width: 35,height: 35,child: home2,)
: Container(
alignment: AlignmentDirectional.bottomCenter,child: home1,title: Container(),BottomNavigationBarItem(
icon: current_index == 1 ?
Container(
alignment: AlignmentDirectional.bottomCenter,child: profile2,child: profile1,BottomNavigationBarItem(
icon: current_index == 2 ?
Container(
alignment: AlignmentDirectional.bottomCenter,child: notifications2,)
:
Stack(
alignment: AlignmentDirectional.bottomCenter,children: [
notifications1,Container(
width: 35,alignment: Alignment.topRight,child: notCount == 0 ? null : Container(
width: 18,height: 18,alignment: AlignmentDirectional.center,decoration: Boxdecoration(
color: Colors.transparent,shape: BoxShape.circle,border: Border.all(color: Colors.white,width: 2),child: Container(alignment:AlignmentDirectional.center,width: 18,decoration: Boxdecoration(
color: Colors.red,child:Text(
notCount.toString(),textAlign: TextAlign.center,style: TextStyle(fontSize: 10,color: Colors.white),)
),],BottomNavigationBarItem(
icon: current_index == 3 ?
Container(
alignment: AlignmentDirectional.bottomCenter,child: settings2,):
Container(
alignment: AlignmentDirectional.bottomCenter,child: settings1,));
}
}
解决方法
解决方案:
第二天,我通过将ThemeSwitchingArea小部件放在MainPage.dart中并将ThemeSwitcher小部件保留在SettingsPage.dart中来解决了我的问题
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。