route_guard 0.0.1 copy "route_guard: ^0.0.1" to clipboard
route_guard: ^0.0.1 copied to clipboard

A lightweight Flutter package to add async or conditional navigation guards to routes, similar to middleware.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:route_guard/route_guard.dart';

bool isLoggedIn = false;

void main() => runApp(const MyApp());

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      initialRoute: '/',
      routes: {
        '/': (_) => const LoginPage(),
        '/home': (_) => RouteGuard(
              guard: (_) async => isLoggedIn,
              redirectTo: '/',
              child: const HomePage(),
            ),
      },
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Login Page')),
      body: Center(
        child: ElevatedButton(
          onPressed: () {
            isLoggedIn = true;
            Navigator.pushNamed(context, '/home');
          },
          child: const Text('Login and Go to Home'),
        ),
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Home Page')),
      body: const Center(child: Text('Welcome! You are logged in.')),
    );
  }
}
1
likes
150
points
9
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A lightweight Flutter package to add async or conditional navigation guards to routes, similar to middleware.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter

More

Packages that depend on route_guard