step 3 : login proses
This commit is contained in:
66
lib/screen/home/home_screen.dart
Normal file
66
lib/screen/home/home_screen.dart
Normal file
@@ -0,0 +1,66 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
|
||||
class HomeScreen extends HookConsumerWidget {
|
||||
const HomeScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [
|
||||
SystemUiOverlay.bottom,
|
||||
]);
|
||||
|
||||
final currentUser = ref.watch(currentUserProvider);
|
||||
// final username = currentUser?.model.username ?? "-";
|
||||
final host = currentUser?.host ?? "";
|
||||
final isLoading = useState<bool>(false);
|
||||
final userId = currentUser?.model.userId ?? "";
|
||||
|
||||
useEffect(() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final userID = currentUser?.model.userId ?? "0";
|
||||
if (userID == "0") {
|
||||
// not logged in
|
||||
Navigator.of(context)
|
||||
.pushNamedAndRemoveUntil(loginRoute, (route) => false);
|
||||
return;
|
||||
}
|
||||
});
|
||||
return () {};
|
||||
}, [currentUser]);
|
||||
|
||||
// listRiwayProvider
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
FocusManager.instance.primaryFocus!.unfocus();
|
||||
},
|
||||
child: Scaffold(
|
||||
resizeToAvoidBottomInset: true,
|
||||
backgroundColor: Constant.bgGrey,
|
||||
body: Column(
|
||||
children: [
|
||||
// atas
|
||||
Image.asset(
|
||||
'images/vektoratas.png',
|
||||
width: double.infinity,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 34,
|
||||
),
|
||||
),
|
||||
Text(currentUser?.model.username ?? ""),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
89
lib/screen/login/login_provider.dart
Normal file
89
lib/screen/login/login_provider.dart
Normal file
@@ -0,0 +1,89 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../model/auth_model.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
import '../../provider/dio_provider.dart';
|
||||
import '../../repository/auth_repository.dart';
|
||||
import '../../repository/base_repository.dart';
|
||||
|
||||
// 3. state provider
|
||||
final loginProvider = StateNotifierProvider<LoginNotifier, LoginState>(
|
||||
(ref) => LoginNotifier(ref: ref));
|
||||
|
||||
// 2. notifier
|
||||
class LoginNotifier extends StateNotifier<LoginState> {
|
||||
final Ref ref;
|
||||
LoginNotifier({required this.ref}) : super(LoginStateInit());
|
||||
void login({
|
||||
required String username,
|
||||
required String host,
|
||||
required String password,
|
||||
}) async {
|
||||
try {
|
||||
state = LoginStateLoading();
|
||||
final resp = await AuthRepository(
|
||||
dio: ref.read(dioProvider),
|
||||
).login(
|
||||
username: username,
|
||||
host: host,
|
||||
password: password,
|
||||
);
|
||||
|
||||
// print(resp);
|
||||
state = LoginStateDone(model: resp);
|
||||
//Simpan ke token
|
||||
final shared = await SharedPreferences.getInstance();
|
||||
final token = jsonEncode({
|
||||
"host":host,
|
||||
"date": DateTime.now().toString(),
|
||||
"model": resp.model,
|
||||
"token": resp.token
|
||||
});
|
||||
await shared.setString(Constant.bearerName, token);
|
||||
ref.read(currentUserProvider.notifier).state = resp;
|
||||
|
||||
// print(shared.getString(Constant.bearerName));
|
||||
} catch (e) {
|
||||
if (e is BaseRepositoryException) {
|
||||
state = LoginStateError(message: e.message);
|
||||
} else {
|
||||
state = LoginStateError(message: e.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 1. state
|
||||
abstract class LoginState extends Equatable {
|
||||
final DateTime date;
|
||||
const LoginState(this.date);
|
||||
@override
|
||||
List<Object?> get props => [date];
|
||||
}
|
||||
|
||||
class LoginStateInit extends LoginState {
|
||||
LoginStateInit() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class LoginStateLoading extends LoginState {
|
||||
LoginStateLoading() : super(DateTime.now());
|
||||
}
|
||||
|
||||
class LoginStateError extends LoginState {
|
||||
final String message;
|
||||
LoginStateError({
|
||||
required this.message,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
|
||||
class LoginStateDone extends LoginState {
|
||||
final AuthModel model;
|
||||
LoginStateDone({
|
||||
required this.model,
|
||||
}) : super(DateTime.now());
|
||||
}
|
||||
421
lib/screen/login/login_screen.dart
Normal file
421
lib/screen/login/login_screen.dart
Normal file
@@ -0,0 +1,421 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:toastification/toastification.dart';
|
||||
import '../../app/route.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
import 'login_provider.dart';
|
||||
|
||||
class LoginScreen extends HookConsumerWidget {
|
||||
const LoginScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [
|
||||
SystemUiOverlay.bottom,
|
||||
]);
|
||||
|
||||
final usernameCtr = useTextEditingController(
|
||||
text: "",
|
||||
);
|
||||
|
||||
final passwordCtr = useTextEditingController(
|
||||
text: "",
|
||||
);
|
||||
|
||||
final hostCtr = useTextEditingController(
|
||||
text: "",
|
||||
);
|
||||
|
||||
final isLoading = useState(false);
|
||||
final isSuccess = useState(false);
|
||||
|
||||
ToastificationItem? toastItem;
|
||||
|
||||
void showLongToast(
|
||||
String title,
|
||||
String message,
|
||||
String typeToast,
|
||||
Duration waktu,
|
||||
) {
|
||||
if (typeToast == "success") {
|
||||
toastItem = toastification.show(
|
||||
context: context,
|
||||
title: Text(title),
|
||||
description: Text(message),
|
||||
autoCloseDuration: waktu,
|
||||
type: ToastificationType.success,
|
||||
);
|
||||
} else if (typeToast == "error") {
|
||||
toastItem = toastification.show(
|
||||
context: context,
|
||||
title: Text(title),
|
||||
description: Text(message),
|
||||
autoCloseDuration: waktu,
|
||||
type: ToastificationType.error,
|
||||
style: ToastificationStyle.fillColored,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void hideToast() {
|
||||
if (toastItem != null) {
|
||||
toastification.dismissAll();
|
||||
}
|
||||
}
|
||||
|
||||
// proses login
|
||||
ref.listen(loginProvider, (prev, next) {
|
||||
if (next is LoginStateLoading) {
|
||||
isLoading.value = true;
|
||||
} else if (next is LoginStateError) {
|
||||
isLoading.value = false;
|
||||
// errorMessage.value = next.message;
|
||||
showLongToast(
|
||||
'Error',
|
||||
next.message,
|
||||
'error',
|
||||
Duration(seconds: 3),
|
||||
);
|
||||
} else if (next is LoginStateDone) {
|
||||
hideToast();
|
||||
isLoading.value = false;
|
||||
isSuccess.value = true;
|
||||
ref.read(currentUserProvider.notifier).state = next.model;
|
||||
Navigator.of(context)
|
||||
.pushNamedAndRemoveUntil(homeRoute, (route) => false);
|
||||
}
|
||||
});
|
||||
|
||||
void login() {
|
||||
if (usernameCtr.text.isEmpty ||
|
||||
passwordCtr.text.isEmpty ||
|
||||
hostCtr.text.isEmpty) {
|
||||
showLongToast(
|
||||
'Error',
|
||||
'Inputan wajib diisi',
|
||||
'error',
|
||||
Duration(seconds: 3),
|
||||
);
|
||||
} else {
|
||||
// print('proses login');
|
||||
ref.read(loginProvider.notifier).login(
|
||||
username: usernameCtr.text,
|
||||
host: hostCtr.text,
|
||||
password: passwordCtr.text,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () {
|
||||
FocusManager.instance.primaryFocus!.unfocus();
|
||||
},
|
||||
child: Scaffold(
|
||||
resizeToAvoidBottomInset: true,
|
||||
backgroundColor: Constant.textWhite,
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// atas
|
||||
Image.asset(
|
||||
'images/vektoratas.png',
|
||||
width: double.infinity,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 40,
|
||||
),
|
||||
),
|
||||
// konten didalamnya
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Selamat Datang',
|
||||
style: Constant.title_700(context: context).copyWith(
|
||||
color: Constant.textBlack,
|
||||
),
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Silahkan masuk untuk mengakses akun Anda',
|
||||
style: Constant.title_400(context: context).copyWith(
|
||||
color: Constant.textBlack,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 64,
|
||||
),
|
||||
),
|
||||
|
||||
// inputan
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Username',
|
||||
style: Constant.title_400(context: context).copyWith(
|
||||
color: Constant.inputanGrey,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 16,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: TextField(
|
||||
controller: usernameCtr,
|
||||
decoration: InputDecoration(
|
||||
hintText: "Masukkan Username",
|
||||
enabledBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.grey,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
focusedBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.blue,
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 20,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Password',
|
||||
style: Constant.title_400(context: context).copyWith(
|
||||
color: Constant.inputanGrey,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 16,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: TextField(
|
||||
controller: passwordCtr,
|
||||
obscureText: true,
|
||||
decoration: InputDecoration(
|
||||
hintText: "Masukkan Password",
|
||||
enabledBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.grey,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
focusedBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.blue,
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 20,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: Text(
|
||||
'Host',
|
||||
style: Constant.title_400(context: context).copyWith(
|
||||
color: Constant.inputanGrey,
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 16,
|
||||
),
|
||||
),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: TextField(
|
||||
controller: hostCtr,
|
||||
decoration: InputDecoration(
|
||||
hintText: "Masukkan Host",
|
||||
enabledBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.grey,
|
||||
width: 1,
|
||||
),
|
||||
),
|
||||
focusedBorder: const OutlineInputBorder(
|
||||
borderSide: BorderSide(
|
||||
color: Colors.blue,
|
||||
width: 2,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 64,
|
||||
),
|
||||
),
|
||||
// inputan
|
||||
|
||||
// button login
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
left: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
right: Constant.getActualXPhone(
|
||||
context: context,
|
||||
x: 20,
|
||||
),
|
||||
),
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 48,
|
||||
),
|
||||
child: ElevatedButton(
|
||||
onPressed: () async {
|
||||
(isLoading.value || (isSuccess.value == true))
|
||||
? null
|
||||
: login();
|
||||
},
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Constant.bgButton,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
elevation: 8,
|
||||
shadowColor: Constant.bgButton.withOpacity(0.24),
|
||||
),
|
||||
child: Text(
|
||||
(isLoading.value) ? 'Loading...' : 'LOGIN',
|
||||
style: Constant.titleButton500(context: context).copyWith(
|
||||
color: Constant.textWhite,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
// konten didalamnya
|
||||
|
||||
SizedBox(
|
||||
height: Constant.getActualYPhone(
|
||||
context: context,
|
||||
y: 60,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
113
lib/screen/splash/splash_screen.dart
Normal file
113
lib/screen/splash/splash_screen.dart
Normal file
@@ -0,0 +1,113 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_hooks/flutter_hooks.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../app/constant.dart';
|
||||
import '../../app/route.dart';
|
||||
import '../../model/auth_model.dart';
|
||||
import '../../provider/current_user_provider.dart';
|
||||
|
||||
class SplashScreen extends HookConsumerWidget {
|
||||
const SplashScreen({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context, WidgetRef ref) {
|
||||
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [
|
||||
SystemUiOverlay.bottom,
|
||||
]);
|
||||
|
||||
useEffect(() {
|
||||
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
|
||||
final shared = await SharedPreferences.getInstance();
|
||||
final bearerString = shared.getString(Constant.bearerName);
|
||||
|
||||
if (bearerString == null || bearerString == "null") {
|
||||
Timer(const Duration(seconds: 3), () {
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
loginRoute,
|
||||
(route) => false,
|
||||
);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
final xmodel = jsonDecode(bearerString);
|
||||
if (xmodel == null) return;
|
||||
|
||||
final authModel = AuthModel(
|
||||
host: xmodel["host"],
|
||||
token: xmodel["token"],
|
||||
model: UserModel(
|
||||
userId: xmodel["model"]['M_UserID'],
|
||||
username: xmodel["model"]['M_UserUsername'],
|
||||
groupDashboard: xmodel["model"]['M_UserGroupDashboard'],
|
||||
defaultSampleStationId: xmodel["model"]
|
||||
['M_UserDefaultT_SampleStationID'],
|
||||
staffName: xmodel["model"]['M_StaffName'],
|
||||
isCourier: xmodel["model"]['is_courier'],
|
||||
timeAutoLogout: xmodel["model"]['time_autologout'],
|
||||
ip: xmodel["model"]['ip'],
|
||||
agent: xmodel["model"]['agent'],
|
||||
version: xmodel["model"]['version'],
|
||||
lastLogin: xmodel["model"]['last-login'],
|
||||
satelliteId: xmodel["model"]['M_SatelliteID'],
|
||||
),
|
||||
);
|
||||
|
||||
ref.read(currentUserProvider.notifier).state = authModel;
|
||||
|
||||
Timer(const Duration(seconds: 3), () {
|
||||
Navigator.of(context).pushNamedAndRemoveUntil(
|
||||
homeRoute,
|
||||
(route) => false,
|
||||
);
|
||||
});
|
||||
});
|
||||
return () {};
|
||||
}, []);
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Constant.textWhite,
|
||||
body: Column(
|
||||
children: [
|
||||
// Bagian atas
|
||||
Align(
|
||||
alignment: Alignment.topRight,
|
||||
child: SizedBox(
|
||||
width: Constant.getActualXPhone(context: context, x: 246),
|
||||
child: Image.asset(
|
||||
'images/splashatas.png',
|
||||
fit: BoxFit.fitWidth,
|
||||
),
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
// Logo di tengah
|
||||
Center(
|
||||
child: Image.asset(
|
||||
'images/logo.png',
|
||||
width: Constant.getActualXPhone(context: context, x: 164),
|
||||
),
|
||||
),
|
||||
Spacer(),
|
||||
// Bagian bawah
|
||||
Align(
|
||||
alignment: Alignment.bottomLeft,
|
||||
child: SizedBox(
|
||||
width: Constant.getActualXPhone(context: context, x: 246),
|
||||
child: Image.asset(
|
||||
'images/splashbawah.png',
|
||||
fit: BoxFit.fitWidth,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user