Flutter访问底部导航父级的上下文

如何解决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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?