swift_alert 2.0.0
swift_alert: ^2.0.0 copied to clipboard
Beautiful, customizable notification banners for Flutter. Show alerts anywhere without context after one-time initialization.
import 'package:flutter/material.dart';
import 'package:swift_alert/swift_alert.dart';
// Create a global navigator key
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
// Initialize SwiftAlert with the navigator key
SwiftAlert.initialize(navigatorKey: navigatorKey);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SwiftAlert Example',
theme: ThemeData(primarySwatch: Colors.blue),
// Pass the navigator key to MaterialApp
navigatorKey: navigatorKey,
home: const ExampleHomePage(),
);
}
}
class ExampleHomePage extends StatelessWidget {
const ExampleHomePage({super.key});
// No context needed! Show alerts from anywhere
void _showAlert(NotificationType type) {
SwiftAlert.show(
message: 'This is a ${type.name} notification!',
type: type,
duration: const Duration(seconds: 3),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('SwiftAlert Example')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Text(
'No context required!',
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: () => _showAlert(NotificationType.success),
child: const Text('Show Success'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => _showAlert(NotificationType.error),
child: const Text('Show Error'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => _showAlert(NotificationType.info),
child: const Text('Show Info'),
),
const SizedBox(height: 10),
ElevatedButton(
onPressed: () => _showAlert(NotificationType.warning),
child: const Text('Show Warning'),
),
],
),
),
);
}
}