oktoast 2.3.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 81

oktoast #

oktoast pub package GitHub GitHub stars

A library for flutter.

A pure dart toast Library.

You can completely customize the style of toast.

中文博客介绍

Screenshot #

default pic

custom position and widget pic

gif:

pic

About version 1.X.X #

if you use oktoast 1.X.X ,Please use the 1.X branch, and read version readme.

Proposed migration to 2.X.X version. The new version does not require buildContext.

And you can completely customize the style of toast, because now you can use showToastWidget.

Usage #

1. Add library to your pubspec.yaml #

latest version: pub package

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

2. Import library in dart file #

import 'package:oktoast/oktoast.dart';

3. Wrap your app widget #

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

4. Call method showToast #

showToast("content");

// position and second have default value, is optional

showToastWidget(Text('hello oktoast'));

Explain #

There are two reasons why you need to wrap MaterialApp

  1. Because this ensures that toast can be displayed in front of all other controls
  2. Context can be cached so that it can be invoked anywhere without passing in context

Properties #

OKToast params #

oktoast have default style, and you also can custom style or other behavior.

nametypeneeddesc
childWidgetrequiredUsually Material App
textStyleTextStyleoptional
radiusdoubleoptional
backgroundColorColoroptionalbackroundColor
positionToastPositionoptional
dismissOtherOnShowbooloptionalIf true, other toasts will be dismissed. Default false.
movingOnWindowChangebooloptionalIf true, when the size changes, toast is moved. Default true.
textDirectionTextDirectionoptional
textPaddingEdgeInsetsGeometryoptionalOuter margin of text
textAlignTextAlignoptionalWhen the text wraps, the align of the text.
handleTouchbooloptionalDefault is false, if it's true, can responed use touch event.
animationBuilderOKToastAnimationBuilderoptionalAdd animation to show / hide toast.
animationDurationDurationoptionalThe duration of animation.
animationCurveCurveoptionalCurve of animation.
durationDurationoptionalDefault duration of toast.

Method showToast #

Display text on toast.

Description of params see OKToast.

nametypeneeddesc
msgStringrequiredText of toast.
contextBuildContextoptional
durationDurationoptional
positionToastPositionoptional
textStyleTextStyleoptional
textPaddingEdgeInsetsGeometryoptional
backgroundColorColoroptional
radiusdoubleoptional
onDismissFunctionoptional
textDirectionTextDirectionoptional
dismissOtherToastbooloptional
textAlignTextAlignoptional
animationBuilderOKToastAnimationBuilderoptional
animationDurationDurationoptional
animationCurveCurveoptional

Method showToastWidget #

Display custom widgets on toast

Description of params see showToast.

nametypeneeddesc
widgetWidgetrequiredThe widget you want to display.
contextBuildContextoptional
durationDurationoptional
positionToastPositionoptional
onDismissFunctionoptional
dismissOtherToastbooloptional
textDirectionTextDirectionoptional
handleTouchbooloptional
animationBuilderOKToastAnimationBuilderoptional
animationDurationDurationoptional
animationCurveCurveoptional

Method dismissAllToast #

Dismiss all toast.

Return value of showToast and showToastWidget #

about return type:
showToast and showToastWidget return type is ToastFuture, The ToastFuture can be use to dismiss the toast.

For all dismiss toast method #

An optional parameter showAnim is added to control whether fading animation is required for dismiss.

The praram default value is false.

Examples #

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'),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}


CHANGELOG #

Link

LICENSE #

Apache-2.0

Change log #

2.3.1 #

2.3.0 #

New feature:

  • Add animation for show/hide the oktoast.

2.2.1 #

Fix:

  • textAlign of method showToast.

2.2.0 #

New feature:

  • handleTouch: Caller can use this property to respond to click events.

Change:

  • This is a breaking change : The default value of dismissOtherToast of showToast is changed to better match the OKToast overall settings. .

2.1.9 #

Fix:

  • position is not valid in showToastWidget.

2.1.8 #

Add param showAnim param for dismiss method:

  • dismissAllToast
  • ToastManager.dismissAll
  • ToastFuture.dismiss

2.1.7 #

Add for OKToast:

  • textAlign
  • textPadding

Add for showToast:

  • textAlign

2.1.6 #

add textPadding for showToast method.

2.1.5 #

fix ToastPosition offset params

2.1.4 #

add textDirection params

2.1.3 #

When ui.window's size changes, toast is moved.

2.1.2 #

Now, OKToast add a params dismissOtherOnShow to dismiss other toast.

2.1.1 #

use manager to manage ToastFuture

add a method dismissAllToast to dismiss all toast.

add a param with showToast to dismiss other toast.

2.1.0 #

add new params to helper user listen toast dismiss

showToast and showToastWidget will return ToastFuture the toastFuture call dismiss() to dismiss toast.

2.0.1 #

oktoast support page level.

you can use oktoast wrap your Scaffold.

2.0.0 #

use BuildContext cache. then showToast not need context, you can use the showToast in anywhere.

1.0.4 #

support flutter sdk 0.10 ,fix bug

1.0.3 #

update the textAlign : TextAlign.center
update the overflow: TextOverflow.ellipsis,

1.0.2 #

fix defaultPosition is invalid.

[1.0.1+2] #

update readme

[1.0.1+1] #

update readme

[1.0.1] rename library name #

now libray is oktoast

[1.0.0] first release version #

stable version

[0.0.3] add the toast position #

the toast can set position

  1. showToast
  2. defaultPosition

[0.0.2] - add toast style #

add the textStyle radius backgroundColor

[0.0.1] - first version #

init commit

example/lib/main.dart

import 'dart:async';

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
      textStyle: TextStyle(fontSize: 19.0, color: Colors.white),
      backgroundColor: Colors.grey,
      radius: 10.0,
      child: MaterialApp(
        title: 'Demo for OKToast',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage(),
      ),
      animationCurve: Curves.easeIn,
      animationBuilder: Miui10AnimBuilder(),
      animationDuration: Duration(milliseconds: 200),
      duration: Duration(seconds: 3),
    );
  }
}

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++;

    setState(() {});
  }

  void _showToast() {
    // 3.1 use showToast method
    showToast(
      "$_counter",
      position: ToastPosition.bottom,
      backgroundColor: Colors.black.withOpacity(0.8),
      radius: 13.0,
      textStyle: TextStyle(fontSize: 18.0),
      animationBuilder: Miui10AnimBuilder(),
    );

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

    // 3.2 use showToastWidget method
    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: 2), () {
      toastFuture.dismiss(); // dismiss
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Example for OKToast"),
      ),
      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: Tooltip(
                    message: "Toast status when using this to test routing.",
                    child: RaisedButton(
                      child: Text("New page"),
                      onPressed: () {
                        Navigator.push(context,
                            MaterialPageRoute(builder: (ctx) => MyHomePage()));
                      },
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Tooltip(
                    message: "Add number.",
                    child: RaisedButton(
                      onPressed: _incrementCounter,
                      child: Text('Add'),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: Tooltip(
                    message: "Show toast.",
                    child: RaisedButton(
                      onPressed: _showToast,
                      child: Text('Toast'),
                    ),
                  ),
                ),
                TextField(
                  decoration: InputDecoration(
                      hintText: "Use TextField to test the toast of softkey."),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  oktoast: ^2.3.1

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:oktoast/oktoast.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
97
Health:
Code health derived from static analysis. [more]
55
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
81
Learn more about scoring.

We analyzed this package on May 14, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.1
  • pana: 0.13.8-dev
  • Flutter: 1.17.0

Health issues and suggestions

Fix lib/src/widget/overlay.dart. (-44.03 points)

Analysis of lib/src/widget/overlay.dart failed with 2 errors, 1 hint:

line 246 col 11: This requires the 'control-flow-collections' experiment to be enabled.

line 663 col 7: This requires the 'control-flow-collections' experiment to be enabled.

line 578 col 8: This method overrides a method annotated as '@mustCallSuper' in 'Element', but doesn't invoke the overridden method.

Fix lib/src/widget/oktoast.dart. (-1 points)

Analysis of lib/src/widget/oktoast.dart reported 2 hints:

line 104 col 22: 'Typography' is deprecated and shouldn't be used. The default Typography constructor defaults to the 2014 material design defaults. Applications are urged to migrate to Typography.material2018(), or, if the 2014 defaults are desired, to explicitly request them using Typography.material2014(). This feature was deprecated after v1.13.8..

line 108 col 26: 'body1' is deprecated and shouldn't be used. This is the term used in the 2014 version of material design. The modern term is bodyText2. This feature was deprecated after v1.13.8..

Fix lib/src/widget/theme.dart. (-0.50 points)

Analysis of lib/src/widget/theme.dart reported 1 hint:

line 55 col 15: 'inheritFromWidgetOfExactType' is deprecated and shouldn't be used. Use dependOnInheritedWidgetOfExactType instead. This feature was deprecated after v1.12.1..

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.28.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test