feat: add logout features

This commit is contained in:
Syaroful 2024-10-16 14:25:33 +07:00
parent 0e89909893
commit bbc4d6a632
7 changed files with 221 additions and 155 deletions

View File

@ -64,6 +64,36 @@ class AppService {
} }
} }
// logout
Future<LoginResponse> logout() async {
final SharedPreferences pref = await SharedPreferences.getInstance();
final String auth = 'Bearer ${pref.getString('token')}';
try {
final response = await _dioWithoutInterceptor.post(
'/auth/logout',
options: Options(
headers: {'Authorization': auth},
),
);
if (response.statusCode == 200) {
final data = LoginResponse.fromJson(response.data);
pref.remove('token');
pref.remove('jwtToken');
pref.remove('username');
pref.remove('email');
pref.remove('fullName');
pref.setBool('isLoggedIn', false);
return data;
} else {
throw Exception('Failed to load data');
}
} on DioException catch (e) {
final String errorMessage = e.response?.data['message'];
throw (errorMessage);
}
}
Future<RelayResponse> getRelayStatus() async { Future<RelayResponse> getRelayStatus() async {
final SharedPreferences pref = await SharedPreferences.getInstance(); final SharedPreferences pref = await SharedPreferences.getInstance();
final String auth = 'Bearer ${pref.getString('token')}'; final String auth = 'Bearer ${pref.getString('token')}';

View File

@ -96,7 +96,9 @@ class ControlProvider extends ChangeNotifier {
if (result.success == true) { if (result.success == true) {
relayState = ResultState.hasData; relayState = ResultState.hasData;
notifyListeners(); notifyListeners();
print(result.message); if (kDebugMode) {
print(result.message);
}
return true; return true;
} else { } else {
return false; return false;

View File

@ -33,7 +33,7 @@ class ConductivityScreen extends StatelessWidget {
padding: EdgeInsets.only(right: 16), padding: EdgeInsets.only(right: 16),
child: Icon( child: Icon(
Icons.electric_bolt_rounded, Icons.electric_bolt_rounded,
color: Colors.green, color: Colors.teal,
), ),
) )
], ],

View File

@ -32,8 +32,8 @@ class PhScreen extends StatelessWidget {
Padding( Padding(
padding: EdgeInsets.only(right: 16), padding: EdgeInsets.only(right: 16),
child: Icon( child: Icon(
BootstrapIcons.water, BootstrapIcons.pie_chart,
color: Colors.green, color: Colors.amber,
), ),
) )
], ],

View File

@ -5,6 +5,7 @@ import 'package:agrilink_vocpro/features/home/provider/home_provider.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk1.dart'; import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk1.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk2.dart'; import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_npk2.dart';
import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_dht.dart'; import 'package:agrilink_vocpro/features/home/widgets/list_data_from_censor_dht.dart';
import 'package:agrilink_vocpro/features/setting/provider/setting_provider.dart';
import 'package:animated_segmented_tab_control/animated_segmented_tab_control.dart'; import 'package:animated_segmented_tab_control/animated_segmented_tab_control.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart';
@ -29,96 +30,99 @@ class _HomeScreenState extends State<HomeScreen> {
scrolledUnderElevation: 0, scrolledUnderElevation: 0,
flexibleSpace: Padding( flexibleSpace: Padding(
padding: EdgeInsets.symmetric(horizontal: 12.w), padding: EdgeInsets.symmetric(horizontal: 12.w),
child: Column( child:
children: [ Consumer<SettingProvider>(builder: (context, settingP, child) {
Row( return Column(
children: [ children: [
CircleAvatar( Row(
radius: 24.r, children: [
backgroundColor: AppColor.primary, CircleAvatar(
child: Icon( radius: 24.r,
Icons.person, backgroundColor: AppColor.primary,
color: Colors.white, child: Icon(
size: 24.r, Icons.person,
), color: Colors.white,
), size: 24.r,
SizedBox(width: 16.w),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
getGreeting(DateTime.now().toString()),
style: AppTheme.labelSmall,
), ),
Text( ),
'Fikril Mahesaputra', SizedBox(width: 16.w),
style: AppTheme.labelMedium, Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
getGreeting(DateTime.now().toString()),
style: AppTheme.labelSmall,
),
Text(
settingP.userFullName,
style: AppTheme.labelMedium,
),
],
),
const Spacer(),
IconButton(
onPressed: () {
context.read<HomeProvider>().getLatestData();
},
icon: const Icon(
Icons.refresh_rounded,
color: AppColor.primary,
),
)
],
),
SizedBox(
height: 8.h,
),
Container(
padding: const EdgeInsets.all(16),
height: MediaQuery.of(context).size.height * 0.17,
decoration: BoxDecoration(
color: AppColor.secondary,
image: const DecorationImage(
image:
AssetImage('assets/images/green_house_image.jpg'),
fit: BoxFit.cover),
borderRadius: BorderRadius.circular(16),
),
child: Column(
children: [
Container(
decoration: BoxDecoration(
color: AppColor.ternary.withAlpha(200),
borderRadius: BorderRadius.circular(32),
),
padding: const EdgeInsets.symmetric(
vertical: 8, horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("${settingP.userFullName}'s",
style: AppTheme.labelMedium
.copyWith(color: Colors.white)),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: AppColor.primary,
borderRadius: BorderRadius.circular(16),
),
child: Text(
dateFormater(DateTime.now().toString()),
style: AppTheme.labelMedium
.copyWith(color: Colors.white),
),
),
],
),
), ),
], ],
), ),
const Spacer(),
IconButton(
onPressed: () {
context.read<HomeProvider>().getLatestData();
},
icon: const Icon(
Icons.refresh_rounded,
color: AppColor.primary,
),
)
],
),
SizedBox(
height: 8.h,
),
Container(
padding: const EdgeInsets.all(16),
height: MediaQuery.of(context).size.height * 0.17,
decoration: BoxDecoration(
color: AppColor.secondary,
image: const DecorationImage(
image:
AssetImage('assets/images/green_house_image.jpg'),
fit: BoxFit.cover),
borderRadius: BorderRadius.circular(16),
), ),
child: Column( ],
children: [ );
Container( }),
decoration: BoxDecoration(
color: AppColor.ternary.withAlpha(200),
borderRadius: BorderRadius.circular(32),
),
padding: const EdgeInsets.symmetric(
vertical: 8, horizontal: 16),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("Fikril's Greenhouse",
style: AppTheme.labelMedium
.copyWith(color: Colors.white)),
Container(
padding: const EdgeInsets.symmetric(
horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color: AppColor.primary,
borderRadius: BorderRadius.circular(16),
),
child: Text(
dateFormater(DateTime.now().toString()),
style: AppTheme.labelMedium
.copyWith(color: Colors.white),
),
),
],
),
),
],
),
),
],
),
), ),
), ),
body: DefaultTabController( body: DefaultTabController(

View File

@ -1,4 +1,6 @@
import 'package:flutter/material.dart'; import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/domain/service/app_service.dart';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
class SettingProvider extends ChangeNotifier { class SettingProvider extends ChangeNotifier {
@ -15,4 +17,24 @@ class SettingProvider extends ChangeNotifier {
userEmail = prefs.getString('email') ?? 'unknown'; userEmail = prefs.getString('email') ?? 'unknown';
notifyListeners(); notifyListeners();
} }
ResultState logoutState = ResultState.initial;
Future<void> logout() async {
logoutState = ResultState.loading;
notifyListeners();
try {
final result = await AppService().logout();
if (result.data == null) {
logoutState = ResultState.hasData;
} else {
logoutState = ResultState.error;
}
} catch (e) {
if (kDebugMode) {
print('Error logout: $e');
}
logoutState = ResultState.error;
}
notifyListeners();
}
} }

View File

@ -1,6 +1,7 @@
import 'package:agrilink_vocpro/core/constant/app_color.dart'; import 'package:agrilink_vocpro/core/constant/app_color.dart';
import 'package:agrilink_vocpro/core/constant/app_theme.dart'; import 'package:agrilink_vocpro/core/constant/app_theme.dart';
import 'package:agrilink_vocpro/core/route/app_route.dart'; import 'package:agrilink_vocpro/core/route/app_route.dart';
import 'package:agrilink_vocpro/core/state/result_state.dart';
import 'package:agrilink_vocpro/features/setting/provider/setting_provider.dart'; import 'package:agrilink_vocpro/features/setting/provider/setting_provider.dart';
import 'package:bootstrap_icons/bootstrap_icons.dart'; import 'package:bootstrap_icons/bootstrap_icons.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -20,73 +21,72 @@ class SettingScreen extends StatelessWidget {
backgroundColor: Colors.white, backgroundColor: Colors.white,
scrolledUnderElevation: 0, scrolledUnderElevation: 0,
), ),
body: SafeArea( body: Consumer<SettingProvider>(builder: (context, provider, child) {
child: ListView( return SafeArea(
padding: EdgeInsets.all(16.r), child: ListView(
children: [ padding: EdgeInsets.all(16.r),
Row( children: [
children: [ Row(
const CircleAvatar( children: [
radius: 30, const CircleAvatar(
backgroundColor: AppColor.secondary, radius: 30,
child: Icon(BootstrapIcons.person_fill, color: Colors.white), backgroundColor: AppColor.secondary,
), child: Icon(BootstrapIcons.person_fill, color: Colors.white),
SizedBox(width: 8.w), ),
Consumer<SettingProvider>(builder: (context, provider, child) { SizedBox(width: 8.w),
return Column( Column(
crossAxisAlignment: CrossAxisAlignment.start, crossAxisAlignment: CrossAxisAlignment.start,
children: [ children: [
Text(provider.userFullName, style: AppTheme.labelMedium), Text(provider.userFullName, style: AppTheme.labelMedium),
Text(provider.userEmail, style: AppTheme.labelSmall) Text(provider.userEmail, style: AppTheme.labelSmall)
], ],
); )
}) ],
],
),
SizedBox(height: 16.h),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(8.r),
), ),
child: Column( SizedBox(height: 16.h),
children: [ Container(
ListTile( decoration: BoxDecoration(
tileColor: Colors.white, borderRadius: BorderRadius.circular(8.r),
title: Text('Account', ),
style: child: Column(
AppTheme.labelSmall.copyWith(color: Colors.black87)), children: [
leading: const Icon(BootstrapIcons.person), ListTile(
trailing: Icon( tileColor: Colors.white,
Icons.arrow_forward_ios, title: Text('Account',
size: 16.r, style: AppTheme.labelSmall
.copyWith(color: Colors.black87)),
leading: const Icon(BootstrapIcons.person),
trailing: Icon(
Icons.arrow_forward_ios,
size: 16.r,
),
onTap: () {},
), ),
onTap: () {}, ListTile(
), tileColor: Colors.white,
ListTile( title: Text('Kebijakan & privasi',
tileColor: Colors.white, style: AppTheme.labelSmall
title: Text('Kebijakan & privasi', .copyWith(color: Colors.black87)),
style: leading: const Icon(BootstrapIcons.shield_check),
AppTheme.labelSmall.copyWith(color: Colors.black87)), trailing: Icon(
leading: const Icon(BootstrapIcons.shield_check), Icons.arrow_forward_ios,
trailing: Icon( size: 16.r,
Icons.arrow_forward_ios, ),
size: 16.r, onTap: () {},
), ),
onTap: () {}, ListTile(
), tileColor: Colors.white,
ListTile( title: Text('Syarat & ketentuan',
tileColor: Colors.white, style: AppTheme.labelSmall
title: Text('Syarat & ketentuan', .copyWith(color: Colors.black87)),
style: leading: const Icon(BootstrapIcons.file_text),
AppTheme.labelSmall.copyWith(color: Colors.black87)), trailing: Icon(
leading: const Icon(BootstrapIcons.file_text), Icons.arrow_forward_ios,
trailing: Icon( size: 16.r,
Icons.arrow_forward_ios, ),
size: 16.r, onTap: () {},
), ),
onTap: () {}, ListTile(
),
ListTile(
tileColor: Colors.white, tileColor: Colors.white,
title: Text('Logout', title: Text('Logout',
style: AppTheme.labelSmall.copyWith(color: Colors.red)), style: AppTheme.labelSmall.copyWith(color: Colors.red)),
@ -113,19 +113,27 @@ class SettingScreen extends StatelessWidget {
), ),
TextButton( TextButton(
child: Text('Ya'), child: Text('Ya'),
onPressed: () { onPressed: () async {
context.go(AppRoute.root); await provider.logout();
if (context.mounted) {
if (provider.logoutState ==
ResultState.hasData) {
context.go(AppRoute.root);
}
}
}, },
), ),
], ],
), ),
); );
}), },
], ),
],
),
), ),
), ],
], ));
)), }),
); );
} }
} }