我可以在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);

其他回答

如果目前给出的Fluttertoast包不奏效,那么我建议你试试吐司。

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

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

你可以使用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,
);

就是这样……

在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编写本地代码,然后将其转换为插件,你就可以开始了。

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

颤振里没有任何烤面包的小部件。你可以去这个插件。

用例:

Fluttertoast.showToast(
    msg: "My toast message",
    textColor: Colors.white,
    toastLength: Toast.LENGTH_SHORT,
    timeInSecForIos: 1,
    gravity: ToastGravity.BOTTOM,
    backgroundColor: Colors.indigo,);

只需使用SnackBar(内容:文本(“hello”),)在任何事件如onTap和onPress。

你可以阅读更多关于展示小吃店的信息。