Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Toast library for Flutter.

License

NotificationsYou must be signed in to change notification settings

OpenFlutter/flutter_oktoast

Repository files navigation

oktoastpub packageGitHubGitHub stars

A library for flutter.

A pure dart toast Library.

You can completely customize the style of toast.

中文博客介绍

Screenshot

DefaultCustomGIF
picpicpic

Versions

3.x.x

Starting from the 3.x version, OKToast provides a null-safety version,the specific introduction of null-safety can be viewed indart orflutter.

The 2.3.2 version is the last version that does not support null-safety.

About version 1.x

if you use OKToast 1.x, Please use the 1.x branch, and read version readme.

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

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

Usage

1. Add library to your pubspec.yaml

flutter pub add oktoast

2. Import library in dart file

import'package:oktoast/oktoast.dart';

3. Wrap your app widget

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

Tips:If you happened error like:No MediaQuery widget found,you can try to use thiscodeto includeOKToast to your App.

MaterialApp(  builder: (BuildContext context,Widget? widget) {returnOKToast(child: widget);  },);

4. Call methodshowToast

showToast('content');// position and second have default value, is optionalshowToastWidget(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.

MethodshowToast

Display text on toast.

Description of params seeOKToast.

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

MethodshowToastWidget

Display custom widgets on toast

Description of params seeshowToast.

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

MethoddismissAllToast

Dismiss all toast.

Return value ofshowToast andshowToastWidget

about return type:
showToast andshowToastWidget return type isToastFuture,TheToastFuture can be use to dismiss the toast.

For all dismiss toast method

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

The praram default value isfalse.

Examples

import'package:flutter/material.dart';import'package:oktoast/oktoast.dart';// 1. import libraryvoidmain()=>runApp(MyApp());classMyAppextendsStatelessWidget {@overrideWidgetbuild(BuildContext context) {returnOKToast(// 2. wrap your app with OKToast      child:MaterialApp(        title:'Flutter Demo',        theme:ThemeData(          primarySwatch:Colors.blue,        ),        home:MyHomePage(),      ),    );  }}classMyHomePageextendsStatefulWidget {MyHomePage({Key key}):super(key: key);@override_MyHomePageStatecreateState()=>_MyHomePageState();}class_MyHomePageStateextendsState<MyHomePage> {int _counter=0;void_incrementCounter() {    _counter++;// 3.1 use showToast methodshowToast("$_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 widgetWidget 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 futureFuture.delayed(Duration(seconds:1), () {      toastFuture.dismiss();// dismiss    });setState(() {    });  }@overrideWidgetbuild(BuildContext context) {returnScaffold(      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:constEdgeInsets.all(8.0),                  child:RaisedButton(                    onPressed: () {Navigator.push(context,MaterialPageRoute(builder: (ctx)=>MyHomePage()));                    },                  ),                ),Padding(                  padding:constEdgeInsets.all(8.0),                  child:RaisedButton(                    onPressed: _incrementCounter,                    child:Text('toast'),                  ),                ),              ],            ),          ),        ],      ),    );  }}

CHANGELOG

Link

LICENSE

Apache-2.0

Packages

No packages published

Contributors10


[8]ページ先頭

©2009-2025 Movatter.jp