Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

login #26

Merged
merged 12 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 74 additions & 7 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,31 +1,98 @@
import 'dart:io';

import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:intl/date_symbol_data_local.dart';
import 'package:intl/intl.dart';
import 'package:intl/intl_standalone.dart' if (dart.library.html) 'package:intl/intl_browser.dart';
import 'pages/login.dart';

import 'routes/route.gr.dart';
import 'services/login.dart';
import 'theme.dart';

// TODO(mael): create certificate
class MyHttpOverrides extends HttpOverrides {
@override
HttpClient createHttpClient(SecurityContext? context) {
return super.createHttpClient(context)
..badCertificateCallback = (X509Certificate cert, String host, int port) => true;
}
}

void main() async {
HttpOverrides.global = MyHttpOverrides();
Intl.systemLocale = await findSystemLocale();
runApp(const MyApp());
runApp(CPM());
}

final LoginState loginState = LoginState();
final GlobalKey<ScaffoldMessengerState> scaffoldMessengerKey = GlobalKey<ScaffoldMessengerState>();

class LoginState extends ChangeNotifier {
bool authenticated = false;
String apiToken = '';
int statusCode = 0;
String reasonPhrase = '';

Future<void> login(String username, String password) async {
final List<dynamic> result = await LoginService().login(username, password);
authenticated = result[0] as bool;
apiToken = result[1] as String;
statusCode = result[2] as int;
reasonPhrase = result[3] as String;
notifyListeners();
}

Future<void> logout() async {
authenticated = false;
apiToken = '';
statusCode = 0;
reasonPhrase = '';
notifyListeners();
}
}

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

final AppRouter _appRouter = AppRouter();

@override
State<MyApp> createState() => _MyAppState();
State<CPM> createState() => _CPMState();
}

class _MyAppState extends State<MyApp> {
class _CPMState extends State<CPM> {
@override
void initState() {
super.initState();
loginState.addListener(() => setState(() {}));
initializeDateFormatting();
}

@override
Widget build(BuildContext context) {
return MaterialApp(title: 'CPM', theme: CPMThemeLight().theme, home: const Login());
return MaterialApp.router(
routerDelegate: AutoRouterDelegate.declarative(
widget._appRouter,
routes: (_) => <PageRouteInfo<dynamic>>[
if (loginState.authenticated)
const HomeRoute()
else
LoginRoute(
onLogin: _handleLogin,
),
],
),
routeInformationParser: widget._appRouter.defaultRouteParser(includePrefixMatches: true),
title: 'CPM',
theme: CPMThemeLight().theme);
}

Future<void> _handleLogin(String username, String password) async {
loginState.login(username, password).then((void value) {
if (loginState.statusCode != 200 && scaffoldMessengerKey.currentContext != null) {
ScaffoldMessenger.of(scaffoldMessengerKey.currentContext!).showSnackBar(LoginSnackBar().generateSnackBar());
}
});
}
}
16 changes: 12 additions & 4 deletions lib/models/member.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
class Member {
Member(this.firstName, this.lastName, this.phone);
const Member({required this.firstName, required this.lastName, required this.phone});

String firstName;
String lastName;
String phone;
factory Member.fromJson(Map<String, dynamic> json) {
return Member(
firstName: json['firstName'].toString(),
lastName: json['lastName'].toString(),
phone: json['phone'].toString(),
);
}

final String firstName;
final String lastName;
final String phone;
}
10 changes: 4 additions & 6 deletions lib/pages/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@ import 'dart:io' show Platform;

import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';

import '../main.dart';
import '../models/location.dart';
import '../models/member.dart';
import '../widgets/custom_appbar.dart';
import 'information.dart';
import 'locations.dart';
import 'login.dart';
import 'members.dart';
import 'projects.dart';
import 'test.dart';
Expand Down Expand Up @@ -97,8 +96,7 @@ class HomeState extends State<Home> {
child: IconButton(
icon: const Icon(Icons.logout),
onPressed: () {
Navigator.push(
context, PageTransition<Login>(type: PageTransitionType.topToBottom, child: const Login()));
loginState.logout();
}),
),
),
Expand All @@ -120,8 +118,8 @@ class HomeState extends State<Home> {
case 0:
return const Projects();
case 1:
return Members(
members: <Member>[Member('Jean', 'Neymar', '0123456789'), Member('Paul', 'Issier', '0123456788')],
return const Members(
members: <Member>[Member(firstName: 'Paul', lastName: 'Issière', phone: '69')],
);
case 2:
return Locations(
Expand Down
123 changes: 76 additions & 47 deletions lib/pages/login.dart
Original file line number Diff line number Diff line change
@@ -1,61 +1,90 @@
import 'package:flutter/material.dart';
import 'package:page_transition/page_transition.dart';

import 'home.dart';
import '../main.dart';

class Login extends StatelessWidget {
const Login({super.key});
class Login extends StatefulWidget {
const Login({super.key, required this.onLogin});

final void Function(String, String) onLogin;

@override
State<Login> createState() => _LoginState();
}

class _LoginState extends State<Login> {
final TextEditingController usernameController = TextEditingController();
final TextEditingController passwordController = TextEditingController();

@override
Widget build(BuildContext context) {
return Scaffold(
key: scaffoldMessengerKey,
body: SafeArea(
child: Center(
child: Column(
children: <Widget>[
const Spacer(),
Padding(
padding: const EdgeInsets.only(bottom: 64.0),
child: Image.asset('assets/logo-camera.png', fit: BoxFit.fitWidth, width: 250),
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: SizedBox(
width: 300,
child: TextField(
decoration: InputDecoration(border: OutlineInputBorder(), isDense: true, labelText: 'Login'),
child: Center(
child: Column(
children: <Widget>[
const Spacer(),
Padding(
padding: const EdgeInsets.only(bottom: 64.0),
child: Image.asset('assets/logo-camera.png', fit: BoxFit.fitWidth, width: 250),
),
),
),
const Padding(
padding: EdgeInsets.symmetric(vertical: 8.0),
child: SizedBox(
width: 300,
child: TextField(
enableSuggestions: false,
autocorrect: false,
obscureText: true,
decoration: InputDecoration(border: OutlineInputBorder(), isDense: true, labelText: 'Password'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: SizedBox(
width: 300,
child: Focus(
canRequestFocus: false,
child: TextField(
autofocus: true,
textInputAction: TextInputAction.next,
controller: usernameController,
onEditingComplete: () {
FocusScope.of(context).nextFocus();
},
decoration:
const InputDecoration(border: OutlineInputBorder(), isDense: true, labelText: 'Username'),
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: SizedBox(
width: 300,
child: FilledButton(
onPressed: () {
Navigator.push(
context, PageTransition<Home>(type: PageTransitionType.bottomToTop, child: const Home()));
},
child: const Text('Log in'),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: SizedBox(
width: 300,
child: TextField(
controller: passwordController,
enableSuggestions: false,
autocorrect: false,
obscureText: true,
onEditingComplete: () {
FocusScope.of(context).unfocus();
submit();
},
decoration:
const InputDecoration(border: OutlineInputBorder(), isDense: true, labelText: 'Password'),
),
),
),
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: SizedBox(
width: 300,
child: FilledButton(
onPressed: () {
submit();
},
child: const Text('Log in'),
),
),
),
),
const Spacer()
],
),
const Spacer()
],
),
),
));
),
));
}

void submit() {
widget.onLogin(usernameController.text, passwordController.text);
}
}
23 changes: 23 additions & 0 deletions lib/routes/route.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:auto_route/annotations.dart';
import 'package:auto_route/auto_route.dart';

import '../pages/home.dart';
import '../pages/information.dart';
import '../pages/login.dart';
import '../pages/members.dart';
import '../pages/test.dart';

// @CupertinoAutoRouter
// @AdaptiveAutoRouter
// @CustomAutoRouter
@MaterialAutoRouter(
replaceInRouteName: 'Page,Route',
routes: <AutoRoute>[
AutoRoute(page: Login, name: 'LoginRoute', path: '/login', initial: true),
AutoRoute(page: Home, name: 'HomeRoute', path: '/home'),
AutoRoute(page: Members, name: 'MembersRoute'),
AutoRoute(page: Information, name: 'InformationRoute'),
AutoRoute(page: Test, name: 'TestRoute'),
],
)
class $AppRouter {}
Loading