avocado_toast 0.2.1

  • README.md
  • CHANGELOG.md
  • Installing
  • Versions
  • 73

avocado_toast

A Flutter Plugin for generating custom Android Toasts, completely in Dart code (no channels).

Getting Started

Why Avocado Toast?

It seemed clever at the time.

What is Avocado Toast?

Avocado Toast is a Dart plugin for Flutter apps, allowing developers the opportunity to add traditional and custom toasts to their Flutter-made Android apps, using either strings or widgets. Currently, Flutter has not included a traditional toast widget to their material package.

Snackbars are Toasts in Material Design, why not just use a material snackbar?

fiddler on the roof - tradition

Tradition!

Why not make a channel, and use Android's native toasts?

That's actually a really good idea. There's nothing wrong with creating a channel, especially when the widget is meant to emulate functionality only found on Android devices. However, I wanted to create something that would have a limited connection to the native layer, add nothing to the dex method count, and exist in a single .dart file.

What constructors / methods / constants are in the API?

  • Current features:
    • Toastable() Widget (as a body argument for a Scaffold widget), for wrapping Material child widgets, and accepts two arguments:
      • toastController parameter (mandatory), which accepts a ToastController instance (see below)
      • child parameter (mandatory), the child of the Scaffold widget, and the widget upon which one wishes to fire off a Toast
    • ToastController() constructor, which accepts a State argument as a parameter (mandatory)
    • show() method, to fire off a Toast:
      • text parameter, for passing Strings (do not use if you only want to create a custom widget)
      • widget parameter, for passing Widgets (do not use if you only want to make a classic toast)
      • toastLength parameter (mandatory), which accepts one of only two constants for duration:
        • ToastLength.SHORT - duration of 2.0 seconds
        • ToastLength.LONG - duration of 3.5 seconds
  • Current limitations:
    • Should only be used with Android apps (no alert option for iOS)
    • Widget position is anchored close to the bottom of the screen (great for classic toasts, but limits placement of custom widgets, i.e. images, buttons, videos, gifs, etc.)
    • Currently queued toasts do not survive moving between routes
  • Improvements planned for next release:
    • Sustain toasts between state/route changes
    • allow more positional composition for custom widgets

How do I use Avocado Toast?

First, incorporate this plugin into your Flutter project by adding it as a dependency to your app's pubspec.yaml file:

name: example
description: A Flutter app.

dependencies:
  flutter:
    sdk: flutter
    
  cupertino_icons: ^0.1.2

  avocado_toast: ^0.2.1

...

add the RobotoCondensed font to the pubspec.yaml as well:

flutter:

  uses-material-design: true

  fonts:
    - family: RobotoCondensed
      fonts:
        - asset: packages/avocado_toast/fonts/RobotoCondensed-Regular.ttf

and import the package into your app:

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

Next, in your PageState class for whichever page you'd like to have toasts displayed, declare and initialize your ToastController with a State instance by overriding initState(), and clean up resources by overriding dispose():

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  ToastController toastController;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  void initState() {
    super.initState();
    toastController = ToastController(state: this);
    }

  @override
  void dispose() {
    super.dispose();
    toastController = null;
  }
  ....
}

Then, add a Toastable() widget to the body parameter of your Scaffold() widget of your overridden build() method, passing in the ToastController to the toastController parameter, and the rest of the layout widgets to the child parmeter:

....

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Toastable(
        toastController: toastController,
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.display1,
              ),
            ],
          ),
        ),
      ),
      ....

Finally, call the show() method on your ToastController instance as needed, and pass in your text as a string, and the duration of the toast as either ToastLength.SHORT or ToastLength.LONG:

      ....
      
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _incrementCounter();
          toastController.show(
              text: 'Button Clicked this many times: $_counter', 
              toastLength: ToastLength.LONG);
        },
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
 ....

And that's it! The toast should look a little something like this:

traditonal toast

And now for something completely meta:

widget toast

A widget with an image of Avocado Toast inside an Avocado Toast Toastable() widget. You're welcome.

License

The MIT License. Now go forth and make Toasts like it's 2008!

Please see the attached LICENSE file for more information.

You can find the plugin by searching for avocado_toast at https://pub.dartlang.org, or you can download the Flutter plugin here!

[0.2.1] - 06/26/2018

  • Added example project to the example folder, demonstrating how to implement avocado_toast in a real-world app
  • Updated README.md to include instructions related to adding the RobotoCondensed font to the app's pubspec.yaml file

[0.2.0] - 06/26/2018

  • Moved RobotoCondensed-Regular.ttf font to /lib/fonts folder for in-app use, to resolve traditional toast font issue

[0.1.0] - 06/16/2018

  • Initial release.
  • Current features:
    • Toastable() Widget (as a body argument for a Scaffold widget), for wrapping Material child widgets, and accepts two arguments:
      • toastController parameter (mandatory), which accepts a ToastController instance (see below)
      • child parameter (mandatory), the child of the Scaffold widget, and the widget upon which one wishes to fire off a Toast
    • ToastController() constructor, which accepts a State argument as a parameter (mandatory)
    • show() method, to fire off a Toast:
      • text parameter, for passing Strings (do not use if you only want to create a custom widget)
      • widget parameter, for passing Widgets (do not use if you only want to make a classic toast)
      • toastLength parameter (mandatory), which accepts one of only two constants for duration:
        • ToastLength.SHORT - duration of 2.0 seconds
        • ToastLength.LONG - duration of 3.5 seconds
  • Current limitations:
    • Should only be used with Android apps (no alert option for iOS)
    • Widget position is anchored close to the bottom of the screen (great for classic toasts, but limits placement of custom widgets, i.e. images, buttons, videos, gifs, etc.)
    • Currently queued toasts do not survive moving between routes
  • Improvements planned for next release:
    • Sustain toasts between state/route changes
    • allow more positional composition for custom widgets

Use this package as a library

1. Depend on it

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


dependencies:
  avocado_toast: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:


import 'package:avocado_toast/avocado_toast.dart';
  
Version Uploaded Documentation Archive
0.2.1 Jun 27, 2018 Go to the documentation of avocado_toast 0.2.1 Download avocado_toast 0.2.1 archive
0.2.0 Jun 26, 2018 Go to the documentation of avocado_toast 0.2.0 Download avocado_toast 0.2.0 archive
0.1.0 Jun 16, 2018 Go to the documentation of avocado_toast 0.1.0 Download avocado_toast 0.1.0 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
80
Overall:
Weighted score of the above. [more]
73
Learn more about scoring.

We analyzed this package on Jul 20, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.69.0
  • pana: 0.11.7
  • Flutter: 0.5.5

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://www.dartlang.org/dart-2#migration.

Package is pre-v1 release.

While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction of the API.

Maintain an example.

None of the files in your example/ directory matches a known example patterns. Common file name patterns include: main.dart, example.dart or you could also use avocado_toast.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.23.0 <2.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.11
meta 1.1.5 1.1.6
sky_engine 0.0.99
typed_data 1.1.5 1.1.6
vector_math 2.0.6 2.0.7
Dev dependencies
test ^0.12.0