【发布时间】:2019-12-27 14:48:13
【问题描述】:
是否可以防止ModalBottomSheet 被外界触摸隐藏?就像在 showDialog() 中一样,我们可以使用 barrierDismissible property to prevent dialog from closing on outside touch
【问题讨论】:
是否可以防止ModalBottomSheet 被外界触摸隐藏?就像在 showDialog() 中一样,我们可以使用 barrierDismissible property to prevent dialog from closing on outside touch
【问题讨论】:
你可以像这样使用isDismissible: false和enableDrag: false
showModalBottomSheet(
isDismissible: false,
enableDrag: false,
builder: (context) {
return Container(
height: 100.0
)
}
);
【讨论】:
尝试在 showModalBottomSheet 中将 isDismissible 设置为 false
showModalBottomSheet(
isDismissible: false,
context: context,
builder: (BuildContext bc) {
return SheetWidget();
},
);
SheetWidget 是您尝试调用为 BottomSheet 的小部件
【讨论】:
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() 回调时,我认为这是一种解决方法而不是解决方案。
您需要使用不包含屏障的showBottomSheet() 而不是使用showModalBottomSheet()。
更多信息here
【讨论】:
showModalBottomSheet 和showBottomSheet 之间的唯一区别是后者不能通过触摸其上下文之外的内容来消除。和你要求的不一样吗?
showBottomSheet 后访问/触摸外部小部件?如果我错了,请纠正我?
AbsorbPointer 小部件。