Last active
July 27, 2023 12:00
-
-
Save davidag/d79262144253f84b7117453c402a891a to your computer and use it in GitHub Desktop.
Problem: How to use GoRouter and Flutter Adaptive Scaffold
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'package:flutter_adaptive_scaffold/flutter_adaptive_scaffold.dart'; | |
import 'package:go_router/go_router.dart'; | |
final router = | |
GoRouter(initialLocation: '/', debugLogDiagnostics: true, routes: [ | |
GoRoute( | |
path: '/', | |
builder: (context, state) => const AppScreen(primaryBody: Placeholder()), | |
), | |
ShellRoute( | |
builder: (context, state, child) { | |
return AppScreen( | |
primaryBody: child, | |
// secondaryBody: secondary, ??? | |
); | |
}, | |
routes: [ | |
GoRoute( | |
path: '/main1', | |
builder: (context, state) => const PrimaryBody(type: 'main1'), | |
routes: [ | |
GoRoute( | |
path: 'details/:id', | |
builder: (context, state) => | |
SecondaryBody(id: state.pathParameters['id']!), | |
) | |
]), | |
GoRoute( | |
path: '/main2', | |
builder: (context, state) => const PrimaryBody(type: 'main2'), | |
routes: [ | |
GoRoute( | |
path: 'details/:id', | |
builder: (context, state) => | |
SecondaryBody(id: state.pathParameters['id']!), | |
) | |
]), | |
]) | |
]); | |
void main() { | |
runApp(const MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
const MyApp({super.key}); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
title: 'GoRouter with Adaptive Scaffold', | |
routerConfig: router, | |
); | |
} | |
} | |
class AppScreen extends StatelessWidget { | |
const AppScreen({super.key, required this.primaryBody, this.secondaryBody}); | |
final Widget primaryBody; | |
final Widget? secondaryBody; | |
@override | |
Widget build(BuildContext context) { | |
return AdaptiveScaffold( | |
selectedIndex: _calculateSelectedIndex(context), | |
onSelectedIndexChange: (newIndex) { | |
context.go('/main${newIndex + 1}'); | |
}, | |
destinations: const [ | |
NavigationDestination( | |
icon: Icon(Icons.inbox_outlined), | |
selectedIcon: Icon(Icons.inbox), | |
label: 'Main 1', | |
), | |
NavigationDestination( | |
icon: Icon(Icons.article_outlined), | |
selectedIcon: Icon(Icons.article), | |
label: 'Main 2', | |
), | |
], | |
body: (_) => primaryBody, | |
secondaryBody: secondaryBody != null ? (_) => secondaryBody! : null, | |
); | |
} | |
int? _calculateSelectedIndex(BuildContext context) { | |
return switch (GoRouterState.of(context).uri.toString()) { | |
'/main1' => 0, | |
'/main2' => 1, | |
_ => null, | |
}; | |
} | |
} | |
class PrimaryBody extends StatelessWidget { | |
const PrimaryBody({super.key, required this.type}); | |
final String type; | |
@override | |
Widget build(BuildContext context) { | |
return ListView.builder( | |
itemCount: 10, | |
itemBuilder: (context, index) => Padding( | |
padding: const EdgeInsets.all(20), | |
child: ElevatedButton( | |
child: Text('Element $index ($type)'), | |
onPressed: () { | |
context.go('/$type/details/$index'); | |
}, | |
)), | |
); | |
} | |
} | |
class SecondaryBody extends StatelessWidget { | |
const SecondaryBody({super.key, required this.id}); | |
final String id; | |
@override | |
Widget build(BuildContext context) { | |
return Center( | |
child: Text("Element $id details"), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment