我该如何禁用/取消底部弹窗模态框的下拉手势,以便用户可以在模态框内进行交互而不会意外关闭模态框?
下面更新了实际的底部弹窗模态框。
return showModalBottomSheet(
context: context,
builder: (BuildContext context) {
...
}
}
我该如何禁用/取消底部弹窗模态框的下拉手势,以便用户可以在模态框内进行交互而不会意外关闭模态框?
下面更新了实际的底部弹窗模态框。
return showModalBottomSheet(
context: context,
builder: (BuildContext context) {
...
}
}
将enableDrag
设置为false
bool enableDrag
如果为true,则底部表可以上下拖动并通过向下滑动解除。
https://docs.flutter.io/flutter/material/BottomSheet/enableDrag.html
您可以尝试使用带有 onVerticalDragStart = (_) {} 的 GestureDetector 包装构建器的结果。
showModalBottomSheet(
context: context,
builder: (context) => GestureDetector(
child: **any_widget_here**,
onVerticalDragStart: (_) {},
),
isDismissible: false,
isScrollControlled: true,
);
如果您仍希望在模态框中使用滚动条但又不希望用户拖动和关闭它,可以使用以下代码:
showModalBottomSheet(
context: context,
enableDrag: false,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
clipBehavior: Clip.antiAliasWithSaveLayer,
builder: (context) => DraggableScrollableSheet(
expand: false,
initialChildSize: 0.9,
minChildSize: 0.5,
maxChildSize: 0.9,
builder: (context, scrollController) {
return SingleChildScrollView(
child: new Container(
color: Colors.white,
child: buildTitleWidget(),
),
);
},
),
isDismissible: false,
isScrollControlled: true,
);
builder: (context, scrollController) {
return SingleChildScrollView(
controller: scrollController <-- HERE
child: new Container(
color: Colors.white,
child: buildTitleWidget(),
),
);
},
我想要一个可以上下拖动,但向下拖动时不会关闭的底部工作表。我的想法是,如果一旦它关闭,我立即再次调用它会发生什么?这时,递归就来拯救了。因此,首先我为我的 modalBottomSheet 创建了一个函数。
Future modalBottomSheetShow(BuildContext context) {
return showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (context) => buildSheet(),
isDismissible: false,
elevation: 0,
);
}
Future modalBottomSheetShow(BuildContext context) {
return showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (context) => buildSheet(),
isDismissible: false,
elevation: 0,
).whenComplete(() => modalBottomSheetShow(context));
}
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
static const idScreen = "HomePage";
@override
State<HomePage> createState() => _HomePageState();
}
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) async {
modalBottomSheetShow(context);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
toolbarHeight: 0,
elevation: 0,
backgroundColor: Colors.black,
),
);
}
Widget buildSheet() {
return DraggableScrollableSheet(
initialChildSize: 0.6,
builder: (BuildContext context, ScrollController scrollController) {
return Container(
decoration: BoxDecoration(color: Colors.white, boxShadow: [
BoxShadow(
color: Color(0x6C000000),
spreadRadius: 5,
blurRadius: 20,
offset: Offset(0, 0),
)
]),
padding: EdgeInsets.all(16),
);
},
);
}
Future modalBottomSheetShow(BuildContext context) {
return showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (context) => buildSheet(),
isDismissible: false,
elevation: 0,
).whenComplete(() => modalBottomSheetShow(context));
}
}
BottomSheet
。 - Jesse