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 如何将回调传递给另一个StatefulWidget?_Dart_Flutter - Fatal编程技术网

Dart 如何将回调传递给另一个StatefulWidget?

Dart 如何将回调传递给另一个StatefulWidget?,dart,flutter,Dart,Flutter,例如,我有两个StatefulWidget来监视相同的回调方法。我该怎么做?如果我有三个以上的StatefulWidget来监视它的事件 class WidgetOne extends StatefulWidget { @override _WidgetOneState createState() => new _WidgetOneState(); } class _WidgetOneState extends State<WidgetOne> { // thi

例如,我有两个StatefulWidget来监视相同的回调方法。我该怎么做?如果我有三个以上的StatefulWidget来监视它的事件

class WidgetOne extends StatefulWidget {
  @override
  _WidgetOneState createState() => new _WidgetOneState();
}

class _WidgetOneState extends State<WidgetOne> {

  // this is the callback, the widget two want listen the callback too
  bool _onNotification(ScrollNotification notification){

  }

  @override
  Widget build(BuildContext context) {
    return new Column(
      children: <Widget>[
        new NotificationListener(child: new ListView(shrinkWrap: true,),
          onNotification: _onNotification),
        new WidgetTwo()
      ],
    );
  }
}

class WidgetTwo extends StatefulWidget {
  @override
  _WidgetTwoState createState() => new _WidgetTwoState();
}

class _WidgetTwoState extends State<WidgetTwo> {

  // in this,How Can I get the callback in WidgetOne?
  @override
  Widget build(BuildContext context) {
    return new Container();
  }
}
类WidgetOne扩展StatefulWidget{
@凌驾
_WidgetOneState createState()=>new_WidgetOneState();
}
类_WidgetOneState扩展状态{
//这是回调,小部件2也要监听回调
bool_onNotification(滚动通知通知){
}
@凌驾
小部件构建(构建上下文){
返回新列(
儿童:[
新建NotificationListener(子项:新建ListView(包覆面提取:true,),
onNotification:_onNotification),
新widgetwo()
],
);
}
}
类widgetwo扩展StatefulWidget{
@凌驾
_WidgetWoState createState()=>新建WidgetWoState();
}
类_widgetwostate扩展状态{
//在这种情况下,如何在WidgetOne中获取回调?
@凌驾
小部件构建(构建上下文){
返回新容器();
}
}

你不能也不应该。小部件永远不应该依赖于其他小部件的体系结构

你有两种可能:

  • 合并
    widgetwo
    WidgetOne
    。因为将它们分开是没有意义的(至少对于您提供的内容是如此)
  • 修改widgetwo以获取一个孩子。并将该
    ListView
    添加为
    widgetwo
    的子级。这样它就可以将列表包装到自己的
    NotificationListener

      你不能也不应该。小部件永远不应该依赖于其他小部件的体系结构

      你有两种可能:

      • 合并
        widgetwo
        WidgetOne
        。因为将它们分开是没有意义的(至少对于您提供的内容是如此)
      • 修改widgetwo以获取一个孩子。并将该
        ListView
        添加为
        widgetwo
        的子级。这样它就可以将列表包装到自己的
        NotificationListener

      您的解决方案可以通过使用
      setState()
      并在widgetwo的构造函数中传递状态函数来实现。我在下面做了一个例子,这个例子的主要思想是我把MyHomePage作为我的主要小部件和MyFloatButton(我想定制为另一个
      StatefulWidget
      ),所以当按下FAB时,我需要在MyHomePage中调用增量计数器功能。下面让我们看看我是如何做到这一点的

      class MyHomePage extends StatefulWidget {
        MyHomePage({Key key, this.title}) : super(key: key);
      
        final String title;
      
        @override
        _MyHomePageState createState() => new _MyHomePageState();
      }
      
      class _MyHomePageState extends State<MyHomePage> {
        int _counter = 0;
      
        //Consider this function as your's _onNotification and important to note I am using setState() within :)
        void _incrementCounter() { 
          setState(() {
            _counter++;
          });
        }
      
        @override
        Widget build(BuildContext context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text(
                widget.title,
                style: TextStyle(color: Colors.white),
              ),
            ),
            body: new Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(
                    'You have pushed the button $_counter times:',
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
            floatingActionButton: new MyFloatButton(_incrementCounter),//here I am calling MyFloatButton Constructor passing _incrementCounter as a function
          );
        }
      }
      
      class MyFloatButton extends StatefulWidget {
      
        final Function onPressedFunction;
      
        // Here I am receiving the function in constructor as params
        MyFloatButton(this.onPressedFunction);
      
        @override
        _MyFloatButtonState createState() => new _MyFloatButtonState();
      }
      
      class _MyFloatButtonState extends State<MyFloatButton> {
        @override
        Widget build(BuildContext context) {
          return new Container(
            padding: EdgeInsets.all(5.0),
            decoration: new BoxDecoration(color: Colors.orangeAccent, borderRadius: new BorderRadius.circular(50.0)),
            child: new IconButton(
              icon: new Icon(Icons.add),
              color: Colors.white,
              onPressed: widget.onPressedFunction,// here i set the onPressed property with widget.onPressedFunction. Remember that you should use "widget." in order to access onPressedFunction here!
            ),
          );
        }
      }
      
      类MyHomePage扩展StatefulWidget{
      MyHomePage({Key,this.title}):超级(Key:Key);
      最后的字符串标题;
      @凌驾
      _MyHomePageState createState()=>new_MyHomePageState();
      }
      类_MyHomePageState扩展状态{
      int _计数器=0;
      //将此函数视为您的_onNotification,需要注意的是,我在以下范围内使用了setState()
      void _incrementCounter(){
      设置状态(){
      _计数器++;
      });
      }
      @凌驾
      小部件构建(构建上下文){
      归还新脚手架(
      appBar:新的appBar(
      标题:新文本(
      widget.title,
      样式:TextStyle(颜色:Colors.white),
      ),
      ),
      正文:新中心(
      子:新列(
      mainAxisAlignment:mainAxisAlignment.center,
      儿童:[
      新文本(
      '您已按下按钮$\计数器次数:',
      样式:TextStyle(fontSize:20.0,fontWeight:fontWeight.bold),
      ),
      ],
      ),
      ),
      floatingActionButton:newmyfloatButton(\u incrementCounter),//这里我调用MyFloatButton构造函数,并将\u incrementCounter作为函数传递
      );
      }
      }
      类MyFloatButton扩展StatefulWidget{
      最终函数onpressed函数;
      //在这里,我接收构造函数中的函数作为参数
      MyFloatButton(此.onPressed功能);
      @凌驾
      _MyFloatButtonState createState()=>new_MyFloatButtonState();
      }
      类_MyFloatButtonState扩展了状态{
      @凌驾
      小部件构建(构建上下文){
      退回新货柜(
      填充:所有边缘设置(5.0),
      装饰:新框装饰(颜色:Colors.orangeacent,borderRadius:newborderradius.circular(50.0)),
      孩子:新的图标按钮(
      图标:新图标(Icons.add),
      颜色:颜色,白色,
      onPressed:widget.onPressedFunction,//这里我用widget.onPressedFunction设置了onPressed属性。记住,您应该使用“widget.”才能在这里访问onPressedFunction!
      ),
      );
      }
      }
      

      现在把MyHouthPoad视为您的WigGeOne,MyFloatButton作为您的WIDGET2和One增量计数器函数作为您的通知。希望你能实现你想要的:)


      (我做了一个一般性的示例,这样任何人都可以根据他们面临的场景来理解)

      您的解决方案可以通过使用
      setState()
      并在widgetwo的构造函数中传递状态函数来实现。我在下面做了一个例子,这个例子的主要思想是我把MyHomePage作为我的主要小部件和MyFloatButton(我想定制为另一个
      StatefulWidget
      ),所以当按下FAB时,我需要在MyHomePage中调用增量计数器功能。下面让我们看看我是如何做到这一点的

      class MyHomePage extends StatefulWidget {
        MyHomePage({Key key, this.title}) : super(key: key);
      
        final String title;
      
        @override
        _MyHomePageState createState() => new _MyHomePageState();
      }
      
      class _MyHomePageState extends State<MyHomePage> {
        int _counter = 0;
      
        //Consider this function as your's _onNotification and important to note I am using setState() within :)
        void _incrementCounter() { 
          setState(() {
            _counter++;
          });
        }
      
        @override
        Widget build(BuildContext context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text(
                widget.title,
                style: TextStyle(color: Colors.white),
              ),
            ),
            body: new Center(
              child: new Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  new Text(
                    'You have pushed the button $_counter times:',
                    style: TextStyle(fontSize: 20.0, fontWeight: FontWeight.bold),
                  ),
                ],
              ),
            ),
            floatingActionButton: new MyFloatButton(_incrementCounter),//here I am calling MyFloatButton Constructor passing _incrementCounter as a function
          );
        }
      }
      
      class MyFloatButton extends StatefulWidget {
      
        final Function onPressedFunction;
      
        // Here I am receiving the function in constructor as params
        MyFloatButton(this.onPressedFunction);
      
        @override
        _MyFloatButtonState createState() => new _MyFloatButtonState();
      }
      
      class _MyFloatButtonState extends State<MyFloatButton> {
        @override
        Widget build(BuildContext context) {
          return new Container(
            padding: EdgeInsets.all(5.0),
            decoration: new BoxDecoration(color: Colors.orangeAccent, borderRadius: new BorderRadius.circular(50.0)),
            child: new IconButton(
              icon: new Icon(Icons.add),
              color: Colors.white,
              onPressed: widget.onPressedFunction,// here i set the onPressed property with widget.onPressedFunction. Remember that you should use "widget." in order to access onPressedFunction here!
            ),
          );
        }
      }
      
      类MyHomePage扩展StatefulWidget{
      MyHomePage({Key,this.title}):超级(Key:Key);
      最后的字符串标题;
      @凌驾
      _MyHomePageState createState()=>new_MyHomePageState();
      }
      类_MyHomePageState扩展状态{
      int _计数器=0;
      //将此函数视为您的_onNotification,需要注意的是,我在以下范围内使用了setState()
      void _incrementCounter(){
      设置状态(){
      _计数器++;
      });
      }
      @凌驾
      小部件构建(构建上下文){
      归还新脚手架(
      appBar:新的appBar(
      标题:新文本(
      widget.title,
      样式:TextStyle(颜色:Colors.white),
      ),
      ),
      正文:新中心(
      子:新列(
      男主角
      
      class WidgetTwo extends StatefulWidget {
        final Function callback;
        
        WidgetTwo({this.callback});
      
      
        @override
        _WidgetTwoState createState() => new _WidgetTwoState();
      }
      
      widget.callback