简体   繁体   English

如何将数据从子状态小部件传递到 Flutter 中的父小部件

[英]How to pass data from a child Stateful widget to Parent Widget in Flutter

I have a Stateful widget 'DayPicker' in my flutter application.我的 flutter 应用程序中有一个有状态小部件“DayPicker”。 Code for the same is:相同的代码是:

class DayPicker extends StatefulWidget {
  @override
  _DayPickerState createState() =>
      _DayPickerState();
}

class _DayPickerState extends State<DayPicker> {
  final values2 = List.filled(7, false);
  @override
  Widget build(BuildContext context) {
    var ht = MediaQuery.of(context).size.height;
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: <Widget>[
        SizedBox(
          height: ht / 40,
        ),
        Text(
          'Selected Day(s): ${valuesToEnglishDays(values2, true)}',
          style: TextStyle(fontSize: ht / 40, fontStyle: FontStyle.italic),
        ),
        WeekdaySelector(
          onChanged: (v) {
            setState(() {
              values2[v % 7] = !values2[v % 7];
            });
          },
          values: values2,
          selectedFillColor: Colors.amber,
          selectedColor: Colors.black,
          selectedShape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(5),
          ),
          shape: RoundedRectangleBorder(
            side: BorderSide(color: Colors.red.withOpacity(0.5)),
            borderRadius: BorderRadius.circular(25),
          ),
        ),
      ],
    );
  }
}

This widget is used to select the days of week as shown here:此小部件用于 select 星期几,如下所示:

在此处输入图像描述

I want the list of days to be passed into my Parent widget ie the widget using DayPicker(), so that I can store this list to Firebase Firestore.我希望将天数列表传递给我的父小部件,即使用 DayPicker() 的小部件,以便我可以将此列表存储到 Firebase Firestore。 How can I do this?我怎样才能做到这一点? In case of Stateless widget there is a method to pass data from child to parent using callback but I wonder how to pass data in this case.在无状态小部件的情况下,有一种方法可以使用回调将数据从子级传递到父级,但我想知道在这种情况下如何传递数据。

You can also use callback to handle in this case.在这种情况下,您也可以使用回调来处理。 You need to use it like widget.callback(value) where it is changing data on child.您需要像widget.callback(value)一样使用它来更改子数据。 Run the bellow snippet and do a check yourself.运行下面的代码片段并自己检查一下。


class AppX extends StatefulWidget {
  final String province;
  const AppX({
    Key? key,
    required this.province,
  }) : super(key: key);

  @override
  State<AppX> createState() => _AppXState();
}

class _AppXState extends State<AppX> {
  double? sliderValue;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Text("${sliderValue ?? 0}"), //handling null using defaul value
          Child(
            callback: (p0) {
              setState(() {
                sliderValue = p0;
              });
            },
          )
        ],
      ),
    );
  }
}

class Child extends StatefulWidget {
  const Child({
    Key? key,
    required this.callback,
  }) : super(key: key);

  final Function(double) callback;

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

class _ChildState extends State<Child> {
  double _sliderValue = 0;

  @override
  Widget build(BuildContext context) {
    return Slider(
      value: _sliderValue,
      onChanged: (value) {
        widget.callback(value);
        setState(() {
          _sliderValue = value;
        });
      },
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter:如何将数据从父状态小部件传递到BottomNavigationBar中的选项卡之一? - Flutter: How to pass data from the parent stateful widget to one of the tabs in the BottomNavigationBar? 将数据从子小部件传递到父小部件 - Pass data from child widget to the parent widget 如何使用 flutter 中的提供程序或 valuenotifier 将数据从子小部件传递到父小部件 - How to pass data from child widget to parent widget using provider or valuenotifier in flutter 如何从父小部件在子有状态小部件内运行函数? - How to run a function inside a child stateful widget from parent widget? Flutter:如何将数据从子部件传递给父部件? - Flutter: How to pass data from child widget to parents widget? 如何将数据从子小部件传递到父小部件 - How can i pass data from a child widget to a parent widget 如何在更新父有状态小部件时更新子有状态小部件 - How to Update child stateful widget on update parent stateful widget 如何将数据传递给有状态的小部件 - How to pass data to stateful widget 如何在不重建父小部件的情况下将数据子小部件传递给子小部件? - How to pass data child to child widget without rebuild parent widget in flutter? 如何将父小部件的高度传递给 flutter 中的子小部件? - How to pass the height of parent widget to child widget in flutter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM