【发布时间】:2023-01-04 15:45:26
【问题描述】:
【问题讨论】:
-
改用吐司应该更适合你。由于您分享的图像看起来像吐司消息而不是小吃店。
-
这是一个具有自定义颜色和宽度的小吃店。
标签: flutter flutter-layout snackbar
【问题讨论】:
标签: flutter flutter-layout snackbar
您可以使用一个非常有用的包 - fluttertoast。
简单地写 -
Fluttertoast.showToast(
msg: "This is Center Short Toast",
);
您可以添加 toastLength 使其变短或变长 -
Fluttertoast.showToast(
msg: "This is Center Short Toast",
toastLength: Toast.LENGTH_SHORT,
);
希望这可以帮助。让我知道。
【讨论】:
其他解决方案是使用oktoast,我们可以用位置参数改变位置
showToast(
"$_counter",
duration: Duration(seconds: 2),
position: ToastPosition.bottom,
backgroundColor: Colors.black.withOpacity(0.8),
radius: 13.0,
textStyle: TextStyle(fontSize: 18.0),
);
【讨论】:
诀窍是在这里我使用透明背景。您可以创建一个传递上下文和大小的方法。
showMySnackBar({
required BuildContext context,
required double width,
double? height,
}) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.transparent,
elevation: 0,
padding: EdgeInsets.zero,
content: Column(
mainAxisSize: MainAxisSize.min, // needed for flexible height
children: [
Container(
alignment: Alignment.center,
width: width,
height: height,
color: Colors.green,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text("MY snack"),
],
),
),
],
),
),
);
}
并使用
showMySnackBar(context: context, width: 400);
【讨论】: