我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
我可以在Flutter中创建类似Toasts的东西吗?
只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。
当前回答
我想提供一个替代解决方案,以使用包刷新条。
正如包中所说:如果在通知用户时需要更多自定义,请使用此包。对于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);
其他回答
要显示Toast消息,您可以使用FlutterToast插件。要使用这个插件,你必须:
将此依赖项添加到您的pubspec中。Yaml文件:fluttertoast: ^8.0.8 要获取包,必须运行以下命令:$ flutter packages get 导入包:导入'package:fluttertoast/fluttertoast.dart';
像这样使用它:
Fluttertoast.showToast(
msg: "your message",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM // Also possible "TOP" and "CENTER"
backgroundColor: "#e74c3c",
textColor: '#ffffff');
要了解更多信息,请查看这个。
这很简单:
我们只需要安装颤动吐司包。 请参考以下文档: https://pub.dev/packages/fluttertoast
在安装选项卡,你会得到依赖,你必须把它粘贴到pubspec。Yaml文件然后安装。
在此之后,只需导入包:
import 'package:fluttertoast/fluttertoast.dart';
类似于上面的一行。
然后通过使用FlutterToast类你可以使用你的FlutterToast。
你做完了! !
颤振里没有任何烤面包的小部件。你可以去这个插件。
用例:
Fluttertoast.showToast(
msg: "My toast message",
textColor: Colors.white,
toastLength: Toast.LENGTH_SHORT,
timeInSecForIos: 1,
gravity: ToastGravity.BOTTOM,
backgroundColor: Colors.indigo,);
您可以使用飘动吐司包装。为此,将其添加到pubspec中。Yaml文件如下:
dependencies:
fluttertoast: ^8.0.8
然后在需要吐司的.dart文件中导入这个包并编写代码。
例如,参考以下代码:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
class ToastExample extends StatefulWidget {
@override
_ToastExampleState createState() {
return _ToastExampleState();
}
}
class _ToastExampleState extends State {
void showToast() {
Fluttertoast.showToast(
msg: 'This is flutterToast example', // Message
toastLength: Toast.LENGTH_SHORT, // toast length
gravity: ToastGravity.CENTER, // position
timeInSecForIos: 1, // duaration
backgroundColor: Colors.red, // background color
textColor: Colors.white // text color
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Toast Tutorial',
home: Scaffold(
appBar: AppBar(
title: Text('Toast Tutorial'),
),
body: Padding(
padding: EdgeInsets.all(15.0),
child: Center(
child: RaisedButton(
child: Text('Show Toast'),
onPressed: showToast,
),
),
)
),
);
}
}
void main() => runApp(ToastExample());
使用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
);