# Toast

Toast 是一个轻反馈组件。

# FLToastDefaults

FLToastDefaults 支持对 toast 视图的样式,位置,展示时间,黑暗模式以及其他行为进行配置。默认配置如下:

const FLToastDefaults({
    this.showDuration = const Duration(milliseconds: 1500),
    this.darkColor = Colors.white,
    this.darkBackgroundColor = Colors.black87,
    this.backgroundOpacity = 0.8,
    this.lightColor = const Color(0xFF2F2F2F),
    this.lightBackgroundColor = const Color(0xFFE0E0E0),
    this.position = FLToastPosition.center,
    this.style = FLToastStyle.dark,
    this.dismissOtherToast = true,
    this.hideWithTap = true,
    this.textDirection = TextDirection.ltr,
    this.topOffset = kToolbarHeight + 10,
    this.bottomOffset = 10,
});

# FLToastProvider

FLToastProvider 为子组件树提供了展示 toast 的能力,一般包裹在 MaterialApp 的 child 的外层。

class _MyAppState extends State<MyApp> {
  FLToastDefaults _toastDefaults = FLToastDefaults();
  
  
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'FLUI',
        theme: $YOUR_THEME
        routes: $ROUTES
        builder: (BuildContext context, Widget child) {
            return FLToastProvider(
                  defaults: _toastDefaults,
                  child: child
            );
        }
    );
}

# FLToast



展示 Toast 时则需要使用 FLToast 的类方法。

  • text
FLToast.text(text: 'Here is text');
/// or
FLToast.showText(text: 'Here is text', position: FLToastPosition.xxx, duration: Duration(seconds: xxx), style: FLToastStyle.xxx);
/// shortcut
FLToast.showText(text: 'Here is text');
  • loading
var dismiss = FLToast.loading(text: 'Loading...');
/// do something...
Future.delayed(Duration(seconds: 2), () {
    /// hide toast
    dismiss();
});
  • info, success & error
/// info
FLToast.info(text: 'Some info');
/// success
FLToast.success(text: 'Fetch success');
/// error
FLToast.error(text: 'Something was wrong');
  • custom content

GestureDetector(
  onTapDown: (TapDownDetails details) {
    final Widget mic = Container(
        padding: EdgeInsets.all(15),
        child: Image.asset('assets/record.gif', scale: 2));

    _hideCustomToast = FLToast.show(
        text: 'Swipe up to cancel sending',
        contentBuilder: (context) => mic);
  },
  onLongPressEnd: (LongPressEndDetails details) {
    if (_hideCustomToast != null) {
      _hideCustomToast();
      _hideCustomToast = null;
    }
  },
  child: ....
)

# dark mode



FLToastProvider(
    defaults: FLToastDefaults(style: FLToastStyle.light),
    ...
)
/// or show just once
FLToast.showText(text: 'Here is text', style: FLToastStyle.light);

# other position



FLToastProvider(
    defaults: FLToastDefaults(position: FLToastPosition.top),
    ...
)
/// or show just once
FLToast.showText(text: 'Here is text', position: FLToastPosition.top);

# API

# FLToastDefaults

属性 描述 类型 默认值
showDuration 显示时间长度 Duration Duration(milliseconds: 1500)
darkColor 深色背景下文字、icon 颜色 Color Colors.white
darkBackgroundColor 深色背景颜色 Color Colors.black87
backgroundOpacity 背景透明度 double 0.8
lightColor 浅色背景下文字、icon 颜色 Color Color(0xFF2F2F2F)
lightBackgroundColor 浅色背景颜色 Color Color(0xFFE0E0E0)
position toast 在容器中展示位置 FLToastPosition FLToastPosition.center
style 浅色还是深色风格 FLToastStyle FLToastStyle.dark
dismissOtherToast toast 展示时是否隐藏已显示的 toast bool true
hideWithTap 点击容器区域隐藏 toast bool true
textDirection 文字展示方向 TextDirection TextDirection.ltr
topOffset 位置为顶部时距离容器顶部的间距 double 66
bottomOffset 位置为底部时距离容器底部的间距 double 10

# FLToastProvider

属性 描述 类型 默认值
defaults 默认配置 FLToastDefaults FLToastDefaults()
child 子组件 Widget

# FLToast

方法列表:

  • loading({String text})

  • showLoading({String text, FLToastPosition position, FLToastStyle style})

  • show({String text, FLToastContentBuilder contentBuilder})

  • showToast({String text, FLToastContentBuilder contentBuilder, Duration duration, FLToastPosition position, FLToastStyle style})

  • text({String text})

  • showText({String text, Duration showDuration, FLToastPosition position, FLToastStyle style, VoidCallback onDismissed})

  • success({String text})

  • showSuccess({String text, Duration showDuration, FLToastPosition position, FLToastStyle style, VoidCallback onDismissed})

  • error({String text})

  • showError({String text, Duration showDuration, FLToastPosition position, FLToastStyle style, VoidCallback onDismissed})

  • info({String text})

  • showInfo({String text, Duration showDuration, FLToastPosition position, FLToastStyle style, VoidCallback onDismissed})


参数列表
参数 描述 类型 默认值
text toast 文字 String
position toast 位置 FLToastPosition defaults.position
style toast 风格 FLToastStyle defaults.style
duration 显示时长 Duration defaults.duration
onDismissed 隐藏后回调 VoidCallback

loading, showLoading, show, showToast 均有返回值,为用于隐藏 toast 的函数。