如何在 Flutter 中将数据从子 Stateful widget 传递到 Parent Widget
How to pass data from a child Stateful widget to Parent Widget in Flutter
我的 flutter 应用程序中有一个有状态小部件 'DayPicker'。相同的代码是:
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),
),
),
],
);
}
}
此小部件用于 select 星期几,如下所示:
我希望将天数列表传递到我的父小部件,即使用 DayPicker() 的小部件,以便我可以将此列表存储到 Firebase Firestore。我怎样才能做到这一点?
在无状态小部件的情况下,有一种方法可以使用回调将数据从子项传递到父项,但我想知道在这种情况下如何传递数据。
这种情况也可以使用回调来处理。您需要像 widget.callback(value)
那样使用它来更改子数据。 运行 下面的片段并自己检查一下。
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;
});
},
);
}
}
我的 flutter 应用程序中有一个有状态小部件 'DayPicker'。相同的代码是:
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),
),
),
],
);
}
}
此小部件用于 select 星期几,如下所示:
我希望将天数列表传递到我的父小部件,即使用 DayPicker() 的小部件,以便我可以将此列表存储到 Firebase Firestore。我怎样才能做到这一点? 在无状态小部件的情况下,有一种方法可以使用回调将数据从子项传递到父项,但我想知道在这种情况下如何传递数据。
这种情况也可以使用回调来处理。您需要像 widget.callback(value)
那样使用它来更改子数据。 运行 下面的片段并自己检查一下。
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;
});
},
);
}
}