Created with <3 with dartpad.dev.
Created
December 19, 2022 21:22
-
-
Save johnpryan/c9041532bd7cd7d5dfc84172103aac9d to your computer and use it in GitHub Desktop.
ubiquitous-cliff-4392
This file contains hidden or 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'; | |
void main() { | |
runApp(DevToolsApp()); | |
} | |
final _router = GoRouter( | |
routes: [ | |
ShellRoute( | |
builder: (context, state, child) { | |
return AppScaffold( | |
child: child, | |
key: ValueKey('AppScaffold'), | |
); | |
}, | |
routes: [ | |
GoRoute( | |
path: '/', | |
builder: (context, state) => HomeScreen(), | |
), | |
GoRoute( | |
path: '/articles', | |
builder: (context, state) => ArticlesScreen(), | |
), | |
], | |
), | |
], | |
); | |
class DevToolsApp extends StatelessWidget { | |
const DevToolsApp({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp.router( | |
routerConfig: _router, | |
); | |
} | |
} | |
class HomeScreen extends StatelessWidget { | |
const HomeScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.grey, | |
child: Center( | |
child: Container( | |
color: Colors.blue, width: 100, height: 100, child: Text('Home')), | |
), | |
); | |
} | |
} | |
class ArticlesScreen extends StatelessWidget { | |
const ArticlesScreen({Key? key}) : super(key: key); | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
color: Colors.white, | |
child: Center( | |
child: Container( | |
color: Colors.green, | |
width: 100, | |
height: 100, | |
child: Text('Articles')), | |
), | |
); | |
} | |
} | |
class AppScaffold extends StatelessWidget { | |
final Widget child; | |
const AppScaffold({required this.child, Key? key}) : super(key: key); | |
static int _getSelectedIndex(BuildContext context) { | |
final location = GoRouter.of(context).location; | |
late final index; | |
if (location == '/') { | |
index = 0; | |
} else if (location == '/articles') { | |
index = 1; | |
} else { | |
index = 0; | |
} | |
return index; | |
} | |
@override | |
Widget build(BuildContext context) { | |
return AdaptiveScaffold( | |
key: key, | |
selectedIndex: _getSelectedIndex(context), | |
onSelectedIndexChange: (idx) { | |
String path = "/"; | |
switch (idx) { | |
case 0: | |
path = "/"; | |
break; | |
case 1: | |
path = "/articles"; | |
break; | |
} | |
// if (Navigator.of(context).canPop()) { | |
// Navigator.of(context).pop(); | |
// } | |
context.go(path); | |
}, | |
destinations: [ | |
NavigationDestination( | |
icon: Icon(Icons.home), | |
label: 'Home', | |
), | |
NavigationDestination( | |
icon: Icon(Icons.article), | |
label: 'Articles', | |
), | |
], | |
body: (_) => child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment