oktoast

一个纯Dart的Toast库。

您可以完全自定义提示的样式。

20181207161700

20181207161742

ktoast2

版本

3.x.x

从3.x版本开始,oktoast提供了null-safety版本,null-safety的具体介绍可以查看 dartflutter

2.3.2版本是不支持null-safety的最后一个版本。

关于1.X.X版本

如果您使用oktoast 1.X.X,请使用1.X分支,并阅读版本自述文件。

建议迁移到2.X.X版本。新版本不需要BuildContext。

而且您可以完全自定义Toast的样式,因为现在您可以使用showToastWidget

用法

1.将库添加到pubspec.yaml

最新版本: pub package

dependencies:
  oktoast: ^3.0.0 # such as version, you need use the latest version of pub.

2.在dart文件中导入库

import 'package:oktoast/oktoast.dart';

3.包装您的应用Widget

OKToast(
  /// set toast style, optional
  child:MaterialApp()
);

提示
如果您遇到类似这样的错误: No MediaQuery widget found

您可以尝试使用下面的 代码oktoast包含到您的App中。

MaterialApp(builder: (context, widget) {
  return OKToast(
    child: widget,
  );
});

4.调用方法showToast

showToast("content");

// position and second have default value, is optional

showToastWidget(Text('hello oktoast'));

解释

您需要包装MaterialApp的两个原因

  1. 因为这可以确保Toast显示在所有其他控件的最前面
  2. Context可以被缓存,因此可以在任何地方调用而无需传递context

属性

OKToast参数

oktoast具有默认样式,您也可以自定义样式或其他行为。

名称 类型 需要 描述
child Widget 必需 通常是Material App
文本样式 TextStyle 可选
radius 双精度 可选
backgroundColor 颜色 可选 背景颜色
position Toast位置 可选
dismissOtherOnShow 布尔值 可选 如果为true,则会关闭其他Toast。默认为false。
movingOnWindowChange 布尔值 可选 如果为true,则在窗口大小改变时,Toast会移动。默认为true。
textDirection 文本方向 可选
textPadding EdgeInsetsGeometry 可选 文本的外边距
textAlign TextAlign 可选 当文本换行时,文本的对齐方式。
handleTouch 布尔值 可选 默认为false,如果为true,则可以响应触摸事件。
animationBuilder OKToastAnimationBuilder 可选 为Toast的显示/隐藏添加动画。
animationDuration 持续时间 可选 动画的时长。
animationCurve Curve 可选 动画的曲线。
duration 持续时间 可选 Toast的默认时长。

方法showToast

在Toast上显示文本。

参数说明请参阅OKToast

名称 类型 需要 描述
msg 字符串 必需 Toast的文本。
context BuildContext 可选
duration 持续时间 可选
position Toast位置 可选
文本样式 TextStyle 可选
textPadding EdgeInsetsGeometry 可选
backgroundColor 颜色 可选
radius 双精度 可选
onDismiss 功能 可选
textDirection 文本方向 可选
dismissOtherToast 布尔值 可选
textAlign TextAlign 可选
animationBuilder OKToastAnimationBuilder 可选
animationDuration 持续时间 可选
animationCurve Curve 可选

方法showToastWidget

在Toast上显示自定义Widget

参数说明请参阅showToast

名称 类型 需要 描述
widget Widget 必需 您想显示的Widget。
context BuildContext 可选
duration 持续时间 可选
position Toast位置 可选
onDismiss 功能 可选
dismissOtherToast 布尔值 可选
textDirection 文本方向 可选
handleTouch 布尔值 可选
animationBuilder OKToastAnimationBuilder 可选
animationDuration 持续时间 可选
animationCurve Curve 可选

方法dismissAllToast

关闭所有Toast。

showToastshowToastWidget的返回值

关于返回类型
showToastshowToastWidget的返回类型是ToastFuture
ToastFuture可用于关闭Toast。

所有关闭Toast的方法

添加了可选参数showAnim来控制关闭时是否需要淡入动画。

该参数的默认值为false

示例

import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart'; // 1. import library

void main() => runApp( MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return OKToast(
      //2. wrap your app with OKToast
      child:  MaterialApp(
        title: 'Flutter Demo',
        theme:  ThemeData(
          primarySwatch: Colors.blue,
        ),
        home:  MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() =>  _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    _counter++;

    // 3.1 use showToast method
    showToast(
      "$_counter",
      duration: Duration(seconds: 2),
      position: ToastPosition.bottom,
      backgroundColor: Colors.black.withOpacity(0.8),
      radius: 13.0,
      textStyle: TextStyle(fontSize: 18.0),
    );

    showToast(
      "$_counter",
      duration: Duration(seconds: 2),
      position: ToastPosition.top,
      backgroundColor: Colors.black.withOpacity(0.8),
      radius: 3.0,
      textStyle: TextStyle(fontSize: 30.0),
    );

    // 3.2 use showToastWidget method to custom widget
    Widget widget = Center(
      child: ClipRRect(
        borderRadius: BorderRadius.circular(30.0),
        child: Container(
          width: 40.0,
          height: 40.0,
           color: Colors.grey.withOpacity(0.3),
          child: Icon(
            Icons.add,
            size: 30.0,
            color: Colors.green,
          ),
        ),
      ),
    );

    ToastFuture toastFuture = showToastWidget(
      widget,
      duration: Duration(seconds: 3),
      onDismiss: () {
        print("the toast dismiss"); // the method will be called on toast dismiss.
      },
    );

    // can use future
    Future.delayed(Duration(seconds: 1), () {
      toastFuture.dismiss(); // dismiss
    });

    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      appBar:  AppBar(
        title:  Text("ktoast demo"),
      ),
      body: Stack(
        children: <Widget>[
           Center(
            child: ListView(
              children: <Widget>[
                 Text(
                  'You have pushed the button this many times:',
                ),
                 Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.display1,
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: RaisedButton(
                    onPressed: () {
                      Navigator.push(context,
                          MaterialPageRoute(builder: (ctx) => MyHomePage()));
                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: RaisedButton(
                    onPressed: _incrementCounter,
                    child: Text('toast'),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}


GitHub

https://github.com/OpenFlutter/flutter_oktoast