Creating Toast Notifications in Flutter

Creating Toast Notifications in Flutter

19 December 2024 Stephan Petzl Leave a comment Tech-Help

Toasts are an essential feature in mobile applications, offering brief notifications that do not obstruct user interaction. In Flutter, while the concept of a “Toast” is not built-in as it is in Android, you can achieve similar functionality using widgets like SnackBar or third-party packages. Below, we explore some methods to implement toast-like notifications in Flutter.

Using SnackBar

Flutter provides the SnackBar widget as part of its material design components, which serves a similar purpose to Toasts in the Android ecosystem. Here’s how you can display a SnackBar in your Flutter application:


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: ElevatedButton(
          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),
      ),
    );
  }
}
  

The ScaffoldMessenger is the recommended approach for displaying SnackBar in Flutter 2.0 and later. This method allows you to easily show notifications without interrupting the user experience.

Using Fluttertoast Plugin

For developers who prefer a more traditional Toast, the fluttertoast plugin offers a straightforward solution. Here’s how to implement it:


dependencies:
  fluttertoast: ^8.1.1

import 'package:fluttertoast/fluttertoast.dart';

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

This plugin provides a wide range of customization options, including gravity, duration, and styling, making it a versatile choice for various use cases.

Choosing the Right Approach

The choice between using SnackBar or a third-party plugin like fluttertoast depends on your application’s requirements and the level of customization needed. SnackBar is ideal for apps adhering to material design guidelines, while fluttertoast offers greater flexibility in terms of appearance and behavior.

Enhancing Testing with Repeato

When developing Flutter applications, testing is crucial to ensure a smooth user experience. This is where Repeato, a no-code test automation tool for iOS and Android, can be highly beneficial. Repeato allows you to create, run, and maintain automated tests efficiently. Leveraging computer vision and AI, it provides a fast and reliable testing solution, ensuring that your toast notifications and other UI elements function as expected across different devices and platforms. For more information, explore our Flutter Test Automation Guide.

Like this article? there’s more where that came from!