我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
当前回答
使用这个依赖:
toast: ^0.1.3
然后在页面中导入toast的依赖关系:
import 'package:toast/toast.dart';
然后在小部件的onTap()上:
Toast.show("Toast plugin app", context, duration:Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
其他回答
如果目前给出的Fluttertoast包不奏效,那么我建议你试试吐司。
它没有装饰,也没有仪式。
它就是有用。 我注意到在它的README文件中给出的例子中有一个错误: Toast.show ( “吐司插件应用程序”, 持续时间:烤面包。LENGTH_SHORT, 重力:Toast.BOTTOM); 而该方法需要一个上下文。所以要像这样添加“上下文”: Toast.show ( "Toast插件应用" 持续时间:烤面包。LENGTH_SHORT, 重力:Toast.BOTTOM); 有一个机会,这将在你检查的时候被修复。我已经提交了PR。
在这里买颤振吐司包
将这个包添加到文件pubspec.yaml中的项目依赖项中。
然后当你想要吐司显示时,就像点击一个按钮:
Toast.show("Toast plugin app", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
答案Scaffold.of(context). showsnackbar(…)在大多数情况下都不起作用。
我建议最佳的方法是在类中声明一个Scaffold state键,并将其分配给Scaffold,如下所示:
GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();
然后
Scaffold(
key: _scaffoldKey,
...
)
当你想要显示零食栏时,这样做:
_scaffoldKey.currentState.showSnackBar(SnackBar(
content: Text("This works!"),
));
我想提供一个替代解决方案,以使用包刷新条。
正如包中所说:如果在通知用户时需要更多自定义,请使用此包。对于Android开发人员来说,它可以替代吐司和零食条。
使用flushbar的另一个建议是如何在Flutter的navigator.pop(context)之后显示小吃条?
你也可以设置flushbarPosition为TOP或BOTTOM:
Flushbar(
title: "Hey Ninja",
message: "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
flushbarPosition: FlushbarPosition.TOP,
flushbarStyle: FlushbarStyle.FLOATING,
reverseAnimationCurve: Curves.decelerate,
forwardAnimationCurve: Curves.elasticOut,
backgroundColor: Colors.red,
boxShadows: [BoxShadow(color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)],
backgroundGradient: LinearGradient(colors: [Colors.blueGrey, Colors.black]),
isDismissible: false,
duration: Duration(seconds: 4),
icon: Icon(
Icons.check,
color: Colors.greenAccent,
),
mainButton: FlatButton(
onPressed: () {},
child: Text(
"CLAP",
style: TextStyle(color: Colors.amber),
),
),
showProgressIndicator: true,
progressIndicatorBackgroundColor: Colors.blueGrey,
titleText: Text(
"Hello Hero",
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.yellow[600], fontFamily: "ShadowsIntoLightTwo"),
),
messageText: Text(
"You killed that giant monster in the city. Congratulations!",
style: TextStyle(fontSize: 18.0, color: Colors.green, fontFamily: "ShadowsIntoLightTwo"),
),
)..show(context);
使用fluttertoast插件
将这一行添加到依赖项中
fluttertoast: ^8.1.1
然后你可以使用Toast无构建上下文(功能有限,无法控制UI,请检查文档)
Fluttertoast.showToast(
msg: "This is a Toast message",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.CENTER,
timeInSecForIosWeb: 1,
textColor: Colors.white,
fontSize: 16.0
);