ModalBottomSheet弹出后,如何更新原始页面的状态



我已经设置了一个floatingActionButton:

floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
shape:
RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
onPressed: () async {
_showAddDrinkPanel();
},
),
void _showAddDrinkPanel() async {
showModalBottomSheet (
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0))),
context: context,
builder: (context) {
return ChooseDrinkForm();
});
}

choose_drink_form.dart

在这个页面上,它让用户输入饮酒量,然后将其保存到数据库(sqflite(中。要提交的按钮是:

onPressed: () {
setState(() {
_submitLog();
Navigator.pop(context);
});

现在的问题是,当Modal弹出时,页面没有更新以反映用户刚刚在Modal中添加的数量。

您可以在showModalBottomSheet的then方法中调用setState方法,而不是在onpressed方法中调用setState方法,这可能会解决您的问题。

就像下面的代码一样。

void _showAddDrinkPanel() async {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0))),
context: context,
builder: (context) {
return Text("data"); 
}).then((value) {
setState(() {});    // setState addded
});
}

最新更新