Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 在Flatter中访问其子级中的父类变量?_Flutter_Dart_Scope_Widget - Fatal编程技术网

Flutter 在Flatter中访问其子级中的父类变量?

Flutter 在Flatter中访问其子级中的父类变量?,flutter,dart,scope,widget,Flutter,Dart,Scope,Widget,我正在尝试使用一个定制的statefull页面包装小部件来包装我的所有页面。其想法是让它返回一个Scaffold并使用相同的菜单抽屉和底部导航栏,并调用适当的页面作为页面参数 我的底部导航栏工作正常,我正在设置正确的selectedIndex,但我找不到在子页面(即另一个文件)中访问它的方法,因为我不知道如何访问父级的selectedIndex并显示页面列表中相应的小部件 import 'package:flutter/material.dart'; class PageWrapper ext

我正在尝试使用一个定制的statefull页面包装小部件来包装我的所有页面。其想法是让它返回一个Scaffold并使用相同的菜单抽屉和底部导航栏,并调用适当的页面作为页面参数

我的底部导航栏工作正常,我正在设置正确的selectedIndex,但我找不到在子页面(即另一个文件)中访问它的方法,因为我不知道如何访问父级的selectedIndex并显示页面列表中相应的小部件

import 'package:flutter/material.dart';

class PageWrapper extends StatefulWidget {

  final Widget page;
  final AppBar appBar;
  final BottomNavigationBar bottomNav;
  final Color bckColor;

  PageWrapper({@required this.page, this.appBar, this.bckColor, this.bottomNav});

  @override
  _PageWrapperState createState() => _PageWrapperState();
}

class _PageWrapperState extends State<PageWrapper> {

  int _selectedIndex;

  void _onItemTapped(int index) {
    setState(() {
      _selectedIndex = index;
    });
  }

  @override
  void initState() {
    super.initState();
    _selectedIndex = 0;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: widget.appBar,
        backgroundColor: widget.bckColor,
        bottomNavigationBar: CustomBottomNavigation(selectedIndex: _selectedIndex, onItemTapped: _onItemTapped),
        body: widget.page,
        drawer: Drawer(...),
    );
  }
}

我想在我的主屏幕和RoomService屏幕中访问底部导航栏的当前索引。有办法吗?

您可以通过使用诸如
Provider
Bloc
之类的状态管理工具来解决这个问题。为了让事情变得简单,让我们用它来做吧

在main.dart中使用
ChangeNotifierProvider
包装
MaterialApp

return MultiProvider(
  providers: [
    ChangeNotifierProvider<IndexModel>(
        create: (context) => IndexModel()),
  ],
  child: MaterialApp(...)
);
以下是如何根据数据索引显示数据(理想情况下,您应该使用选择器而不是使用者,以便小部件仅在其侦听的值更改时重建):

@覆盖
小部件构建(构建上下文){
//其他小部件
选择器(
选择器:(u,model)=>model.index,
建筑商:(uu,i,uu){
开关(一){
//你是根据索引返回这里的吗
}
},
);
}
)
}
附加说明。以下是如何在UI中访问ImageModel的值:

final model=Provider.of<IndexModel>(context,listen:false);
int index =model.index; //get index value
model.index=index; //set your index value
final model=Provider.of(上下文,listen:false);
int index=model.index//获取索引值
模型指数=指数//设置索引值

当您不侦听更改时,必须通过
listen:false
。当您在
initState
onPressed
中访问它时,这是必需的,我一直在试图避免提供程序,但它都能完美地工作!我很高兴这有帮助。祝你的项目好运。
class IndexModel extends ChangeNotifier {
  int _index;
  get index => _index;
  set index(int index) {
    _index = index;
    notifyListeners(); //Notifies its listeners that the value has changed
  }
}
@override
Widget build(BuildContext context) {
 //other widgets
 Selector<IndexModel, String>(
  selector: (_, model) => model.index,
  builder: (_, i, __) {
   switch(i){
     //do your returning here based on the index
      }
     },
   );
  }
 )
}
final model=Provider.of<IndexModel>(context,listen:false);
int index =model.index; //get index value
model.index=index; //set your index value