how can i controll the default pop property of bottom sheet.Like I want to asign a value to a variable when showModalBottomSheet is popped .I have tried to do with controllers
CodePudding user response:
Why don't you just do :
showModalBottomSheet(
context: context,
builder: (context) {
var a = "desired value";
return Widget;
CodePudding user response:
you can trigger when the bottom sheet is popped/dismissed with an AnimationController
like this:
in your StatefulWidget
's 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();
}
in your showModalBottomSheet
:
showModalBottomSheet(
context: context,
builder: (context) => Container(),
transitionAnimationController: _controller, // assign the controller
);
CodePudding user response:
You can set isDismissible: false,
and than add one button (Close button) on tap of button, you have to do your code and pop the bottomSheet.
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,
)
],
),
),
);
});