flutter - 如何使用flutter的showModalBottomSheet接收数据并更改其值?
问题描述
我已经实现了一个调用有状态小部件的 showModalBottomSheet。我希望有状态小部件能够从 showModalBottomSheet 调用接收数据,并对其进行修改。
下面是我的父类,即调用“showModalBottomSheet”函数的类:
class _parentClass extends StatelessWidget {
bool testing = false; //This is the variable that I am trying to change.
@override
Widget build(BuildContext context) {
void _callModalBottomSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext bc) {
return Wrap(
children: <Widget>[
Container(
child: myStatefulWidget(testingValue: testing),
),
]);
});
print("Testing Value: $testing");
}
return Column(
children: <Widget>[
FlatButton(
child: Text("my button"),
onPressed: _callModalBottomSheet,
),
],
);
}
}
“myStatefulWidget”实际上是在一个全新文件中实现的另一个类,因此,访问“测试”变量的唯一方法是通过它的构造函数(至少,我知道的唯一方法)。
我已经尝试过了,但它会引发错误:
class myStatefulWidget extends StatefulWidget {
final testingValue;
myStatefulWidget({
this.testingValue,
});
//testingValue = !testingValue; //This line throws an error!
@override
myStatefulWidgetState createState() => myStatefulWidgetState();
}
//...
非常感谢您的帮助!
解决方案
您可以在showModalBottomSheet 的then 方法中调用setState 方法,这样新数据就可以反映在scree 中。您必须在 navigator.pop 方法中传递值。
以下代码可以帮助您更好地理解。
完整的工作演示:
class DeleteWidget extends StatefulWidget {
@override
_DeleteWidgetState createState() => _DeleteWidgetState();
}
class _DeleteWidgetState extends State<DeleteWidget> {
bool testing = false; //This is the variable that I am trying to change.
@override
Widget build(BuildContext context) {
void _callModalBottomSheet() {
showModalBottomSheet(
context: context,
builder: (BuildContext bc) {
return Wrap(children: <Widget>[
Container(
child: myStatefulWidget(testingValue: testing),
),
]);
}).then((value) {
setState(() {
testing = value;
});
});
print("Testing Value: $testing");
}
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(testing.toString()),
FlatButton(
child: Text("my button"),
onPressed: _callModalBottomSheet,
),
],
),
);
}
}
class myStatefulWidget extends StatefulWidget {
final bool testingValue;
myStatefulWidget({this.testingValue});
@override
_myStatefulWidgetState createState() => _myStatefulWidgetState();
}
class _myStatefulWidgetState extends State<myStatefulWidget> {
bool index;
@override
void initState() {
super.initState();
index = widget.testingValue;
}
@override
Widget build(BuildContext context) {
return Column(
children: [
Text(index.toString()),
RaisedButton(
child: Text("change counter value"),
onPressed: () {
setState(() {
index = !index;
});
},
),
RaisedButton(
child: Text("close sheet"),
onPressed: () {
Navigator.pop(context, index);
},
)
],
);
}
}
推荐阅读
- datatables - 消除 DataTables.net 中的重复行
- css - 用于重复闪烁效果的 CSS 关键帧(wifi)
- python-import - 一个彬彬有礼的 python 模块如何安装自己?
- highcharts - Highcharts:如何在图表标签上实现多行?
- swift - 快速切换视图控制器时内存使用量增加
- c++ - CUDA 将 GpuMat 的 c 数组传递给内核
- python - 根据 numpy histogram 返回的 bin 将数组拆分为数据
- r - 需要 R Markdown Pandoc 1.12.3 或更高版本
- solace - 无法使用 JavaRTO API 为句柄创建会话
- c++ - 为什么会这样编译?[C++]