【问题标题】:From child set state of other child component从其他子组件的子集状态
【发布时间】:2019-08-19 06:29:47
【问题描述】:

假设我们有 Car (Stateless) 类,它里面有两个类,现在它将是 Wheel (Statefull) 和 Mask (Statefull),我的工作是每当类 Wheel 的状态改变时调用类 Mask 来改变它状态也包含来自 Wheel 的特定数据,但父级也应该有权访问子级数据。我怎样才能实现它?

import 'package:flutter/material.dart';

void main() {
  runApp(Car());
}

class Car extends StatelessWidget {
 int childMaskVal = ..??????

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Scaffold(
        appBar: AppBar(
          title: Text('App bar'),
        ),
        body: Column(
          children: <Widget>[
            Wheel(),
            Mask(),
          ],
        ),
      ),
    );
  }
}


class Wheel extends StatefulWidget {

  _WheelState createState() => _WheelState();
}

class _WheelState extends State<Wheel> {
 int _value = 0;

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Column(
         children: <Widget>[
           RaisedButton(
           onPressed: () {
             setState(() {
              _value++; 
             });
           },
         ),
         Text(_value.toString()),
         ],
       ),
    );
  }
}

class Mask extends StatefulWidget {

  _MaskState createState() => _MaskState();
}

class _MaskState extends State<Mask> {
  int _value = 13;

  @override
  Widget build(BuildContext context) {
    return Container(
       child: Text((_value * Wheel._value).toString()),???????
    );
  }
}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以将回调传递给孩子。

    在您提供的示例中,父级应为statefulWidget,子级应为无状态小部件。您应该在父级中初始化状态,然后将其传递给子级。

    如果你发现 parent 需要访问 child 中的数据,这意味着你需要将 state 向上移动。

    请看下面的例子:

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(Car());
    }
    
    class Car extends StatefulWidget{
    @override
      _CarState createState() => _CarState();
    }
    
    class _CarState extends State<Car> {
     int childMaskVal = ..??????
     int _value = 1;
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: Scaffold(
            appBar: AppBar(
              title: Text('App bar'),
            ),
            body: Column(
              children: <Widget>[
                Wheel(
                  value: _value,
                  onPressed: () {
                   setState(() {
                      _value++;
                      // update state here
                   })
                }),
                Mask(),
              ],
            ),
          ),
        );
      }
    }
    
    class Wheel extends Stateless {
      int value;
      Function onPressed;
      Wheel({this.value, this.onPresed})
    
      @override
      Widget build(BuildContext context) {
        return Container(
           child: Column(
             children: <Widget>[
               RaisedButton(
               onPressed: onPressed
             ),
             Text(value.toString()),
             ],
           ),
        );
      }
    }
    
    

    你也可以查看这个帖子How to pass data from child widget to its parent

    【讨论】:

    • 这两个我都看过了。我正在寻找更全球化的东西,或者至少可以在不成为某些小部件的孩子或父母的情况下交换信息。
    猜你喜欢
    • 2019-08-29
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    • 2020-12-14
    • 2019-04-04
    • 2021-03-14
    • 2017-11-05
    • 2021-11-02
    相关资源
    最近更新 更多