在Flutter中,是否可以防止ModalBottomSheet
被触摸到外部时隐藏?就像在showDialog()
中使用barrierDismissible
属性一样,我们可以使用该属性来防止对话框在外部触摸时关闭
在Flutter中,是否可以防止ModalBottomSheet
被触摸到外部时隐藏?就像在showDialog()
中使用barrierDismissible
属性一样,我们可以使用该属性来防止对话框在外部触摸时关闭
您可以按照以下方式使用 isDismissible: false
和 enableDrag: false
showModalBottomSheet(
isDismissible: false,
enableDrag: false,
builder: (context) {
return Container(
height: 100.0
)
}
);
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
final _scaffoldKey = new GlobalKey<ScaffoldState>();
VoidCallback _showPersBottomSheetCallback;
@override
void initState() {
super.initState();
_showPersBottomSheetCallback = _showBottomSheet;
}
void _showBottomSheet() {
setState(() {
_showPersBottomSheetCallback = null;
});
_scaffoldKey.currentState
.showBottomSheet((context) {
return new Container(
color: Colors.greenAccent,
height: 300.0,
child: new Center(
child: new Text("Hi BottomSheet"),
),
);
})
.closed
.whenComplete(() {
if (mounted) {
setState(() {
_showPersBottomSheetCallback = _showBottomSheet;
});
}
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
key: _scaffoldKey,
appBar: new AppBar(
title: new Text("Flutter BottomSheet"),
),
body: GestureDetector(
behavior: HitTestBehavior.opaque,
onTap: () {},
child: Padding(
padding: const EdgeInsets.only(top: 10.0),
child: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new RaisedButton(
onPressed: _showPersBottomSheetCallback,
child: new Text("Persistent"),
),
],
),
),
),
),
);
}
}
showBottomSheet()
回调时,我认为这只是一种解决方法而不是解决方案。 - CopsOnRoad showModalBottomSheet(
isDismissible: false,
context: context,
builder: (BuildContext bc) {
return SheetWidget();
},
);
其中SheetWidget是您要作为BottomSheet调用的小部件
showModalBottomSheet
和showBottomSheet
之间唯一的区别是后者不能通过触摸其上下文以外的区域来解除。这不就是你要求的吗? - CopsOnRoadAbsorbPointer
小部件来实现这个功能。 - CopsOnRoad