flutter_deep_link_guard 1.0.0
flutter_deep_link_guard: ^1.0.0 copied to clipboard
Declarative route middleware and deep link security guard wrapper for GoRouter in Flutter.
import 'package:flutter/material.dart';
import 'package:flutter_deep_link_guard/flutter_deep_link_guard.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'flutter_deep_link_guard Demo',
theme: ThemeData.dark(useMaterial3: true),
home: const GuardHomeScreen(),
);
}
}
class GuardHomeScreen extends StatefulWidget {
const GuardHomeScreen({super.key});
@override
State<GuardHomeScreen> createState() => _GuardHomeScreenState();
}
class _GuardHomeScreenState extends State<GuardHomeScreen> {
bool _isUserLoggedIn = false;
late final GuardPipeline _pipeline;
@override
void initState() {
super.initState();
_pipeline = GuardPipeline([
AuthGuard(
isAuthenticated: () => _isUserLoggedIn,
fallbackRedirectPath: '/login',
),
]);
}
Future<void> _attemptNavigate(String path) async {
final redirect = await _pipeline.evaluate(GuardContext(location: path));
if (redirect != null) {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('🛑 Access Denied to $path. Redirecting to $redirect'),
backgroundColor: Colors.redAccent,
),
);
}
} else {
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('✅ Access Granted to $path!'),
backgroundColor: Colors.green,
),
);
}
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('flutter_deep_link_guard Demo')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
SwitchListTile(
title: const Text('Simulate User Logged In'),
value: _isUserLoggedIn,
onChanged: (val) => setState(() => _isUserLoggedIn = val),
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () => _attemptNavigate('/dashboard'),
child: const Text('Try Navigating to Protected /dashboard'),
),
],
),
),
);
}
}