Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create Toast in Flutter

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.

like image 824
Shady Aziza Avatar asked Aug 29 '17 20:08

Shady Aziza


People also ask

How do you show toast in flutter Web?

To show toast using snackbar: Step 1: Add the button (ElevatedButton) if you haven't already added it. Step 2: Inside the onPressed method of a button, create a SnackBar. Step 3: Just in the next line, add the ScaffoldMessenger, call the showSnackBar method and pass the SnackBar created previously.

What is toast in UI?

A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.


2 Answers

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(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),       ),     );   } } 
like image 133
Rémi Rousselet Avatar answered Sep 25 '22 06:09

Rémi Rousselet


Use fluttertoast plugin

Fluttertoast.showToast(         msg: "This is a Toast message",  // message         toastLength: Toast.LENGTH_SHORT, // length         gravity: ToastGravity.CENTER,    // location         timeInSecForIos: 1               // duration     ); 

enter image description here

like image 32
Raouf Rahiche Avatar answered Sep 22 '22 06:09

Raouf Rahiche