Skip to content

Commit

Permalink
Add auto_router and mock pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pmaite88 committed Feb 24, 2023
1 parent e12d23e commit c025104
Show file tree
Hide file tree
Showing 11 changed files with 356 additions and 107 deletions.
19 changes: 19 additions & 0 deletions lib/app_router.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';
import 'package:auto_route/auto_route.dart';

import 'pages/login.dart';
import 'pages/home.dart';
import 'pages/profile.dart';

part 'app_router.gr.dart';

@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(page: LoginPage, initial: true),
AutoRoute(page: HomePage),
AutoRoute(page: ProfilePage),
],
)
// extend the generated private router
class AppRouter extends _$AppRouter {}
91 changes: 91 additions & 0 deletions lib/app_router.gr.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// **************************************************************************
// AutoRouteGenerator
// **************************************************************************

// GENERATED CODE - DO NOT MODIFY BY HAND

// **************************************************************************
// AutoRouteGenerator
// **************************************************************************
//
// ignore_for_file: type=lint

part of 'app_router.dart';

class _$AppRouter extends RootStackRouter {
_$AppRouter([GlobalKey<NavigatorState>? navigatorKey]) : super(navigatorKey);

@override
final Map<String, PageFactory> pagesMap = {
LoginRoute.name: (routeData) {
return MaterialPageX<dynamic>(
routeData: routeData,
child: const LoginPage(),
);
},
HomeRoute.name: (routeData) {
return MaterialPageX<dynamic>(
routeData: routeData,
child: const HomePage(),
);
},
ProfileRoute.name: (routeData) {
return MaterialPageX<dynamic>(
routeData: routeData,
child: const ProfilePage(),
);
},
};

@override
List<RouteConfig> get routes => [
RouteConfig(
LoginRoute.name,
path: '/',
),
RouteConfig(
HomeRoute.name,
path: '/home-page',
),
RouteConfig(
ProfileRoute.name,
path: '/profile-page',
),
];
}

/// generated route for
/// [LoginPage]
class LoginRoute extends PageRouteInfo<void> {
const LoginRoute()
: super(
LoginRoute.name,
path: '/',
);

static const String name = 'LoginRoute';
}

/// generated route for
/// [HomePage]
class HomeRoute extends PageRouteInfo<void> {
const HomeRoute()
: super(
HomeRoute.name,
path: '/home-page',
);

static const String name = 'HomeRoute';
}

/// generated route for
/// [ProfilePage]
class ProfileRoute extends PageRouteInfo<void> {
const ProfileRoute()
: super(
ProfileRoute.name,
path: '/profile-page',
);

static const String name = 'ProfileRoute';
}
43 changes: 43 additions & 0 deletions lib/components/app_drawer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';

import '../app_router.dart';

// statefull app drawer
class AppDrawer extends StatefulWidget {
const AppDrawer({Key? key}) : super(key: key);

@override
State<AppDrawer> createState() => _AppDrawerState();
}

class _AppDrawerState extends State<AppDrawer> {
@override
Widget build(BuildContext context) {
return Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: [
const DrawerHeader(
decoration: BoxDecoration(
color: Colors.green
),
child: Text('Drawer Header'),
),
ListTile(
title: const Text('Home'),
onTap: () {
context.router.push(const HomeRoute());
},
),
ListTile(
title: const Text('Profile'),
onTap: () {
context.router.push(const ProfileRoute());
},
),
],
),
);
}
}
6 changes: 3 additions & 3 deletions lib/injection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'injection.config.dart';
final getIt = GetIt.instance;

@InjectableInit(
initializerName: r'$initGetIt', // default
preferRelativeImports: true, // default
asExtension: false, // default
initializerName: r'$initGetIt',
preferRelativeImports: true,
asExtension: false,
)
void configureDependencies() => $initGetIt(getIt);
111 changes: 8 additions & 103 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,118 +1,23 @@
import 'package:flutter/material.dart';

import 'injection.dart';
import 'app_router.dart';

void main() {
configureDependencies();
runApp(const MyApp());
runApp(MyApp());
}

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

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".

final String title;

@override
State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
final _appRouter = AppRouter();

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug painting" (press "p" in the console, choose the
// "Toggle Debug Paint" action from the Flutter Inspector in Android
// Studio, or the "Toggle Debug Paint" command in Visual Studio Code)
// to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
Widget build(BuildContext context){
return MaterialApp.router(
routerDelegate: _appRouter.delegate(),
routeInformationParser: _appRouter.defaultRouteParser(),
);
}
}
50 changes: 50 additions & 0 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';

import '../components/app_drawer.dart';

class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);

@override
State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
void showAlert(String message) {
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: const Text('Alert'),
content: Text(message),
actions: [
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text('OK')
),
],
);
},
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home'),
),
drawer: const AppDrawer(),
body: Center(
child: ElevatedButton(
onPressed: () {
showAlert('You are in the home screen!');
},
child: const Text('Hello!'),
)
),
);
}
}
Loading

0 comments on commit c025104

Please sign in to comment.