303

Can I create something similar to Toasts in Flutter?

enter image description here

Just a tiny notification window that is not directly in the face of the user and does not lock or fade the view behind it.

Shady Aziza
  • 50,824
  • 20
  • 115
  • 113

32 Answers32

415

UPDATE: Scaffold.of(context).showSnackBar is deprecated in Flutter 2.0.0 (stable)

You can access the parent ScaffoldMessengerState using ScaffoldMessenger.of(context).

Then do something like

ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
     content: Text("Sending Message"),
));

Snackbars are the official "Toast" from material design. See Snackbars.

Here is a fully working example:

Enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatelessWidget {
  const Home({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Snack bar'),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () => _showToast(context),
          child: const Text('Show toast'),
        ),
      ),
    );
  }

  void _showToast(BuildContext context) {
    final scaffold = ScaffoldMessenger.of(context);
    scaffold.showSnackBar(
      SnackBar(
        content: const Text('Added to favorite'),
        action: SnackBarAction(label: 'UNDO', onPressed: scaffold.hideCurrentSnackBar),
      ),
    );
  }
}
Almas Adilbek
  • 4,371
  • 10
  • 58
  • 97
Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • 4
    How should this be wrapped inside an onPressed for example ? because I have tried it and nothing shows up on the screen. – Shady Aziza Aug 29 '17 at 21:41
  • 1
    Well, I never fit the mould and I know "official" doesn't mean absolute!! The Snackbar really sucks for my app UX so I stay well clear of it and always implement the older floating popup style notification. I feel it's very easy for users to miss Snackbar notifications (especially if they're not used to it); but it's not easy to miss a popup floating in the middle, top or bottom of the screen. – SilSur Sep 01 '19 at 11:43
  • 1
    @aziza wrap you button widget in Builder(), it will work – Reza Mojed May 19 '20 at 07:46
  • 2
    The widget calling `showSnackBar()` should have a `Scaffold` parent. – Lahiru Chandima Jun 02 '20 at 09:21
  • 4
    Note: If you are using Snackbar solution as mentioned in the answer and want to have something closer to traditional Android Toast view, use floating behavior in Snackbar like -> "behavior: SnackBarBehavior.floating". It will detach Snackbar from bottom and you will have something closer to traditional toast. – Eren Oct 16 '20 at 17:02
  • 2
    Getting error - 'showSnackBar' is deprecated and shouldn't be used. Use ScaffoldMessenger.showSnackBar. This feature was deprecated after v1.23.0-14.0.pre.. Try replacing the use of the deprecated member with the replacement. – Kamlesh Feb 01 '21 at 06:19
  • 2
    This is now deprecated! The new version is `ScaffoldMessage.showSnackBar()`. – Karolina Hagegård Apr 04 '21 at 08:52
  • It shows error like `The following assertion was thrown during a scheduler callback: There are multiple heroes that share the same tag within a subtree. Within each subtree for which heroes are to be animated (i.e. a PageRoute subtree), each Hero must have a unique non-null tag. In this case, multiple heroes had the following tag: Here is the subtree for one of the offending heroes: Hero tag: state: _HeroState when snackbar message is being displayed and user clicks on another button or icon to navigate – Kamlesh Jul 25 '21 at 14:03
  • @remi Remi - Kindly see my last message and suggest me how can we fix this issue? Thanks a lot. – Kamlesh Jul 25 '21 at 14:03
  • Nothing works for me but this one – Noor Hossain Oct 01 '22 at 21:05
155

Use fluttertoast plugin

add this line to your dependencies

fluttertoast: ^8.1.1

Then you can use Toast with No Build Context (has limited features and no control over UI, please check the doc)

Fluttertoast.showToast(
        msg: "This is a Toast message",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        textColor: Colors.white,
        fontSize: 16.0
    );

enter image description here

Zhar
  • 3,330
  • 2
  • 24
  • 25
Raouf Rahiche
  • 28,948
  • 10
  • 85
  • 77
  • 6
    You have to first add the Fluttertoast dependecy in the pubspec.yaml file. Link to the dependency is here [link] (https://pub.dartlang.org/packages/fluttertoast). Then you can use the above code – fritz-playmaker Aug 15 '18 at 15:06
  • 4
    `Unhandled Exception: MissingPluginException(No implementation found for method showToast on channel PonnamKarthik/fluttertoast)` – Robin Feb 01 '20 at 10:01
  • 1
    Now it works, i need to stop the app and Run ‍♂️ without debugging :) – Robin Feb 01 '20 at 10:06
  • Anyone faced the issue that the round border is disabled after setting bg color https://github.com/PonnamKarthik/FlutterToast/issues/156 – thanhbinh84 Apr 21 '20 at 03:29
  • I just added a custom motion toast. Awesome! https://pub.flutter-io.cn/packages/motion_toast – Bhikkhu Subhuti Aug 19 '21 at 04:12
  • @SachinSolanki You need to run in production mode to be able to see it flutter run --release – Shareef Dweikat Oct 12 '21 at 18:54
  • it has some error in web and some deprecation warning in android like this"FlutterToastPlugin.kt:9:48: warning: 'Registrar' is deprecated. Deprecated in Java ... Unit' is deprecated. Deprecated in Java" – MrSalesi Aug 29 '23 at 18:06
86

SnackBar is definitely the right class to use, as pointed out by Darky.

Snackbar

One tricky thing about showSnackBar is getting to the ScaffoldState, if you're trying to call showSnackBar within the build method where you construct your Scaffold.

You might see an error like this, which includes some text explaining how to solve the problem.

══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
Scaffold.of() called with a context that does not contain a Scaffold.
No Scaffold ancestor could be found starting from the context that was passed to Scaffold.of(). This
usually happens when the context provided is from the same StatefulWidget as that whose build
function actually creates the Scaffold widget being sought.
There are several ways to avoid this problem. The simplest is to use a Builder to get a context that
is "under" the Scaffold. For an example of this, please see the documentation for Scaffold.of():
  https://docs.flutter.io/flutter/material/Scaffold/of.html
A more efficient solution is to split your build function into several widgets. This introduces a
new context from which you can obtain the Scaffold. In this solution, you would have an outer widget
that creates the Scaffold populated by instances of your new inner widgets, and then in these inner
widgets you would use Scaffold.of().
A less elegant but more expedient solution is assign a GlobalKey to the Scaffold, then use the
key.currentState property to obtain the ScaffoldState rather than using the Scaffold.of() function.
The context used was:
  MyHomePage
When the exception was thrown, this was the stack:
#0      Scaffold.of (package:flutter/src/material/scaffold.dart:444:5)
#1      MyHomePage.build.<anonymous closure> (/Users/jackson/Library/Developer/CoreSimulator/Devices/7072C907-DBAD-44FE-8F40-0257442C51D9/data/Containers/Data/Application/77FEC1A4-1453-442C-8208-96E0323DEFB2/tmp/so_scratch2Tkq9Jb/so_scratch2/lib/main.dart:23:24)
#2      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:323:14)
#3      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:375:30)
#4      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
#5      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:149:9)
#6      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:119:7)
#7      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
#8      BindingBase&SchedulerBinding&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
#9      BindingBase&SchedulerBinding&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
#10     BindingBase&SchedulerBinding&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
#11     BindingBase&SchedulerBinding&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
#12     BindingBase&SchedulerBinding&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
#13     _invoke1 (file:///b/build/slave/Mac_Engine/build/src/flutter/lib/ui/hooks.dart:100)
#14     _dispatchPointerDataPacket (file:///b/build/slave/Mac_Engine/build/src/flutter/lib/ui/hooks.dart:58)
Handler: onTap
Recognizer:
  TapGestureRecognizer#69dbc(debugOwner: GestureDetector, state: ready)
════════════════════════════════════════════════════════════════════════════════════════════════════

You can either pass a GlobalKey to your Scaffold constructor:

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final key = new GlobalKey<ScaffoldState>();
    return new Scaffold(
      key: key,
      floatingActionButton: new Builder(
        builder: (BuildContext context) {
          return new FloatingActionButton(
            onPressed: () {
              key.currentState.showSnackBar(new SnackBar(
                content: new Text("Sending Message"),
              ));
            },
            tooltip: 'Increment',
            child: new Icon(Icons.add),
          );
        }
      ),
    );
  }
}

Or you can use a Builder to create a BuildContext that is a child of the Scaffold.

class MyHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      floatingActionButton: new Builder(
        builder: (BuildContext context) {
          return new FloatingActionButton(
            onPressed: () {
              Scaffold.of(context).showSnackBar(new SnackBar(
                content: new Text("Sending Message"),
              ));
            },
            tooltip: 'Increment',
            child: new Icon(Icons.add),
          );
        }
      ),
    );
  }
}

Finally, you could split your widget into multiple classes, which is the best long-term approach.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Collin Jackson
  • 110,240
  • 31
  • 221
  • 152
  • Tried GlobalKey, Now I am getting this exception: `I/flutter ( 4965): The following assertion was thrown while handling a gesture: I/flutter ( 4965): type 'LabeledGlobalKey' is not a subtype of type 'BuildContext' of 'context' where I/flutter ( 4965): LabeledGlobalKey is from package:flutter/src/widgets/framework.dart I/flutter ( 4965): ScaffoldState is from package:flutter/src/material/scaffold.dart I/flutter ( 4965): Scaffold is from package:flutter/src/material/scaffold.dart I/flutter ( 4965): BuildContext is from package:flutter/src/widgets/framework.dart` – Shajeel Afzal Sep 10 '17 at 11:13
  • 1
    It looks like you're using a `GlobalKey` as an argument where a `BuildContext` is expected. I can't help you debug this further without seeing more of your code. Please post the line of code that is throwing the exception, probably you just aren't using the right arguments. – Collin Jackson Sep 10 '17 at 22:01
  • 2
    I can confirm that using the `Builder` option you gave works well. Ran into this issue and this solved it for me. – SeaFuzz May 25 '18 at 23:54
  • I got an error with GlobalKey method but taking the declaration `final key = new GlobalKey();` outside of Widget build fixed it. – Sagar V Mar 31 '20 at 09:10
20

To show a Toast message you can use the FlutterToast plugin. To use this plugin, you have to:

  1. Add this dependency to your pubspec.yaml file: fluttertoast: ^8.0.8
  2. To get the package, you have to run this command: $ flutter packages get
  3. import the package: import 'package:fluttertoast/fluttertoast.dart';

Use it like this:

Fluttertoast.showToast(
    msg: "your message",
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.BOTTOM // Also possible "TOP" and "CENTER"
    backgroundColor: "#e74c3c",
    textColor: '#ffffff');

For more information, check this.

Virendra Varma
  • 895
  • 1
  • 12
  • 23
15

fluttertoast: ^3.1.3

import 'package:fluttertoast/fluttertoast.dart';

Fluttertoast.showToast(
        msg: "This is Center Short Toast",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIos: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0
    );
mlykotom
  • 4,850
  • 1
  • 22
  • 27
fucukur
  • 274
  • 1
  • 3
  • 11
  • 5
    Please try to add descriptive sentence or two which describes where to put which code. Something like "Add package to your pubspec" and "In code, use:" – mlykotom Nov 29 '19 at 13:20
15

Snack Bar

When I tried to use a solution with using the ScaffoldState object (suggested by others), I got a warning that it is deprecated:

'showSnackBar' is deprecated and shouldn't be used. Use ScaffoldMessenger.showSnackBar. This feature was deprecated after v1.23.0-14.0.pre..

Using ScaffoldMessenger works as expected:

ScaffoldMessenger.of(context)
    .showSnackBar(SnackBar(content: Text("My amazing message! O.o")));

Example:

Snack bar message

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Micer
  • 8,731
  • 3
  • 79
  • 73
10

In case the Fluttertoast package given so far doesn’t work, then I will suggest you to try toast.

It has no frills and no ceremony.

Enter image description here

It just works.

I noticed a bug within the example given within its README file though:

Toast.show(
"Toast plugin app",
 duration: Toast.LENGTH_SHORT,
 gravity:  Toast.BOTTOM);

While the method requires a context. So do well to add 'context' like this:

Toast.show(
"Toast plugin app", context,
 duration: Toast.LENGTH_SHORT,
 gravity: Toast.BOTTOM);

There is a chance that this would have been fixed by the time you checked though. I already submitted a PR.

Victor Eronmosele
  • 7,040
  • 2
  • 10
  • 33
Wale
  • 1,321
  • 16
  • 11
10

For the toast message in Flutter, use the bot_toast library. This library provides a feature-rich, support for displaying notifications, text, loading, attachments, etc. Toast

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dhaval Kansara
  • 3,478
  • 5
  • 22
  • 50
8

I would like to provide an alternative solution to use package flushbar.

As the package said: Use this package if you need more customization when notifying your user. For Android developers, it is made to substitute toasts and snackbars.

Another suggestion to use flushbar is How can I show a snackbar after navigator.pop(context) in Flutter?

You can also set flushbarPosition to TOP or BOTTOM:

Enter image description here

    Flushbar(
      title: "Hey Ninja",
      message: "Lorem Ipsum is simply dummy text of the printing and typesetting industry",
      flushbarPosition: FlushbarPosition.TOP,
      flushbarStyle: FlushbarStyle.FLOATING,
      reverseAnimationCurve: Curves.decelerate,
      forwardAnimationCurve: Curves.elasticOut,
      backgroundColor: Colors.red,
      boxShadows: [BoxShadow(color: Colors.blue[800], offset: Offset(0.0, 2.0), blurRadius: 3.0)],
      backgroundGradient: LinearGradient(colors: [Colors.blueGrey, Colors.black]),
      isDismissible: false,
      duration: Duration(seconds: 4),
      icon: Icon(
        Icons.check,
        color: Colors.greenAccent,
      ),
      mainButton: FlatButton(
        onPressed: () {},
        child: Text(
          "CLAP",
          style: TextStyle(color: Colors.amber),
        ),
      ),
      showProgressIndicator: true,
      progressIndicatorBackgroundColor: Colors.blueGrey,
      titleText: Text(
        "Hello Hero",
        style: TextStyle(
            fontWeight: FontWeight.bold, fontSize: 20.0, color: Colors.yellow[600], fontFamily: "ShadowsIntoLightTwo"),
      ),
      messageText: Text(
        "You killed that giant monster in the city. Congratulations!",
        style: TextStyle(fontSize: 18.0, color: Colors.green, fontFamily: "ShadowsIntoLightTwo"),
      ),
    )..show(context);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
chunhunghan
  • 51,087
  • 5
  • 102
  • 120
6

Import the library fluttertoast: 3.1.3

Use it like below:

Fluttertoast.showToast(
    msg: "Hello, World!",
    textColor: Colors.white,
    toastLength: Toast.LENGTH_SHORT,
    timeInSecForIos: 1,
    gravity: ToastGravity.BOTTOM,
    backgroundColor: Colors.indigo,
);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nimal viju
  • 59
  • 1
  • 3
  • Except for the version number it is (was) a copy-pasted [Android Dev's answer](https://stackoverflow.com/questions/45948168/how-to-create-toast-in-flutter/56729660#56729660) (including all the grammar mistakes). – Peter Mortensen May 30 '21 at 22:03
6

There is a three-way to show toast in a Flutter app.

I will tell you about all three ways that I know and choose which one you want to use. I would recommend the second one.

1: Using the external package.

This is the first method which is the easiest way to show toast in a Flutter app.

First of all, you have to add this package to file pubspec.YAML:

flutter_just_toast:^version_here

Then import the package in the file where you want to show a toast.

'package:flutter_just_toast/flutter_just_toast.dart';

And the last step shows the toast.

Toast.show(message: "Your toast message",
           duration: Delay.SHORT,
           textColor: Colors.black);

2: Using the official way.

This method is also simple, but you have to deal with it. I am not saying that it is hard it is simple and clean I would recommend this method.

For this method, all you have to do show toast is using the below code.

Scaffold.of(context).showSnackBar(SnackBar(
          content: Text("Sending Message"),
        ));

But remember that you have to use the scaffold context.

3: Using the native API.

Now, this method does not make sense any more when you already have the two methods above. Using this method, you have to write native code for Android and iOS and then convert it to plugin and you are ready to go.

This method will consume your time and you have to reinvent the wheel. Which has already been invented.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Zakria Khan
  • 1,897
  • 1
  • 17
  • 22
3

For the ones that are looking for a Toast what can survive the route changes, the SnackBar might not be the best option.

Have a look at Overlay instead.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Stefano Saitta
  • 1,844
  • 1
  • 19
  • 34
3

Add flutter_just_toast to your dependencies in your Pubspecs.yaml file.

Dependencies:

flutter_just_toast: ^1.0.1

Next import the package into your class:

import 'package:flutter_just_toast/flutter_just_toast.dart';

Implement Toast with a message:

Toast.show(message: "Your toast message",
    duration: Delay.SHORT,
    textColor: Colors.black);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
McShaba
  • 31
  • 1
3

Just use SnackBar(content: Text("hello"),) inside any event like onTap and onPress.

You can read more about Snackbar on Display a snackbar.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rashid Iqbal
  • 1,123
  • 13
  • 13
2

For the Android original graphics toast, you can use this:

It works fine on Android and iOS.

enter image description here

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Lukas Marik
  • 119
  • 1
  • 4
2

Use https://pub.dev/packages/toast for toast. This library is pretty easy to use and perfectly works for iOS and Android.

Syntax for showing Toast:

Toast.show("Toast plugin app", duration: Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
M.Bilal Murtaza
  • 1,757
  • 2
  • 10
  • 8
2

You can use this package: toast

Add this line to your dependencies

toast: ^0.1.5

Then use it like this:

import 'package:toast/toast.dart';
Toast.show("Toast plugin app", context, duration: Toast.LENGTH_SHORT, gravity:  Toast.BOTTOM);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Djamel Madani
  • 496
  • 6
  • 8
2

Get the Flutter toast package here

Add this package to your project dependencies in file pubspec.yaml.

Then whenever you want the Toast to be shown, like on a tap of a button:

Toast.show("Toast plugin app", context, duration: Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
hadibtf
  • 63
  • 8
2

You can use fluttertoast package. To do this, add it in the pubspec.yaml file like:

dependencies:
  fluttertoast: ^8.0.8

Then import that package in the .dart file you need the toast in and write your code.

For example, refer to the following code:

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ToastExample extends StatefulWidget {
    @override
    _ToastExampleState createState() {
      return _ToastExampleState();
    }
  }

  class _ToastExampleState extends State {
    void showToast() {
      Fluttertoast.showToast(
          msg: 'This is flutterToast example', // Message
          toastLength: Toast.LENGTH_SHORT,    // toast length
          gravity: ToastGravity.CENTER,      // position
          timeInSecForIos: 1,               // duaration
          backgroundColor: Colors.red,     // background color
          textColor: Colors.white         // text color
      );
    }

    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        title: 'Toast Tutorial',
        home: Scaffold(
            appBar: AppBar(
              title: Text('Toast Tutorial'),
            ),
            body: Padding(
              padding: EdgeInsets.all(15.0),
              child: Center(
                child: RaisedButton(
                  child: Text('Show Toast'),
                  onPressed: showToast,
                ),
              ),
            )
        ),
      );
    }
  }

  void main() => runApp(ToastExample());
Ujjawal Maurya
  • 462
  • 5
  • 17
sathwik
  • 114
  • 3
2

Importcupertino_icons: ^0.1.2 and write the below code:

showToast(BuildContext context, String message) {
  showDialog(
    context: context,
    builder: (BuildContext context) {
      return CupertinoAlertDialog(
        title: Text(
          "Name of App",
        ),
        content: Text(
          message,
        ),
        actions: [
          CupertinoButton(
            child: Text("OK"),
            onPressed: () {
              Navigator.of(context).pop();
            },
          )
        ],
      );
    },
  );
});
Tom Nijs
  • 3,835
  • 3
  • 22
  • 40
Aamil Silawat
  • 7,735
  • 3
  • 19
  • 37
2

For this, there are different versions.

  1. First of all, you can use SnackBar which is a widget in Flutter.

  2. You can use libraries like toast - flutter_toast from pub.dev.

  3. The third version is creating your custom widget. It can be created using Overlay widget and Animation in Flutter.

You can this this tutorial to learn more about it. Here is a link.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
thisisyusub
  • 670
  • 8
  • 20
2

The answer Scaffold.of(context).showSnackBar(...) has not worked in most cases.

I suggest the optimal method be declaring a scaffoldState key within the class and assigning it to Scaffold like below:

GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();

And then

Scaffold(
  key: _scaffoldKey,
  ...
)

When you want to show the Snackbar, do this:

_scaffoldKey.currentState.showSnackBar(SnackBar(
  content: Text("This works!"),
));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jerin
  • 688
  • 1
  • 9
  • 21
2

Use this:

Fluttertoast.showToast(
    msg: "This is a Toast message",
    toastLength: Toast.LENGTH_SHORT,
    gravity: ToastGravity.CENTER,
    timeInSecForIos: 1
);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
2

You can just straight away to use snack bar element

 ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text("Successfully!"),
        behavior: SnackBarBehavior.floating,
        margin: EdgeInsets.all(20),
          shape: StadiumBorder(),
        action: SnackBarAction(
          label: 'Dismiss',
          disabledTextColor: Colors.white,
          textColor: Colors.blue,
          onPressed: () {
            //Do whatever you want
          },
        ),
      ),
    );
Edward
  • 89
  • 1
  • 6
1

Step 1:

Dependencies:

flutter_just_toast: ^1.0.1

Step 2:

import 'package:flutter_just_toast/flutter_just_toast.dart';

Step 3:

Toast.show(
  message: "Your toast message",
  duration: Delay.SHORT,
  textColor: Colors.black);

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alan John
  • 767
  • 9
  • 15
1

Use this dependency:

toast: ^0.1.3

Then import the dependency of toast in the page:

import 'package:toast/toast.dart';

Then on onTap() of the widget:

Toast.show("Toast plugin app", context, duration:Toast.LENGTH_SHORT, gravity: Toast.BOTTOM);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AMAL K G
  • 306
  • 1
  • 7
1

There isn't any widget for toast in Flutter. You can go to this plugin.

Use case:

Fluttertoast.showToast(
    msg: "My toast message",
    textColor: Colors.white,
    toastLength: Toast.LENGTH_SHORT,
    timeInSecForIos: 1,
    gravity: ToastGravity.BOTTOM,
    backgroundColor: Colors.indigo,);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ramesh Giri
  • 21
  • 1
  • 6
1

It's quite simple:

We just have to install the Flutter toast package. Refer to the following documentation: https://pub.dev/packages/fluttertoast

In the installing tab you will get the dependency which you have to paste it in the pubspec.yaml file and then install.

After this, just import the package:

import 'package:fluttertoast/fluttertoast.dart';

Similar to the above line.

And then by using FlutterToast class you can use your fluttertoast.

You're done!!!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prajwal Shinde
  • 195
  • 1
  • 6
1

You can use this link to show Toast in Flutter.

Use this as:

void method1(){

    Fluttertoast.showToast(
        msg: "This is Add Button",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.blueGrey,
        textColor: Colors.white,
        fontSize: 14.0
    );
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rahul Kushwaha
  • 5,473
  • 3
  • 26
  • 30
0

It's quite easy to show a Toast message in Flutter:

Scaffold.of(context).showSnackBar(SnackBar(
    content: Text("Toast Text Here"),
));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Qasim Ali
  • 434
  • 2
  • 5
  • 31
0

You can easily achieve this effect using Overlay.

Code:

OverlayEntry entry = OverlayEntry(builder: (context) {
              return Positioned(
                  top: MediaQuery.of(context).size.height * 0.8,
                  child: Container(
                    width: MediaQuery.of(context).size.width,
                    alignment: Alignment.center,
                    child: const Card(
                      color: Colors.redAccent,
                      child: Padding(
                        padding: EdgeInsets.all(8),
                        child: Text("This is a message."),
                      ),
                    ),
                  ));
            });

            //show overlay
            Overlay.of(context)!.insert(entry);
            //auto remove this overlay after 3 seconds
            Future.delayed(const Duration(seconds: 3)).then((value) => entry.remove());

Screenshot:

enter image description here

hanswim
  • 1,182
  • 8
  • 20
-1

You can use something like FlutterToast.

Import the library:

fluttertoast: ^2.1.4

Use it like below:

Fluttertoast.showToast(
    msg: "Hello, World!",
    textColor: Colors.white,
    toastLength: Toast.LENGTH_SHORT,
    timeInSecForIos: 1,
    gravity: ToastGravity.BOTTOM,
    backgroundColor: Colors.indigo,
);

That's it...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Android Dev
  • 1,496
  • 1
  • 13
  • 25