gt_navigation 1.0.0
gt_navigation: ^1.0.0 copied to clipboard
A production-ready Flutter navigation package with middleware, deep linking, nested navigation, custom transitions, and type-safe routing.
gt_navigation #
A production-ready Flutter navigation package with middleware, deep linking, nested navigation, custom transitions, and type-safe routing.
β¨ Features #
| Feature | Description |
|---|---|
| π§ Context-Free Navigation | Navigate from anywhere using AppRouter() singleton |
| π‘οΈ Middleware Guards | Auth checks and route protection with automatic redirects |
| π Deep Linking | Handle deep links and notification routes seamlessly |
| π± Nested Navigation | Independent stacks for tabs and multi-pane layouts |
| π¨ Custom Transitions | 7 built-in types: fade, slide, scale, rotation, cupertino, material, none |
| π― Dynamic Routes | Regex-based path matching (e.g., /user/:id/posts/:postId) |
| π Route Observers | Track navigation for analytics and debugging |
π¦ Installation #
Add to your pubspec.yaml:
dependencies:
gt_navigation: ^1.0.0
π Quick Start #
1. Define Routes #
// route_constants.dart
class Routes {
static const String home = '/home';
static const String login = '/login';
static const String userProfile = '/user/:id';
static const String settings = '/settings';
}
2. Create Route Configurations #
// app_pages.dart
class AppPages {
static final List<AppRoute> all = [
AppRoute(
path: Routes.home,
builder: (context, args) => const HomeScreen(),
transition: const AppTransition(type: AppTransitionType.fade),
),
AppRoute(
path: Routes.userProfile,
builder: (context, args) => UserProfileScreen(args: args),
transition: const AppTransition(
type: AppTransitionType.slide,
direction: AppTransitionDirection.bottomToTop,
),
),
AppRoute(
path: Routes.settings,
builder: (context, args) => const SettingsScreen(),
middlewares: [AuthGuardMiddleware()], // Protected route
),
];
}
3. Initialize in main.dart #
void main() {
AppRouter().initialize(
routes: AppPages.all,
deepLinkConfig: const DeepLinkConfig(
scheme: 'myapp',
host: 'example.com',
),
enableLogging: true,
);
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
navigatorKey: AppRouter().navigatorKey,
onGenerateRoute: AppRouter().onGenerateRoute,
navigatorObservers: [AppRouter().routeObserver],
initialRoute: Routes.home,
);
}
}
4. Navigate! #
// Basic navigation
AppRouter().navigateTo(Routes.home);
// With path parameters
AppRouter().navigateTo('/user/123');
// With arguments
AppRouter().navigateTo(
'/user/456',
arguments: AppRouteArguments(
queryParameters: {'tab': 'posts'},
data: {'isAdmin': true},
),
);
// Replace current screen
AppRouter().navigateTo(
Routes.home,
mode: NavigationModes.pushReplacement,
);
// Clear stack (e.g., logout)
AppRouter().navigateTo(
Routes.login,
mode: NavigationModes.pushAndRemoveAll,
);
// Go back
AppRouter().goBack();
π‘οΈ Middleware Example #
class AuthGuardMiddleware extends AppMiddleware {
@override
Future<bool> canNavigate(
BuildContext context,
String route,
AppRouteArguments? args,
) async {
return await AuthService.isLoggedIn();
}
@override
String? getRedirectRoute() => Routes.login;
}
π Documentation #
For detailed documentation on all features including:
- Deep link setup for iOS/Android
- Nested navigation patterns
- Custom transitions
- State restoration
- Testing strategies
See the example folder and API documentation.
π License #
MIT License - see LICENSE for details.