如何控制bottom sheet的默认pop属性?比如我想给一个变量赋值当showModalBottomSheet弹出时,我尝试过控制器
你为什么不直接做:
showModalBottomSheet(
context: context,
builder: (context) {
var a = "desired value";
return Widget;
你可以触发当底部表单弹出/解散的AnimationController
像这样:
在你的StatefulWidget
的State
:
late AnimationController _controller;
@override
void initState() {
_controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 300),
);
_controller.addListener(() {
if (_controller.isDismissed) {
print("dismissed");
}
});
super.initState();
}
@override
void dispose() {
_controller.dispose;
super.dispose();
}
在你的showModalBottomSheet
:
showModalBottomSheet(
context: context,
builder: (context) => Container(),
transitionAnimationController: _controller, // assign the controller
);
你可以设置isDismissible: false,
,然后在点击按钮时添加一个按钮(关闭按钮),你必须编写代码并弹出底部列表。
showModalBottomSheet(
isScrollControlled: true,
isDismissible: false,
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(15),
),
),
context: context,
builder: (context) {
return SizedBox(
height:
MediaQuery.of(context).size.height * (0.6),
child: Padding(
padding: const EdgeInsets.only(top: 15),
child: Column(
children: [
Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
InkWell(
onTap: () {
// Add your code here. which you want to perform before closing bottomSheet
Navigator.pop(context);
},
child: const Icon(Icons.close)),
InkWell(
onTap: () {},
child: const Text(
"Reset",
)),
],
),
const SizedBox(height: 15),
//Other widgets of bottomSheet
Container(
height:
MediaQuery.of(context).size.height * (0.5),
color: Colors.amber,
)
],
),
),
);
});
感谢您的帮助
我用WillPopScope解决了这个问题
popfunction() {
SelectedValue = tempValue;
Navigator.pop(context, true);
}
onWillPop: () async {
return popfunction() ?? false;
},
如果要点击back button
,showModalBottomSheet
关闭,我建议在showModalBottomSheet
useRootNavigator: true