live_api_notification 1.1.0
live_api_notification: ^1.1.0 copied to clipboard
An intelligent, zero-boilerplate Flutter overlay notification package with automatic REST API and WebSocket payload parsing, context-free triggers, and smooth animations.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:live_api_notification/live_api_notification.dart';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
WidgetsFlutterBinding.ensureInitialized();
// Initialize global settings & navigatorKey for context-free interceptor calls
LiveNotification.init(
navigatorKey: navigatorKey,
config: const LiveNotificationConfig(
position: LiveNotificationPosition.top,
duration: Duration(seconds: 4),
enableBlur: false,
),
);
runApp(const LiveNotificationDemoApp());
}
class LiveNotificationDemoApp extends StatelessWidget {
const LiveNotificationDemoApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: navigatorKey,
title: 'live_api_notification Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
useMaterial3: true,
colorScheme: ColorScheme.fromSeed(
seedColor: const Color(0xFF10B981),
brightness: Brightness.dark,
),
scaffoldBackgroundColor: const Color(0xFF0F172A),
),
home: const DemoHomeScreen(),
);
}
}
class DemoHomeScreen extends StatefulWidget {
const DemoHomeScreen({super.key});
@override
State<DemoHomeScreen> createState() => _DemoHomeScreenState();
}
class _DemoHomeScreenState extends State<DemoHomeScreen> {
LiveNotificationPosition _position = LiveNotificationPosition.top;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Live API Notification Demo'),
backgroundColor: const Color(0xFF1E293B),
elevation: 0,
),
body: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// Position selector
Card(
color: const Color(0xFF1E293B),
child: Padding(
padding: const EdgeInsets.all(16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
const Text(
'Banner Position:',
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w600,
color: Colors.white,
),
),
SegmentedButton<LiveNotificationPosition>(
segments: const [
ButtonSegment(
value: LiveNotificationPosition.top,
label: Text('Top'),
),
ButtonSegment(
value: LiveNotificationPosition.bottom,
label: Text('Bottom'),
),
],
selected: {_position},
onSelectionChanged: (selected) {
setState(() {
_position = selected.first;
});
},
),
],
),
),
),
const SizedBox(height: 20),
_buildSectionHeader('1. Semantic Overlays'),
Wrap(
spacing: 12,
runSpacing: 12,
children: [
_buildButton(
label: 'Success Toast',
color: const Color(0xFF166534),
icon: Icons.check_circle_rounded,
onPressed: () {
LiveNotification.success(
context: context,
position: _position,
message: 'Changes saved successfully to cloud!',
);
},
),
_buildButton(
label: 'Error Toast',
color: const Color(0xFF991B1B),
icon: Icons.error_rounded,
onPressed: () {
LiveNotification.error(
context: context,
position: _position,
message:
'Connection timed out. Please check your network.',
);
},
),
_buildButton(
label: 'Warning Toast',
color: const Color(0xFF854D0E),
icon: Icons.warning_amber_rounded,
onPressed: () {
LiveNotification.warning(
context: context,
position: _position,
message:
'Your authentication token expires in 5 minutes.',
);
},
),
_buildButton(
label: 'Info / Guest Prompt',
color: const Color(0xFF1E3A8A),
icon: Icons.person_add_alt_1_rounded,
onPressed: () {
LiveNotification.guestPrompt(
context: context,
position: _position,
message: 'Sign up to chat and receive live alerts!',
actionLabel: 'Sign Up',
onSignUp: () {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text('Sign up button clicked!')),
);
},
);
},
),
],
),
const SizedBox(height: 28),
_buildSectionHeader('2. Automated API & Socket Payloads'),
Text(
'Simulates backend responses automatically parsed by fromApi and fromSocket:',
style: TextStyle(color: Colors.white.withValues(alpha: 0.7)),
),
const SizedBox(height: 12),
Wrap(
spacing: 12,
runSpacing: 12,
children: [
_buildButton(
label: '200 OK REST Response',
color: const Color(0xFF047857),
icon: Icons.cloud_done_rounded,
onPressed: () {
final apiPayload = {
'statusCode': 200,
'data': {
'message': 'Order #4829 successfully placed!',
},
};
LiveNotification.fromApi(apiPayload, position: _position);
},
),
_buildButton(
label: '422 Validation Error Map',
color: const Color(0xFFB91C1C),
icon: Icons.rule_folder_rounded,
onPressed: () {
final apiErrorPayload = {
'statusCode': 422,
'errors': {
'email': ['Email format is invalid.'],
'password': ['Password is too short (min 8 chars).'],
},
};
LiveNotification.fromApi(apiErrorPayload,
position: _position);
},
),
_buildButton(
label: 'WebSocket Event Payload',
color: const Color(0xFF4338CA),
icon: Icons.bolt_rounded,
onPressed: () {
final socketPayload = {
'event': 'bid_placed',
'success': true,
'message': 'Live Bid: \$450.00 by @alex92',
};
LiveNotification.fromSocket(socketPayload,
position: _position);
},
),
],
),
const SizedBox(height: 28),
_buildSectionHeader('3. Context-Free / Interceptor Simulation'),
Text(
'Simulate a background Dio interceptor without passing BuildContext:',
style: TextStyle(color: Colors.white.withValues(alpha: 0.7)),
),
const SizedBox(height: 12),
_buildButton(
label: 'Trigger Without BuildContext',
color: const Color(0xFF0284C7),
icon: Icons.link_off_rounded,
onPressed: () {
// Notice: NO context passed!
LiveNotification.success(
position: _position,
message:
'Triggered directly from Dio interceptor without BuildContext!',
);
},
),
],
),
),
);
}
Widget _buildSectionHeader(String title) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: Text(
title,
style: const TextStyle(
fontSize: 18,
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
);
}
Widget _buildButton({
required String label,
required Color color,
required IconData icon,
required VoidCallback onPressed,
}) {
return ElevatedButton.icon(
style: ElevatedButton.styleFrom(
backgroundColor: color,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
),
icon: Icon(icon, size: 18),
label: Text(label),
onPressed: onPressed,
);
}
}