问题 每当我按下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,
textTheme: TextTheme(
title: TextStyle(
color: Colors.white,
),
),
);
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, width: 27, height: 27);
SvgPicture profile1 = new SvgPicture.asset('assets/profile1_icon.svg', color: grey, width: 27, height: 27);
SvgPicture profile2 = new SvgPicture.asset('assets/profile2_icon.svg', color: green, width: 27, height: 27);
SvgPicture notifications1 = new SvgPicture.asset('assets/notifications1_icon.svg', color: grey, width: 27, height: 27);
SvgPicture notifications2 = new SvgPicture.asset('assets/notifications2_icon.svg', color: green, width: 27, height: 27);
SvgPicture settings1 = new SvgPicture.asset('assets/settings1_icon.svg', color:grey, width: 27, height: 27);
SvgPicture settings2 = new SvgPicture.asset('assets/settings2_icon.svg', color: green, width: 27, 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,
color: grey,
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,
width: 35,
height: 35,
child: home1,),
title: Container(),
),
BottomNavigationBarItem(
icon: current_index == 1 ?
Container(
alignment: AlignmentDirectional.bottomCenter,
width: 35,
height: 35,
child: profile2,)
: Container(
alignment: AlignmentDirectional.bottomCenter,
width: 35,
height: 35,
child: profile1,),
title: Container(),
),
BottomNavigationBarItem(
icon: current_index == 2 ?
Container(
alignment: AlignmentDirectional.bottomCenter,
width: 35,
height: 35,
child: notifications2,)
:
Stack(
alignment: AlignmentDirectional.bottomCenter,
children: [
notifications1,
Container(
width: 35,
height: 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,
height: 18,
decoration: BoxDecoration(
color: Colors.red,
shape: BoxShape.circle,
),
child:Text(
notCount.toString(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 10, color: Colors.white),
),
)
),
),
],
),
title: Container(),
),
BottomNavigationBarItem(
icon: current_index == 3 ?
Container(
alignment: AlignmentDirectional.bottomCenter,
width: 35,
height: 35,
child: settings2,):
Container(
alignment: AlignmentDirectional.bottomCenter,
width: 35,
height: 35,
child: settings1,),
title: Container(),
),
],
),
));
}
}
答案 0 :(得分:0)
解决方案:
第二天,我通过将ThemeSwitchingArea小部件放在MainPage.dart中并将ThemeSwitcher小部件保留在SettingsPage.dart中来解决了我的问题