一、引入第三方依赖

fluttertoast
fluttertoast | Flutter Package

image.png

Use this package as a library

Depend on it

Run this command:
With Flutter:

$ flutter pub add fluttertoast

This will add a line like this to your package’s pubspec.yaml (and run an implicit flutter pub get):

dependencies: fluttertoast: ^8.0.8

Alternatively, your editor might support or flutter pub get. Check the docs for your editor to learn more.

Import it

Now in your Dart code, you can use:

import ‘package:fluttertoast/fluttertoast.dart’;

二、使用组件

How to Use

# add this line to your dependencies fluttertoast: ^8.0.8

import ‘package:fluttertoast/fluttertoast.dart’;

  1. Fluttertoast.showToast(
  2. msg: "Hello,World",
  3. toastLength: Toast.LENGTH_SHORT,
  4. gravity: ToastGravity.CENTER,
  5. backgroundColor: Color.fromRGBO(238, 162, 164, 1),
  6. fontSize: 16.0
  7. );

效果图:
image.png

To cancel all the toasts call

Fluttertoast.cancel()

三、常见属性

property description default
msg String (Not Null)(required) required
toastLength Toast.LENGTH_SHORT or Toast.LENGTH_LONG (optional) Toast.LENGTH_SHORT
gravity ToastGravity.TOP (or) ToastGravity.CENTER (or) ToastGravity.BOTTOM (Web Only supports top, bottom) ToastGravity.BOTTOM
timeInSecForIosWeb int (for ios & web) 1 (sec)
backgroundColor Colors.red null
textcolor Colors.white null
fontSize 16.0 (float) null
webShowClose false (bool) false
webBgColor String (hex Color) linear-gradient(to right, #00b09b, #96c93d)
webPosition String (left, center or right) right