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
Dart 在单独的bottomNotificationBar类中使用setState返回到主类_Dart_Flutter - Fatal编程技术网

Dart 在单独的bottomNotificationBar类中使用setState返回到主类

Dart 在单独的bottomNotificationBar类中使用setState返回到主类,dart,flutter,Dart,Flutter,如果我将bottomNotificationBar与页面的其余部分保持在同一个类中,则setState工作正常,按钮工作正常 如果我将bottomNotificationBar移动到另一个类,我将无法使setState工作,因为它需要引用回主类。我试过一些方法,但我还不能完全理解这一点 错误是: 处理手势时引发了以下断言: 构造函数中调用的setState(): 不工作的零件标记在本页底部附近: import 'package:flutter/material.dart'; v

如果我将bottomNotificationBar与页面的其余部分保持在同一个类中,则setState工作正常,按钮工作正常

如果我将bottomNotificationBar移动到另一个类,我将无法使setState工作,因为它需要引用回主类。我试过一些方法,但我还不能完全理解这一点

错误是: 处理手势时引发了以下断言: 构造函数中调用的setState():

不工作的零件标记在本页底部附近:

    import 'package:flutter/material.dart';

    void main() {
      runApp(new MyApp());
    }

    class MyApp extends StatelessWidget {
      // This widget is the root of your application.
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          title: 'My Title',
          home: new MyHomePage(),
        );
      }
    }


    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => new _MyHomePageState();
    }


    class _MyHomePageState extends State<MyHomePage> {
      var selectedPageIndex = 0;
      var pages = [ Page1(), Page2(), ];

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          body: pages[selectedPageIndex],
          bottomNavigationBar:
              MyClass().buildBottomNavigationBar(selectedPageIndex),
        );
      }
    }

    class MyClass {
      BottomNavigationBar buildBottomNavigationBar(selectedPageIndex) {
        return new BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          items: [
            BottomNavigationBarItem(
              title: Text("Page1"),
              icon: Icon(Icons.account_circle),
            ),
            BottomNavigationBarItem(
              title: Text("Page2"),
              icon: Icon(Icons.account_circle),
            ),
          ],
          onTap: (index) {

/////////////////////////////START OF SECTION///////////////////////////
            _MyHomePageState().setState(() {
              selectedPageIndex = index;
            });
/////////////////////////////END OF SECTION///////////////////////////

          },
          currentIndex: selectedPageIndex,
        );
      }
    }
导入“包装:颤振/材料.省道”;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“我的标题”,
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
var selectedPageIndex=0;
var pages=[Page1(),Page2(),];
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:页面[selectedPageIndex],
底部导航栏:
MyClass().buildBottomNavigationBar(selectedPageIndex),
);
}
}
类MyClass{
底部导航栏构建底部导航栏(selectedPageIndex){
返回新的底部导航栏(
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
标题:正文(“第1页”),
图标:图标(图标、账户和圆圈),
),
底部导航气压计(
标题:正文(“第2页”),
图标:图标(图标、账户和圆圈),
),
],
onTap:(索引){
/////////////////////////////节首///////////////////////////
_MyHomePageState().setState(){
selectedPageIndex=索引;
});
/////////////////////////////节末///////////////////////////
},
currentIndex:selectedPageIndex,
);
}
}
--------------编辑:----------------

好的,现在我有下面的代码,我得到以下两件事:

信息: 成员“setState”只能在“package:flatter/src/widgets/framework.dart”子类的实例成员中使用

例外情况: 处理手势时引发以下NoSuchMethodError: 对null调用了方法“setState”。 收件人:空 尝试调用:setState(闭包:()=>Null)

导入“包装:颤振/材料.省道”;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“我的标题”,
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
静态void setIndex(BuildContext上下文,int\u newIndex){
_MyHomePageState=context.anteStorStateofType(TypeMatcher());
state.setState(){
state.selectedPageIndex=\u newIndex;
});
}
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
var selectedPageIndex=0;
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:新容器(),
底部导航栏:
MyClass().buildBottomNavigationBar(上下文,selectedPageIndex),
);
}
}
类MyClass{
BottomNavigationBar构建BottomNavigationBar(上下文,selectedPageIndex){
返回新的底部导航栏(
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
标题:正文(“第1页”),
图标:图标(图标、账户和圆圈),
),
底部导航气压计(
标题:正文(“第2页”),
图标:图标(图标、账户和圆圈),
),
],
onTap:(索引){
设置索引(上下文,索引);
},
currentIndex:selectedPageIndex,
);
}
}

您需要的是另一个类的
回调
函数。必须在对象上调用As
setState
\u MyHomePageState

import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'My Title',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var selectedPageIndex = 0;
  var pages = [
    Page1(),
    Page2(),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: pages[selectedPageIndex],
      bottomNavigationBar: MyClass(
        selectedPageIndex: selectedPageIndex,
        myFunc: _myFunc,
      ),
    );
  }

  void _myFunc(int index) {
    setState(() {
      selectedPageIndex = index;
    });
  }
}

class MyClass extends StatelessWidget {
  MyClass({this.selectedPageIndex, this.myFunc});
  final int selectedPageIndex;
  final Function myFunc;
  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          title: Text("Page1"),
          icon: Icon(Icons.account_circle),
        ),
        BottomNavigationBarItem(
          title: Text("Page2"),
          icon: Icon(Icons.account_circle),
        ),
      ],
      onTap: (index) {
/////////////////////////////START OF SECTION///////////////////////////
        myFunc(index);
/////////////////////////////END OF SECTION///////////////////////////
      },
      currentIndex: selectedPageIndex,
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Text('1'),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Container(child: Text('3'),));
  }
}
使用
类构造函数
我们传递初始数据并在
SetState()
上得到回调

导入“包装:颤振/材料.省道”;
void main(){
runApp(新的MyApp());
}
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回新材料PP(
标题:“我的标题”,
主页:新建MyHomePage(),
);
}
}
类MyHomePage扩展StatefulWidget{
@凌驾
_MyHomePageState createState()=>new_MyHomePageState();
}
类_MyHomePageState扩展状态{
var selectedPageIndex=0;
变量页=[
第1页(),
第2页(),
];
@凌驾
小部件构建(构建上下文){
归还新脚手架(
正文:页面[selectedPageIndex],
底部导航栏:MyClass(
selectedPageIndex:selectedPageIndex,
myFunc:_myFunc,
),
);
}
void _myFunc(整数索引){
设置状态(){
selectedPageIndex=索引;
});
}
}
类MyClass扩展了无状态小部件{
MyClass({this.selectedPageIndex,this.myFunc});
最终int-selectedPageIndex;
最终函数myFunc;
@凌驾
小部件构建(构建上下文){
返回底部导航栏(
类型:BottomNavigationBarType.fixed,
项目:[
底部导航气压计(
标题:正文(“第1页”),
图标:图标(图标、账户和圆圈),
),
底部导航气压计(
标题:正文(“第2页”),
图标:图标(图标、账户和圆圈),
),
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'My Title',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  var selectedPageIndex = 0;
  var pages = [
    Page1(),
    Page2(),
  ];

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: pages[selectedPageIndex],
      bottomNavigationBar: MyClass(
        selectedPageIndex: selectedPageIndex,
        myFunc: _myFunc,
      ),
    );
  }

  void _myFunc(int index) {
    setState(() {
      selectedPageIndex = index;
    });
  }
}

class MyClass extends StatelessWidget {
  MyClass({this.selectedPageIndex, this.myFunc});
  final int selectedPageIndex;
  final Function myFunc;
  @override
  Widget build(BuildContext context) {
    return BottomNavigationBar(
      type: BottomNavigationBarType.fixed,
      items: [
        BottomNavigationBarItem(
          title: Text("Page1"),
          icon: Icon(Icons.account_circle),
        ),
        BottomNavigationBarItem(
          title: Text("Page2"),
          icon: Icon(Icons.account_circle),
        ),
      ],
      onTap: (index) {
/////////////////////////////START OF SECTION///////////////////////////
        myFunc(index);
/////////////////////////////END OF SECTION///////////////////////////
      },
      currentIndex: selectedPageIndex,
    );
  }
}

class Page1 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: Container(
        child: Text('1'),
      ),
    );
  }
}

class Page2 extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(child: Container(child: Text('3'),));
  }
}
  class MyHomePage extends StatefulWidget {

  static void setIndex(BuildContext context, int _newIndex) {
  _MyHomePageState state = context.ancestorStateOfType(TypeMatcher<_MyHomePageState>());
    state.setState(() {
      state.selectedPageIndex =_newIndex;
    });
  }

  @override
  _MyHomePageState createState() => new _MyHomePageState();
  }
  onTap (index) {
    MyHomePage.setIndex(context, index);
  }