Skip to content

Instantly share code, notes, and snippets.

@johnpryan
Created December 19, 2022 21:22
Show Gist options
  • Save johnpryan/c9041532bd7cd7d5dfc84172103aac9d to your computer and use it in GitHub Desktop.
Save johnpryan/c9041532bd7cd7d5dfc84172103aac9d to your computer and use it in GitHub Desktop.
ubiquitous-cliff-4392

ubiquitous-cliff-4392

Created with <3 with dartpad.dev.

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