native_toast_pro 1.0.0
native_toast_pro: ^1.0.0 copied to clipboard
A production-ready Flutter plugin providing unified native toast experience across Android and iOS platforms with clean API and robust error handling.
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:native_toast_pro/native_toast_pro.dart';
import 'package:native_toast_pro/native_toast_pro_platform_interface.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _platformVersion = 'Unknown';
bool _isSupported = false;
@override
void initState() {
super.initState();
initPlatformState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
String platformVersion;
bool isSupported;
try {
platformVersion =
await NativeToastProPlatform.instance.getPlatformVersion() ??
'Unknown platform version';
isSupported = await NativeToastPro.isSupported;
} on PlatformException {
platformVersion = 'Failed to get platform version.';
isSupported = false;
}
if (!mounted) return;
setState(() {
_platformVersion = platformVersion;
_isSupported = isSupported;
});
}
void _showBasicToast() {
try {
NativeToastPro.show('Hello Native Toast!');
} on PlatformException catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Toast error: ${e.message}')));
}
}
void _showCustomToast() {
try {
NativeToastPro.show(
'This is a longer toast message to demonstrate how the native toast system handles text wrapping and display on both Android and iOS platforms!',
);
} on PlatformException catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Toast error: ${e.message}')));
}
}
void _showMultipleToasts() {
try {
NativeToastPro.showWithoutRateLimit('First toast');
Future.delayed(const Duration(milliseconds: 100), () {
NativeToastPro.showWithoutRateLimit('Second toast');
Future.delayed(const Duration(milliseconds: 100), () {
NativeToastPro.showWithoutRateLimit('Third toast');
});
});
} on PlatformException catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Toast error: ${e.message}')));
}
}
void _testErrorHandling() {
try {
NativeToastPro.show(''); // Empty message should throw error
} on ArgumentError catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Expected error: ${e.message}')));
} catch (e) {
ScaffoldMessenger.of(
context,
).showSnackBar(SnackBar(content: Text('Unexpected error: $e')));
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Native Toast Pro Example'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Card(
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'Platform Information',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
Text('Platform: $_platformVersion'),
Text(
'Toast Support: ${_isSupported ? "✅ Available" : "❌ Not Available"}',
),
],
),
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: _showBasicToast,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text('Show Basic Toast'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _showCustomToast,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.green,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text('Show Long Toast Message'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _showMultipleToasts,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text('Show Multiple Toasts (No Rate Limit)'),
),
const SizedBox(height: 12),
ElevatedButton(
onPressed: _testErrorHandling,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 12),
),
child: const Text('Test Error Handling'),
),
const SizedBox(height: 20),
Card(
color: Colors.grey[100],
child: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'About Native Toast Pro',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
const Text(
'• Android: Uses native Android Toast API\n'
'• iOS: Custom UIView-based toast implementation\n'
'• Rate limiting prevents spam (500ms)\n'
'• Comprehensive error handling\n'
'• Production-ready architecture',
style: TextStyle(fontSize: 14),
),
],
),
),
),
],
),
),
),
);
}
}