【发布时间】:2018-10-26 19:43:48
【问题描述】:
【问题讨论】:
【问题讨论】:
对于持久性底页,在材质应用小部件的主题属性中添加以下代码
ThemeData(
bottomSheetTheme: BottomSheetThemeData(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
),
),
【讨论】:
showModalBottomSheet(
context:context
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
clipBehavior: Clip.antiAliasWithSaveLayer,
)
【讨论】:
您也可以在装饰容器中提供borderRadius。
showModalBottomSheet(
context: context,
builder: (builder){
return Container(
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.only(
topLeft: const Radius.circular(15.0),
topRight: const Radius.circular(15.0))),
child: Center(
child: Text("This is a modal sheet"),
),
);
}
);
【讨论】:
您可以在showModalBottomSheet 内添加RoundedRectangleBorder 小部件:
showModalBottomSheet<void>(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0)
),
),
)
【讨论】:
我有这个代码,对我来说很好用。请检查一下,让我知道你的意见。
showBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(
top: Radius.circular(20),
),
),
context: context,
builder: (context) => Container(
height: 250,
child: new Container(
decoration: new BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(20.0),
topRight: const Radius.circular(20.0))),
child: new Center(
child: new Text("This is a modal sheet"),
)),
))
【讨论】:
我认为做圆角模态的最好方法是使用RoundedRectangleBorder 和垂直BorderRadius,只设置它的top 属性:
showModalBottomSheet(
context: context,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.vertical(top: Radius.circular(25.0)),
),
builder: (BuildContext context) {
// return your layout
});
对左上角和右上角使用单独的半径有点冗长且容易出错。
【讨论】:
【讨论】:
showModalBottomSheet(
backgroundColor: Colors.transparent,
context: context,
builder: (ctx) {
return Container(
decoration: BoxDecoration(
color: Colors.green, // or some other color
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0)
)
);
});
【讨论】:
把之前所有的答案放在一起,我可以达到最好的结果(在我看来)。
showModalBottomSheet(
context: context,
backgroundColor: Colors.white,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.only(topLeft: Radius.circular(15.0), topRight: Radius.circular(15.0)),
),
builder: (context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
ListTile(
leading: Icon(Icons.email),
title: Text('Send email'),
onTap: () {
print('Send email');
},
),
ListTile(
leading: Icon(Icons.phone),
title: Text('Call phone'),
onTap: () {
print('Call phone');
},
),
],
);
});
【讨论】:
您现在可以使用默认的showModalBottomSheet 方法来完成此操作,该方法现在支持添加ShapeBorder 和backgroundColor!
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10.0),
),
backgroundColor: Colors.white,
...
);
我没有像其他答案所建议的那样覆盖应用程序的整个主题(这导致我的应用程序的各个部分出现问题),而是决定查看showModalBottomSheet 的实现并自己找出问题所在。事实证明,所需要的只是将模式的主要代码包装在包含 canvasColor: Colors.transparent 技巧的 Theme 小部件中。我还让自定义半径和模态本身的颜色变得更加容易。
您可以在 pub 上使用 package 或包含相同代码的 gist。不要忘记导入正确的包/文件。
showRoundedModalBottomSheet(
context: context,
radius: 20.0, // This is the default
color: Colors.white, // Also default
builder: (context) => ???,
);
【讨论】:
@override Widget build(BuildContext context) { return Theme( // We need this theme override so that the corners of the bottom sheet modal can be rounded data: ThemeData(canvasColor: Colors.transparent), child: Scaffold( ....
您现在可以简单地设置shape 参数。
示例:
showModalBottomSheet(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
),
context: context,
builder: (context) => MyBottomSheet(),
);
【讨论】:
这是需要的modalBottomSheet函数。
void _modalBottomSheetMenu(){
showModalBottomSheet(
context: context,
builder: (builder){
return new Container(
height: 350.0,
color: Colors.transparent, //could change this to Color(0xFF737373),
//so you don't have to change MaterialApp canvasColor
child: new Container(
decoration: new BoxDecoration(
color: Colors.white,
borderRadius: new BorderRadius.only(
topLeft: const Radius.circular(10.0),
topRight: const Radius.circular(10.0))),
child: new Center(
child: new Text("This is a modal sheet"),
)),
);
}
);
}
这个正常工作的最重要部分是,在 MaterialApp 中将 canvasColor 设置为透明,如下所示。
return new MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Tasks',
theme: new ThemeData(
primarySwatch: Colors.teal,
canvasColor: Colors.transparent,
),
home: new TasksHomePage(),
);
}
我已经测试了代码,它运行良好,因为我还在克隆 Google Tasks 应用程序,该应用程序将在我的github 中开源。
【讨论】:
canvasColor 更改为 bottomSheet 圆角并不是一个好的折衷方案。改用 Theme 包装器。此外,如果底部工作表内的小部件取决于主题,则使用 data: yourTheme().copyWith(canvasColor: Colors.transparent) 仅将画布颜色应用于其子项。最好的选择是更新的。
bottomSheetTheme: BottomSheetThemeData(backgroundColor: Colors.transparent)。