如何重写showModalBottomSheet 方法
时间: 2023-10-24 07:16:16 浏览: 78
Flutter中 AlertDialog、SimpleDialog、showModalBottomSheet及第三方组件fluttertoast的使用
重写showModalBottomSheet方法需要继承BottomSheet类,并且覆盖掉showModalBottomSheet方法。具体实现如下:
```dart
class MyBottomSheet extends BottomSheet {
MyBottomSheet({
required Widget child,
required BuildContext context,
}) : super(
onClosing: () {},
builder: (BuildContext context) => child,
);
@override
Future<T?> showModalBottomSheet<T>({
required BuildContext context,
required WidgetBuilder builder,
Color? backgroundColor,
double? elevation,
ShapeBorder? shape,
Clip? clipBehavior,
bool? isScrollControlled,
bool? useRootNavigator,
bool? isDismissible,
bool? enableDrag,
}) {
return super.showModalBottomSheet(
context: context,
builder: (BuildContext context) {
return Container(
padding: EdgeInsets.all(16.0),
child: builder(context),
);
},
backgroundColor: backgroundColor,
elevation: elevation,
shape: shape,
clipBehavior: clipBehavior,
isScrollControlled: isScrollControlled,
useRootNavigator: useRootNavigator,
isDismissible: isDismissible,
enableDrag: enableDrag,
);
}
}
```
这样就可以使用MyBottomSheet类来重写showModalBottomSheet方法,同时可以对底部弹出框的样式进行自定义。
阅读全文