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

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


当前回答

用这个:

Fluttertoast.showToast(
    msg: "This is a Toast message",
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIos: 1
);

其他回答

将flutter_just_toast添加到Pubspecs中的依赖项中。yaml文件。

依赖关系:

flutter_just_toast: ^1.0.1

接下来将包导入到你的类中:

import 'package:flutter_just_toast/flutter_just_toast.dart';

用一条消息实现Toast:

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

您可以使用飘动吐司包装。为此,将其添加到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());

步骤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);

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

用例:

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

答案Scaffold.of(context). showsnackbar(…)在大多数情况下都不起作用。

我建议最佳的方法是在类中声明一个Scaffold state键,并将其分配给Scaffold,如下所示:

GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

然后

Scaffold(
  key: _scaffoldKey,
  ...
)

当你想要显示零食栏时,这样做:

_scaffoldKey.currentState.showSnackBar(SnackBar(
  content: Text("This works!"),
));