【问题标题】:Pass data from child widget to the parent widget将数据从子小部件传递到父小部件
【发布时间】:2020-07-24 09:59:18
【问题描述】:

我有一个仪表板小部件,其主体如下:

我想将数据从子小部件 DashboardGrid(在代码块末尾检查)传递到此父小部件。我该怎么做?

    body: Column(
      children: <Widget>[
      SizedBox(
      height: 20,
    ),
        Padding(
          padding: EdgeInsets.only(left: 16, right: 16),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text(
                    "Categories",
                    style:  TextStyle(
                            color: Colors.white,
                            fontSize: 20,
                            fontWeight: FontWeight.bold)),
     
                ],
              ),
              IconButton(
                alignment: Alignment.topCenter,
                icon: new Icon(Icons.search, color: Theme.of(context).hintColor,),
                onPressed: () {},
              )
            ],
          ),
        ),
        SizedBox(
          height: 40,
        ),
DashboardGrid(),
])

【问题讨论】:

  • 只需将一个函数作为参数从 parent 传递给 DashboardGrid() ,然后通过传递您要传递的所需数据来调用该函数。由于该函数是在 Parent 小部件类中定义的,因此您可以在该函数中评估数据

标签: flutter dart


【解决方案1】:

带有回调。

DashboardGrid内部创建一个函数


class DashboardGrid extends StatelessWidget {

final Function(String) callback;

DashboardGrid({this.callback});

....


然后在列中,您将使用函数对其进行实例化

[
...,
DashboardGrid(callback:(String value)=>print(value));
]

当您想在DashboardGrid 中传递该数据时,只需调用该函数

void passTheData(String data) => callback(data);

示例是String,但您可以传递任何数据。

【讨论】:

    【解决方案2】:

    如果您的子小部件位于同一小部件​​树下,您可以利用Notification 来冒泡数据。

    首先,根据您的需要创建通知。您可以创建 一个 通知或带有多个具体通知的抽象 Notification。对于此示例,我假设您要处理不同的通知。

    abstract class MyNotification extends Notification {}
    
    class SomethingHappened extends MyNotification {}
    
    class NothingHappened extends MyNotification {}
    

    然后您可以在父窗口小部件中处理该类型的所有通知:

    class ParentWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return NotificationListener<MyNotification>(
          onNotification: (notification) {
            // Handle your notification
            return true;
          },
          child: Container(),
        );
      }
    }
    

    或者选择个别的:

    class ParentWidget extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return NotificationListener<MyNotification>(
          onNotification: (notification) {
            if(notification is SomethingHappened){
              // Handle 
            } else if(notification is NothingHappened){
              // Handle
            }
            return true;
          },
          child: Container(),
        );
      }
    

    要从您的子窗口小部件发出通知,您只需调用T().dispatch(context),其中T 是您的Notification 类型。例如,SomethingHappened().dispatch(context);。就是这样。

    【讨论】:

      【解决方案3】:

      为了解决这个问题,有两种方法。第一种方法是创建一个 GlobalKey(https://docs.flutter.io/flutter/widgets/GlobalKey-class.html) 并将其作为参数传递给子小部件。而第二种方式是为父状态创建一个全局变量,并在子状态中使用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-24
        • 1970-01-01
        • 2021-04-23
        • 2022-01-16
        • 1970-01-01
        • 2020-02-22
        • 2021-10-12
        • 2021-08-29
        相关资源
        最近更新 更多