是否有可能在 flutter 中为多个页面设置通用的 showModalBottomSheet

Is it Possible to have common showModalBottomSheet for multiple pages in flutter

我想将底部 Sheet 作为一个单独的函数,但找不到任何实现方法。 Bottom Sheet 无处不在作为函数添加,但是否可以将 Bottom Sheet 作为通用函数,以便我可以在其他地方重用它?

要使用普通 showModalBottomSheet 你需要像

一样传递电流 context
void appModalBottomSheet(BuildContext context) {
  showModalBottomSheet(
    context: context,
    builder: (context) {
      return Text("Common bottomSheet");
    },
  );
}

更多关于showModalBottomSheet

是的。您可以创建通用的 showModalBottomSheet 并将参数添加为视图的子项

 ///
 /// Show app modal bottomsheet
 ///
 Future<T?> showAppModalBottomSheet<T>({
   required BuildContext context,
   required Widget child,
   bool? isScrollControlled,
   ShapeBorder? shape,
   bool isDismissible = true,
 }) {
   return showModalBottomSheet<T>(
     isDismissible: isDismissible,
     context: context,
     elevation: AppConstants.modalElevation,
     barrierColor: AppColors.barrierColor,
     isScrollControlled: isScrollControlled ?? true,
     enableDrag: true,
     backgroundColor: AppColors.modalColor.ofContext(_context),
     shape: shape ?? AppConstants.modalShapeBorder,
     builder: (BuildContext context) {
       return child;
     },
   );
 }