我可以在Flutter中创建类似Toasts的东西吗?

只是一个很小的通知窗口,不直接面对用户,也不锁定或淡出它后面的视图。


当前回答

在Flutter应用程序中有一个三种方式来显示吐司。

我会告诉你我所知道的三种方法,并选择你想要使用的一种。我推荐第二种。

1:使用外挂包。

这是第一个方法,这是最简单的方法显示吐司在Flutter应用程序。

首先,您必须将这个包添加到文件pubspec中。YAML:

flutter_just_toast:^version_here

然后在您想要显示toast的文件中导入该包。

'package:flutter_just_toast/flutter_just_toast.dart';

最后一步是祝酒词。

Toast.show(message: "Your toast message",
           duration: Delay.SHORT,
           textColor: Colors.black);

2:使用官方的方式。

这种方法也很简单,但你必须处理它。我并不是说它很难,而是简单干净,我会推荐这种方法。

对于这个方法,你所要做的就是使用下面的代码。

Scaffold.of(context).showSnackBar(SnackBar(
          content: Text("Sending Message"),
        ));

但是请记住,您必须使用脚手架上下文。

3:使用本地API。

现在,当你已经有了上面的两个方法时,这个方法就没有意义了。使用这种方法,你必须为Android和iOS编写本地代码,然后将其转换为插件,你就可以开始了。

这种方法会消耗你的时间,你必须重新发明轮子。这已经被发明出来了。

其他回答

使用这个依赖:

toast: ^0.1.3

然后在页面中导入toast的依赖关系:

import 'package:toast/toast.dart';

然后在小部件的onTap()上:

Toast.show("Toast plugin app", context, duration:Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);

步骤1:

依赖关系:

flutter_just_toast: ^1.0.1

步骤2:

import 'package:flutter_just_toast/flutter_just_toast.dart';

步骤3:

Toast.show(
  message: "Your toast message",
  duration: Delay.SHORT,
  textColor: Colors.black);

这很简单:

我们只需要安装颤动吐司包。 请参考以下文档: https://pub.dev/packages/fluttertoast

在安装选项卡,你会得到依赖,你必须把它粘贴到pubspec。Yaml文件然后安装。

在此之后,只需导入包:

import 'package:fluttertoast/fluttertoast.dart';

类似于上面的一行。

然后通过使用FlutterToast类你可以使用你的FlutterToast。

你做完了! !

我想提供一个替代解决方案,以使用包刷新条。

正如包中所说:如果在通知用户时需要更多自定义,请使用此包。对于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包不奏效,那么我建议你试试吐司。

它没有装饰,也没有仪式。

它就是有用。 我注意到在它的README文件中给出的例子中有一个错误: Toast.show ( “吐司插件应用程序”, 持续时间:烤面包。LENGTH_SHORT, 重力:Toast.BOTTOM); 而该方法需要一个上下文。所以要像这样添加“上下文”: Toast.show ( "Toast插件应用" 持续时间:烤面包。LENGTH_SHORT, 重力:Toast.BOTTOM); 有一个机会,这将在你检查的时候被修复。我已经提交了PR。