我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
当前回答
Importcupertino_icons: ^0.1.2并编写以下代码:
showToast(BuildContext context, String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return CupertinoAlertDialog(
title: Text(
"Name of App",
),
content: Text(
message,
),
actions: [
CupertinoButton(
child: Text("OK"),
onPressed: () {
Navigator.of(context).pop();
},
)
],
);
},
);
});
其他回答
只需使用SnackBar(内容:文本(“hello”),)在任何事件如onTap和onPress。
你可以阅读更多关于展示小吃店的信息。
你可以使用FlutterToast之类的软件。
导入库:
fluttertoast: ^2.1.4
像下面这样使用它:
Fluttertoast.showToast(
msg: "Hello, World!",
textColor: Colors.white,
toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.indigo,
);
就是这样……
你可以用这个包装:吐司
将这一行添加到依赖项中
toast: ^0.1.5
然后这样使用它:
import 'package:toast/toast.dart';
Toast.show("Toast plugin app", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
你可以直接使用小吃店的元素
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text("Successfully!"),
behavior: SnackBarBehavior.floating,
margin: EdgeInsets.all(20),
shape: StadiumBorder(),
action: SnackBarAction(
label: 'Dismiss',
disabledTextColor: Colors.white,
textColor: Colors.blue,
onPressed: () {
//Do whatever you want
},
),
),
);
在Flutter中显示Toast消息非常简单:
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Toast Text Here"),
));