Skip to main content

api_overlay_toast 0.1.0 copy "api_overlay_toast: ^0.1.0" to clipboard
api_overlay_toast: ^0.1.0 copied to clipboard

Context-free Flutter overlay notifications with automatic REST/WebSocket payload parsing and an optional Hive-backed offline queue.

example/lib/main.dart

import 'dart:async';

import 'package:api_overlay_toast/api_overlay_toast.dart';
import 'package:flutter/widgets.dart';
import 'package:hive_flutter/hive_flutter.dart';

final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  ApiOverlayToast.init(
    navigatorKey: navigatorKey,
    config: const ToastConfig(
      position: ToastPosition.top,
      duration: Duration(seconds: 4),
      animationDuration: Duration(milliseconds: 260),
    ),
  );

  await Hive.initFlutter();
  await ApiOverlayToast.initOffline(
    config: const OfflineConfig(
      enabled: true,
      persistQueue: true,
      autoSyncOnConnectivity: true,
      maxRetries: 3,
    ),
  );

  runApp(const ExampleApp());
}

class ExampleApp extends StatelessWidget {
  const ExampleApp({super.key});

  @override
  Widget build(BuildContext context) {
    return WidgetsApp(
      navigatorKey: navigatorKey,
      color: const Color(0xFF111827),
      debugShowCheckedModeBanner: false,
      home: const HomePage(),
      pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) {
        return PageRouteBuilder<T>(
          settings: settings,
          pageBuilder: (
            BuildContext context,
            Animation<double> animation,
            Animation<double> secondaryAnimation,
          ) =>
              builder(context),
        );
      },
    );
  }
}

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  void dispose() {
    unawaited(ApiOverlayToast.disposeOffline());
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Directionality(
      textDirection: TextDirection.ltr,
      child: ColoredBox(
        color: const Color(0xFFF8FAFC),
        child: SafeArea(
          child: SingleChildScrollView(
            padding: const EdgeInsets.all(24),
            child: Center(
              child: ConstrainedBox(
                constraints: const BoxConstraints(maxWidth: 520),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.stretch,
                  children: <Widget>[
                    const Text(
                      'api_overlay_toast',
                      style: TextStyle(
                        color: Color(0xFF0F172A),
                        fontSize: 28,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    const SizedBox(height: 6),
                    const Text(
                      'Context-free Flutter overlay notifications with offline queueing.',
                      style: TextStyle(
                        color: Color(0xFF475569),
                        fontSize: 15,
                      ),
                    ),
                    const SizedBox(height: 24),
                    _DemoButton(
                      label: 'Success toast',
                      color: const Color(0xFF059669),
                      onPressed: () => ApiOverlayToast.success(
                        message: 'Profile saved successfully',
                      ),
                    ),
                    _DemoButton(
                      label: 'Error toast with action',
                      color: const Color(0xFFDC2626),
                      onPressed: () => ApiOverlayToast.error(
                        message: 'Upload failed',
                        actionLabel: 'Retry',
                        onAction: () => ApiOverlayToast.info(
                          message: 'Retrying upload...',
                        ),
                      ),
                    ),
                    _DemoButton(
                      label: 'Warning toast',
                      color: const Color(0xFFD97706),
                      onPressed: () => ApiOverlayToast.warning(
                        message: 'Your session expires soon',
                      ),
                    ),
                    _DemoButton(
                      label: 'Info toast',
                      color: const Color(0xFF2563EB),
                      onPressed: () => ApiOverlayToast.info(
                        message: 'Sync started',
                      ),
                    ),
                    _DemoButton(
                      label: 'Guest prompt',
                      color: const Color(0xFF7C3AED),
                      onPressed: () => ApiOverlayToast.guestPrompt(
                        message: 'Sign up to sync across devices',
                        actionLabel: 'Sign up',
                        onAction: () => ApiOverlayToast.success(
                          message: 'Opening sign-up screen...',
                        ),
                      ),
                    ),
                    _DemoButton(
                      label: 'Parse REST success payload',
                      color: const Color(0xFF0F766E),
                      onPressed: () => ApiOverlayToast.fromApi(
                        <String, dynamic>{
                          'status': 'success',
                          'message': 'Payment confirmed',
                        },
                      ),
                    ),
                    _DemoButton(
                      label: 'Parse REST error payload',
                      color: const Color(0xFFBE123C),
                      onPressed: () => ApiOverlayToast.fromApi(
                        <String, dynamic>{
                          'error': <String, dynamic>{
                            'description': 'Invalid API key',
                          },
                        },
                      ),
                    ),
                    _DemoButton(
                      label: 'Parse WebSocket frame',
                      color: const Color(0xFF4338CA),
                      onPressed: () => ApiOverlayToast.fromSocket(
                        '{"status":"success","message":"Live update received"}',
                      ),
                    ),
                    _DemoButton(
                      label: 'Queue an offline request',
                      color: const Color(0xFF334155),
                      onPressed: () async {
                        await ApiOverlayToast.callApiWithOffline(
                          method: 'POST',
                          url: 'https://example.com/api/events',
                          body: <String, dynamic>{'demo': true},
                        );
                      },
                    ),
                    _DemoButton(
                      label: 'Show queue length',
                      color: const Color(0xFF475569),
                      onPressed: () => ApiOverlayToast.info(
                        message:
                            'Queued requests: ${ApiOverlayToast.offlineQueueLength}',
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

class _DemoButton extends StatelessWidget {
  const _DemoButton({
    required this.label,
    required this.color,
    required this.onPressed,
  });

  final String label;
  final Color color;
  final VoidCallback onPressed;

  @override
  Widget build(BuildContext context) {
    return Semantics(
      button: true,
      child: GestureDetector(
        onTap: onPressed,
        child: Container(
          margin: const EdgeInsets.only(bottom: 12),
          padding: const EdgeInsets.symmetric(horizontal: 18, vertical: 14),
          decoration: BoxDecoration(
            color: color,
            borderRadius: const BorderRadius.all(Radius.circular(12)),
          ),
          child: Text(
            label,
            style: const TextStyle(
              color: Color(0xFFFFFFFF),
              fontSize: 14,
              fontWeight: FontWeight.w600,
            ),
          ),
        ),
      ),
    );
  }
}
1
likes
160
points
36
downloads

Documentation

API reference

Publisher

verified publisherthodz.com

Weekly Downloads

Context-free Flutter overlay notifications with automatic REST/WebSocket payload parsing and an optional Hive-backed offline queue.

Repository (GitHub)
View/report issues

Topics

#flutter #toast #notification #api #overlay

License

MIT (license)

Dependencies

connectivity_plus, flutter, hive, hive_flutter, http

More

Packages that depend on api_overlay_toast