我如何将数据从showmodalbottomsheet
上一页传递。下面是示例代码,我尝试过的是当我单击它时有一个按钮显示 modalbottomsheet,当我单击完成按钮时它应该将1
值传递到上一页并且我setState
在onTap
按下时添加了但它仍然没有改变以前页面,除非我点击Flutter Hot Reload
。如何解决这个问题?
class TestPage extends StatefulWidget {
const TestPage({Key? key}) : super(key: key);
@override
_TestPageState createState() => _TestPageState();
}
class _TestPageState extends State<TestPage> {
String textResult = "";
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(textResult),
TextButton(
onPressed: () {
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topRight: Radius.circular(24.0),
topLeft: Radius.circular(24.0))),
context: context,
builder: (_) => StatefulBuilder(
builder: (BuildContext context, setState) {
return Container(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
GestureDetector(
onTap: () {
setState(() {
textResult = "1";
});
Navigator.pop(context);
},
child: Text(
'Done',
style: TextStyle(
color: Colors.red,
fontWeight: FontWeight.bold),
),
),
],
),
),
],
),
);
}),
);
},
child: Text('Press'))
],
),
),
),
),
);
}
}