Skip to content

Commit

Permalink
feat: Persisted widget with named route-based navigation. #1
Browse files Browse the repository at this point in the history
  • Loading branch information
LuchoTurtle committed Feb 14, 2023
1 parent 7d7d118 commit 41555ed
Showing 1 changed file with 86 additions and 6 deletions.
92 changes: 86 additions & 6 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ class App extends StatelessWidget {
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const HomePage(),
debugShowCheckedModeBanner: false,
builder: (context, child) => BaseWidget(child: child),
initialRoute: '/',
routes: {
'/': (context) => FirstRoute(),
'/second': (context) => SecondRoute(),
},
);
}
}
Expand Down Expand Up @@ -82,8 +88,7 @@ class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin
),
backgroundColor: Colors.black,
elevation: 0.0,
//automaticallyImplyLeading: false,
/*
automaticallyImplyLeading: false,
actions: [
AnimatedBuilder(
animation: _menuSlideController,
Expand All @@ -103,11 +108,9 @@ class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin
},
),
],
*/
),
body: Stack(
children: [
const SizedBox(),
AnimatedBuilder(
animation: _menuSlideController,
builder: (context, child) {
Expand All @@ -119,7 +122,84 @@ class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin
),
],
),
drawer: const DrawerMenu(),
);
}
}

class BaseWidget extends StatelessWidget {
final Widget? child;
const BaseWidget({super.key, this.child});

List<Widget> _showChild() {
if (child != null) {
return [
Expanded(child: child!),
];
} else {
return [];
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'some menu',
style: TextStyle(
color: Colors.white,
),
)),
body: Column(
children: _showChild(),
),
);
}
}

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

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
const Text("Route 1"),
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, '/second');
},
child: const Text('Go to second'),
),
],
),
),
);
}
}


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

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
children: [
const Text("Route 2"),
ElevatedButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('Go back to first!'),
),
],
),
),
);
}
}

0 comments on commit 41555ed

Please sign in to comment.