diff --git a/lib/app_router.dart b/lib/app_router.dart new file mode 100644 index 0000000..05b52cd --- /dev/null +++ b/lib/app_router.dart @@ -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(page: LoginPage, initial: true), + AutoRoute(page: HomePage), + AutoRoute(page: ProfilePage), + ], +) +// extend the generated private router +class AppRouter extends _$AppRouter {} diff --git a/lib/app_router.gr.dart b/lib/app_router.gr.dart new file mode 100644 index 0000000..7fb0213 --- /dev/null +++ b/lib/app_router.gr.dart @@ -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? navigatorKey]) : super(navigatorKey); + + @override + final Map pagesMap = { + LoginRoute.name: (routeData) { + return MaterialPageX( + routeData: routeData, + child: const LoginPage(), + ); + }, + HomeRoute.name: (routeData) { + return MaterialPageX( + routeData: routeData, + child: const HomePage(), + ); + }, + ProfileRoute.name: (routeData) { + return MaterialPageX( + routeData: routeData, + child: const ProfilePage(), + ); + }, + }; + + @override + List 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 { + const LoginRoute() + : super( + LoginRoute.name, + path: '/', + ); + + static const String name = 'LoginRoute'; +} + +/// generated route for +/// [HomePage] +class HomeRoute extends PageRouteInfo { + const HomeRoute() + : super( + HomeRoute.name, + path: '/home-page', + ); + + static const String name = 'HomeRoute'; +} + +/// generated route for +/// [ProfilePage] +class ProfileRoute extends PageRouteInfo { + const ProfileRoute() + : super( + ProfileRoute.name, + path: '/profile-page', + ); + + static const String name = 'ProfileRoute'; +} diff --git a/lib/components/app_drawer.dart b/lib/components/app_drawer.dart new file mode 100644 index 0000000..71bf9c4 --- /dev/null +++ b/lib/components/app_drawer.dart @@ -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 createState() => _AppDrawerState(); +} + +class _AppDrawerState extends State { + @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()); + }, + ), + ], + ), + ); + } +} diff --git a/lib/injection.dart b/lib/injection.dart index 5039b17..2f35399 100644 --- a/lib/injection.dart +++ b/lib/injection.dart @@ -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); diff --git a/lib/main.dart b/lib/main.dart index 006ab70..d525c9f 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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 createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - 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: [ - 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(), ); } } diff --git a/lib/pages/home.dart b/lib/pages/home.dart new file mode 100644 index 0000000..d402381 --- /dev/null +++ b/lib/pages/home.dart @@ -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 createState() => _HomePageState(); +} + +class _HomePageState extends State { + 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!'), + ) + ), + ); + } +} diff --git a/lib/pages/login.dart b/lib/pages/login.dart new file mode 100644 index 0000000..0d500ac --- /dev/null +++ b/lib/pages/login.dart @@ -0,0 +1,81 @@ +// import 'dart:convert'; + +import 'package:auto_route/auto_route.dart'; +import 'package:flutter/material.dart'; + +import '../app_router.dart'; + +class LoginPage extends StatefulWidget { + const LoginPage({Key? key}) : super(key: key); + + @override + State createState() => _LoginState(); +} + +// login form in dart +class _LoginState extends State { + final _formKey = GlobalKey(); + final _emailController = TextEditingController(); + final _passwordController = TextEditingController(); + + // async function to login + Future login(BuildContext context) async { + context.router.push(const HomeRoute()); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Login'), + ), + body: Form( + key: _formKey, + child: Column( + children: [ + TextFormField( + controller: _emailController, + decoration: const InputDecoration( + labelText: 'Email', + ), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + return null; + }, + ), + TextFormField( + controller: _passwordController, + decoration: const InputDecoration( + labelText: 'Password', + ), + validator: (value) { + if (value == null || value.isEmpty) { + return 'Please enter some text'; + } + return null; + }, + ), + ElevatedButton( + onPressed: () { + if (_formKey.currentState!.validate()) { + ScaffoldMessenger.of(context).showSnackBar( + const SnackBar( + content: Text('Processing Data'), + ), + ); + } + + login(context); + }, + child: const Text('Submit'), + ), + ], + ), + ), + ); + } +} + + diff --git a/lib/pages/profile.dart b/lib/pages/profile.dart new file mode 100644 index 0000000..29a79ff --- /dev/null +++ b/lib/pages/profile.dart @@ -0,0 +1,25 @@ +import 'package:flutter/material.dart'; + +import '../components/app_drawer.dart'; + +class ProfilePage extends StatefulWidget { + const ProfilePage({Key? key}) : super(key: key); + + @override + State createState() => _ProfilePageState(); +} + +class _ProfilePageState extends State { + @override + Widget build(BuildContext context) { + return Scaffold( + appBar: AppBar( + title: const Text('Profile'), + ), + drawer: const AppDrawer(), + body: const Center( + child: Text('Your profile!'), + ), + ); + } +} diff --git a/pubspec.lock b/pubspec.lock index f6eb138..8ca5993 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -33,6 +33,22 @@ packages: url: "https://pub.dev" source: hosted version: "2.10.0" + auto_route: + dependency: "direct main" + description: + name: auto_route + sha256: "12047baeca0e01df93165ef33275b32119d72699ab9a49dc64c20e78f586f96d" + url: "https://pub.dev" + source: hosted + version: "5.0.4" + auto_route_generator: + dependency: "direct dev" + description: + name: auto_route_generator + sha256: de5bfbc02ae4eebb339dd90d325749ae7536e903f6513ef72b88954072d72b0e + url: "https://pub.dev" + source: hosted + version: "5.0.3" boolean_selector: dependency: transitive description: @@ -403,6 +419,14 @@ packages: url: "https://pub.dev" source: hosted version: "1.8.2" + petitparser: + dependency: transitive + description: + name: petitparser + sha256: "49392a45ced973e8d94a85fdb21293fbb40ba805fc49f2965101ae748a3683b4" + url: "https://pub.dev" + source: hosted + version: "5.1.0" pool: dependency: transitive description: @@ -568,6 +592,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.3.0" + xml: + dependency: transitive + description: + name: xml + sha256: "979ee37d622dec6365e2efa4d906c37470995871fe9ae080d967e192d88286b5" + url: "https://pub.dev" + source: hosted + version: "6.2.2" yaml: dependency: transitive description: @@ -578,3 +610,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.19.1 <3.0.0" + flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index bd5fe45..2278822 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: flutter: sdk: flutter + auto_route: ^5.0.4 freezed_annotation: ^2.2.0 get_it: ^7.2.0 injectable: ^2.1.0 @@ -44,6 +45,7 @@ dev_dependencies: flutter_test: sdk: flutter + auto_route_generator: ^5.0.3 build_runner: ^2.3.3 freezed: ^2.3.2 injectable_generator: ^2.1.4 diff --git a/test/widget_test.dart b/test/widget_test.dart index e196b3d..e640c78 100644 --- a/test/widget_test.dart +++ b/test/widget_test.dart @@ -13,7 +13,7 @@ import 'package:flutter_bootstrap/main.dart'; void main() { testWidgets('Counter increments smoke test', (WidgetTester tester) async { // Build our app and trigger a frame. - await tester.pumpWidget(const MyApp()); + await tester.pumpWidget(MyApp()); // Verify that our counter starts at 0. expect(find.text('0'), findsOneWidget);