import 'package:flutter/material.dart';
Future Function() openBottomSheet(BuildContext context, Widget child,
{String? title}) {
return () => showModalBottomSheet(
// Don't let bottom sheet extend past the top of the safe area
useSafeArea: true,
context: context,
// Round the top of the bottom sheet
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
// Add scrollbar when necessary
isScrollControlled: true,
builder: (context) => ScrollableWidget(
child: Container(
// Move bottom sheet above on-screen keyboard, if keyboard is open
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).viewInsets.bottom),
child: Stack(
children: [
// Add back button at top left of bottom sheet (since it's
// not obvious that sheet can be swiped down to close it)
const BackButton(),
Padding(
// Pad the main widget (larger padding on the top to leave
// space for the back button)
padding: const EdgeInsets.fromLTRB(20.0, 35.0, 20.0, 25.0),
child: Column(
children: [
// Make content full-width, so that main widget is
// centered even if it doesn't expand
Row(
mainAxisSize: MainAxisSize.max,
children: const [SizedBox(height: 0)],
),
// Add title text to top of bottom sheet, if provided
title == null
? Container()
: Column(
children: [
Text(
title,
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18),
textAlign: TextAlign.center,
),
const SizedBox(height: 8),
],
),
// Add the main widget
child,
],
),
),
],
),
),
),
);
}
/// A scrollable widget with a scrollbar that is shown when necessary
class ScrollableWidget extends StatefulWidget {
const ScrollableWidget({super.key, required this.child});
final Widget child;
@override
State<ScrollableWidget> createState() => _ScrollableWidgetState();
}
class _ScrollableWidgetState extends State<ScrollableWidget> {
final controller = ScrollController();
@override
Widget build(BuildContext context) {
return Scrollbar(
thumbVisibility: true,
thickness: 15,
radius: const Radius.circular(8),
controller: controller,
child: SingleChildScrollView(
// Same controller must be used on Scrollbar and SingleChildScrollView
controller: controller,
child: widget.child,
),
);
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
}
9条答案
按热度按时间s2j5cfk01#
作为一个选项,你可以修改底部表单1.创建新文件custom_bottom_sheet.dart 2.复制粘贴所有代码从bottom_sheet类到你的定制类3.修改buildPage()方法,如下所示
1.使用你的类
此处为修改后的bottom_sheet类https://pastebin.com/5U7fsqCw
我还认为您需要添加属性
防止状态栏变暗
yzxexxkh2#
如果你看一下
SafeArea
的源代码,这就是正在发生的事情。确保你的构建在小部件树的"根"级,因为后代小部件可能没有顶部填充,因为它们不在边缘之下。jrcvhitl3#
自2022年7月20日起,您可以将
useSafeArea
参数设置为true
,以在状态栏you can find details here下显示模态。注:现在仅在
master
通道上可用,以后将在stable
通道上可用。wf82jlnq4#
另一个简单的圆边框解决方案
);
mzillmmw5#
我认为这个解决方案最适合现在:(使用
FractionallySizedBox
)6yoyoihd6#
您可以使用约束参数并设置所需的高度
cvxl0en27#
最好和最简单的方法是从状态栏高度中减去设备的总高度。您可以通过MediaQuery.of(context). www.example.com获得statusBarHeightviewPadding.top,通过MediaQuery.of(context).size.height获得整个设备的高度
uwopmtnx8#
就像@Roddy R的回答,只是简单一点
谢谢,罗迪!
jucafojl9#
正如AnasSafi的回答所说,使用
useSafeArea: true
来解决这个问题。然而,在创建底部表单时,还有许多其他棘手的问题需要解决。下面是我完整的工作底部表单创建器函数,希望能对大家有所帮助。