BotToast Documentation

repository·master·Indexed 21 days ago

https://github.com/mmmzq/bot_toast

A feature-rich Flutter library for displaying various types of toasts, including notifications, loading indicators, text, and custom widgets. It provides global configuration via BotToast.defaultOption and supports complex layouts through NotificationOption and SimpleNotificationOption. The library includes BotToastNavigatorObserver for managing toast visibility across multiple Navigator instances and BotToastNavigatorObserverProxy for intercepting navigation events.

Tokens
7.2K
Snippets
25
Records
35
Agent score
74%

What's inside BotToast

  1. Migrating from 2.x to 3.x/4.x

    master

    In version 3.0 and above, the initialization logic was reimplemented to be simpler and no longer depend on the Navigator.

    Old 2.x.x method:

    BotToastInit(
      child: MaterialApp(
          title: 'BotToast Demo',
          navigatorObservers: [BotToastNavigatorObserver()],
          home: XxxxPage(),
      )
    );

    New 3.x.x/4.x.x method:

    MaterialApp(
          title: 'BotToast Demo',
          builder: BotToastInit(), 
          navigatorObservers: [BotToastNavigatorObserver()],
          home: XxxxPage(),
    )
  2. Initialize BotToast in your MaterialApp

    master

    To use BotToast, you must initialize it in your MaterialApp using BotToastInit in the builder property and register BotToastNavigatorObserver in navigatorObservers.

    Warning: Do not arbitrarily change the position of the BotToastInit function call within the builder.

    MaterialApp(
          title: 'BotToast Demo',
          builder: BotToastInit(), // 1. Call BotToastInit
          navigatorObservers: [BotToastNavigatorObserver()], // 2. Register route observer
          home: XxxxPage(),
      )
  3. Install and Setup BotToast

    master

    To use BotToast in your Flutter project, follow these three steps:

    1. Add dependency: Add bot_toast to your pubspec.yaml.
    2. Import the library: Import package:bot_toast/bot_toast.dart in your Dart files.
    3. Initialize BotToast: You must register the BotToastInit builder and the BotToastNavigatorObserver within your MaterialApp to enable global toast functionality.

    Warning: Do not arbitrarily change the position of the BotToastInit function call within the builder property. If you are using a custom builder, ensure botToastBuilder is called to wrap the child.

    dependencies:
       bot_toast: ^4.1.0 #null safety
    import 'package:bot_toast/bot_toast.dart';
    
    // Standard initialization
    MaterialApp(
      title: 'BotToast Demo',
      builder: BotToastInit(), // 1. call BotToastInit
      navigatorObservers: [BotToastNavigatorObserver()], // 2. registered route observer
      home: XxxxPage(),
    )
  4. Configure BotToast Global Options

    master

    You can modify the default behavior of all toasts globally using BotToast.defaultOption. This allows you to set default animation durations, styles, and other parameters for specific toast types.

    Common configuration paths include:

    • BotToast.defaultOption.simpleNotification (for showSimpleNotification)
    • BotToast.defaultOption.notification (for showNotification)
    • BotToast.defaultOption.text (for showText)
    • BotToast.defaultOption.customText (for showCustomText)
    • BotToast.defaultOption.loading (for showLoading)
    • BotToast.defaultOption.customLoading (for showCustomLoading)
    • BotToast.defaultOption.attached (for showAttachedWidget)
    • BotToast.defaultOption.animation (for showAnimationWidget)
    • BotToast.defaultOption.enhanced (for showEnhancedWidget)
    /// Globally change the animation duration for standard notifications to 1 second.
    BotToast.defaultOption.notification.animationDuration = const Duration(seconds: 1);
  5. Important: Avoid absorbing click events in custom toasts

    master

    When using ToastBuilder (via showCustomNotification or showCustomText) to generate widgets, ensure the generated widget's background does not absorb click events.

    Widgets like Scaffold or Material occupy the entire parent space and absorb events by default, even if they are transparent. If your widget absorbs events, features like allowClick (clicking through the toast to the background) will fail.

    Solution: If you must use a widget that absorbs events, wrap it in an IgnorePointer widget.

  6. Initialize BotToast by adding BotToastManager to the widget tree

    master

    To enable toast functionality in your Flutter application, you must wrap your application's root widget (usually inside MaterialApp) with the BotToastManager widget. This widget manages the overlay stack where all toasts and notifications will be rendered.

    void main() {
      runApp(
        BotToastManager(
          child: MaterialApp(
            // ... your app configuration
          ),
        ),
      );
    }
  7. Manage toast visibility with BotToastNavigatorObserver

    master

    If your Flutter project uses multiple Navigator instances, you must add BotToastNavigatorObserver to the navigatorObservers list of each Navigator. This ensures that toasts are correctly managed and visible during navigation transitions across different parts of your app.

    To use it, initialize your app with BotToastInit and include the observer in your MaterialApp (or any Navigator):

    BotToastInit(
      child: MaterialApp(
        title: 'Xxxx Demo',
        navigatorObservers: [BotToastNavigatorObserver()],
        home: XxxxPage(),
      ),
    );
  8. Important considerations for BotToast

    master

    Multiple Navigators

    If your project uses multiple Navigator widgets, you must add BotToastNavigatorObserver to the Navigator.observers list of each navigator to ensure all features work correctly.

    Widget Backgrounds and Click Events

    When using ToastBuilder to generate custom widgets, ensure the generated widget's background does not intercept click events. Widgets like Scaffold or Material default to occupying the entire parent space and will absorb touch events (even if they are transparent).

  9. Show Attached Widgets

    master

    Use showAttachedWidget to display a widget at a specific position on the screen relative to a target. This is useful for context-specific notifications or attachments.

    // Popup an attachments toast
    var cancel = BotToast.showAttachedWidget(
        attachedBuilder: (_) => Card(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Icon(
                  Icons.favorite,
                  color: Colors.redAccent,
                ),
              ),
            ),
        duration: Duration(seconds: 2),
        target: Offset(520, 520));
    // ...
    cancel(); // close
  10. Show an attached widget toast

    master

    Use showAttachedWidget to display a widget at a specific position on the screen using an Offset.

    // Show a positioned toast
    var cancel = BotToast.showAttachedWidget(
        attachedBuilder: (_) => Card(
              child: Padding(
                padding: const EdgeInsets.all(8.0),
                child: Icon(
                  Icons.favorite,
                  color: Colors.redAccent,
                ),
              ),
            ),
        duration: Duration(seconds: 2),
        target: Offset(520, 520));
    // ...
    cancel(); // Close