You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
255 lines
10 KiB
Dart
255 lines
10 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_advanced_switch/flutter_advanced_switch.dart';
|
|
import 'package:local_auth_darwin/local_auth_darwin.dart';
|
|
|
|
import 'package:provider/provider.dart';
|
|
import 'package:test_sa/controllers/providers/settings/setting_provider.dart';
|
|
import 'package:test_sa/controllers/providers/api/user_provider.dart';
|
|
import 'package:test_sa/extensions/context_extension.dart';
|
|
import 'package:test_sa/extensions/int_extensions.dart';
|
|
import 'package:test_sa/extensions/string_extensions.dart';
|
|
import 'package:test_sa/extensions/text_extensions.dart';
|
|
import 'package:test_sa/extensions/widget_extensions.dart';
|
|
import 'package:test_sa/helper/utils.dart';
|
|
import 'package:test_sa/new_views/app_style/app_color.dart';
|
|
import 'package:test_sa/new_views/common_widgets/app_filled_button.dart';
|
|
import 'package:test_sa/new_views/pages/login_page.dart';
|
|
import 'package:test_sa/views/widgets/dialogs/dialog.dart';
|
|
import 'package:test_sa/views/widgets/item_views/info_header_widget.dart';
|
|
|
|
import '../common_widgets/default_app_bar.dart';
|
|
|
|
class SettingsPage extends StatefulWidget {
|
|
static const id = "/settings_page";
|
|
|
|
const SettingsPage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<SettingsPage> createState() => _SettingsPageState();
|
|
}
|
|
|
|
class _SettingsPageState extends State<SettingsPage> {
|
|
ValueNotifier<bool>? langController, themeController;
|
|
SettingProvider? _settingProvider;
|
|
|
|
bool localAuth = false;
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
langController?.dispose();
|
|
themeController?.dispose();
|
|
// authController.dispose();
|
|
}
|
|
|
|
void checkForLocalAuth(bool buttonState) async {
|
|
bool authStatus = await _settingProvider!.auth.authenticate(
|
|
localizedReason: Platform.isAndroid ? "Scan your fingerprint to authenticate" : "Scan with face id to authenticate",
|
|
options: const AuthenticationOptions(),
|
|
authMessages: [const IOSAuthMessages(cancelButton: 'cancel', goToSettingsButton: 'settings', goToSettingsDescription: 'Please enable Touch ID.', lockOut: 'Please reenable your Touch ID')]);
|
|
if (authStatus) {
|
|
localAuth = !localAuth;
|
|
await _settingProvider!.setAuth(localAuth);
|
|
await _settingProvider!.checkIsEnabledFaceId(status: localAuth);
|
|
// authController.value = _settingProvider.localAuth == buttonState.toString();
|
|
setState(() {});
|
|
} else {
|
|
// authController.value = !buttonState;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (_settingProvider == null) {
|
|
_settingProvider = Provider.of<SettingProvider>(context, listen: false);
|
|
localAuth = _settingProvider!.localAuth;
|
|
}
|
|
|
|
langController ??= ValueNotifier<bool>(_settingProvider!.language == "ar")
|
|
..addListener(() async {
|
|
/// TODO : uncomment the below lines to support the another language
|
|
await _settingProvider!.setLanguage(_settingProvider!.language == "ar" ? "en" : "ar");
|
|
langController!.value = _settingProvider!.language == "ar";
|
|
});
|
|
themeController ??= ValueNotifier<bool>(_settingProvider!.theme == "light")
|
|
..addListener(() async {
|
|
await _settingProvider!.setDarkTheme(_settingProvider!.theme == "light");
|
|
themeController!.value = _settingProvider!.theme == "light";
|
|
SystemChrome.setSystemUIOverlayStyle(SystemUiOverlayStyle(
|
|
statusBarColor: Colors.transparent,
|
|
systemNavigationBarColor: _settingProvider!.theme == "light" ? Colors.white : AppColor.neutral60,
|
|
systemNavigationBarIconBrightness: _settingProvider!.theme == "light" ? Brightness.dark : Brightness.light,
|
|
));
|
|
});
|
|
|
|
// authController ??= ValueNotifier<bool>(_settingProvider.localAuth == "false")
|
|
// ..addListener(() async {
|
|
// print("authController.value:${authController.value}");
|
|
//
|
|
// // if (authController.value) {
|
|
// // checkForLocalAuth();
|
|
// // } else {
|
|
// // await _settingProvider.setAuth(_settingProvider.localAuth == "false");
|
|
// // authController.value = _settingProvider.localAuth == "false";
|
|
// // }
|
|
// });
|
|
|
|
return Scaffold(
|
|
appBar: DefaultAppBar(title: context.translation.settings),
|
|
body: Column(
|
|
children: [
|
|
Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
spacing: 12,
|
|
children: [
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
InfoHeader16Widget(context.translation.arabic),
|
|
AdvancedSwitch(
|
|
controller: langController,
|
|
initialValue: langController!.value,
|
|
activeColor: AppColor.green50.withValues(alpha: .25),
|
|
inactiveColor: AppColor.neutral10,
|
|
thumb: CircleAvatar(backgroundColor: langController!.value ? AppColor.green50 : AppColor.switchBgColor),
|
|
borderRadius: const BorderRadius.all(Radius.circular(30)),
|
|
width: 42.toScreenWidth,
|
|
height: 24.toScreenHeight,
|
|
|
|
/// TODO : remove the below attribute to enable the switch
|
|
//enabled: false,
|
|
disabledOpacity: 1,
|
|
),
|
|
],
|
|
),
|
|
// 16.height,
|
|
const Divider().defaultStyle(context),
|
|
// 16.height,
|
|
Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
InfoHeader16Widget(context.translation.lightTheme),
|
|
AdvancedSwitch(
|
|
controller: themeController,
|
|
initialValue: themeController!.value,
|
|
activeColor: AppColor.green50.withValues(alpha: .25),
|
|
inactiveColor: AppColor.neutral10,
|
|
thumb: CircleAvatar(backgroundColor: themeController!.value ? AppColor.green50 : AppColor.switchBgColor),
|
|
borderRadius: const BorderRadius.all(Radius.circular(30)),
|
|
width: 42.toScreenWidth,
|
|
height: 24.toScreenHeight,
|
|
disabledOpacity: 1,
|
|
),
|
|
],
|
|
),
|
|
// 16.height,
|
|
const Divider().defaultStyle(context),
|
|
// 16.height,
|
|
FutureBuilder(
|
|
future: _settingProvider!.auth.isDeviceSupported(),
|
|
builder: (cxt, snapshot) {
|
|
bool isDeviceSupported = false;
|
|
if (snapshot.hasData) {
|
|
isDeviceSupported = snapshot.data as bool;
|
|
}
|
|
|
|
if (!isDeviceSupported) {
|
|
return Text("Your device did not support Fingerprint/Face ID.", style: AppTextStyles.tinyFont.copyWith(color: AppColor.red30));
|
|
}
|
|
|
|
return Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
InfoHeader16Widget("Fingerprint/Face ID"),
|
|
customSwitch(localAuth).onPress(() {
|
|
checkForLocalAuth(localAuth);
|
|
}),
|
|
],
|
|
);
|
|
}),
|
|
],
|
|
).toShadowContainer(context, borderRadius: 16, padding: 16).paddingAll(16),
|
|
// 12.height,
|
|
//TODO need to test this ...
|
|
AppFilledButton(
|
|
label: "Delete Account",
|
|
buttonColor: AppColor.red30,
|
|
onPressed: () async {
|
|
bool result = await showDialog(
|
|
context: context,
|
|
builder: (_) => AAlertDialog(
|
|
title: "Confirm".addTranslation,
|
|
content: "By deleting your account, all your data will be permanently removed. Are you sure you want to proceed?".addTranslation,
|
|
),
|
|
);
|
|
|
|
if (result) {
|
|
final userProvider = Provider.of<UserProvider>(context, listen: false);
|
|
|
|
// Check if user data is available
|
|
if (userProvider.user == null) {
|
|
Utils.showToast("User information not available");
|
|
return;
|
|
}
|
|
|
|
final employeeId = userProvider.user!.employeeId ?? "";
|
|
final userId = userProvider.user!.userID ?? "";
|
|
|
|
if (employeeId.isEmpty || userId.isEmpty) {
|
|
Utils.showToast("Unable to delete account: missing user information");
|
|
return;
|
|
}
|
|
|
|
// Call the delete user API
|
|
bool isSuccess = await userProvider.deleteUser(
|
|
context,
|
|
employeeId: employeeId,
|
|
userId: userId,
|
|
);
|
|
|
|
if (isSuccess) {
|
|
// Reset settings and user data
|
|
Provider.of<SettingProvider>(context, listen: false).resetSettings();
|
|
userProvider.reset();
|
|
|
|
// Navigate to login page
|
|
Navigator.of(context).pushNamedAndRemoveUntil(
|
|
LoginPage.routeName,
|
|
(routes) => false,
|
|
);
|
|
}
|
|
}
|
|
},
|
|
).paddingAll(16),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget customSwitch(bool enable) {
|
|
return Container(
|
|
width: 42.toScreenWidth,
|
|
height: 24.toScreenHeight,
|
|
alignment: enable ? Alignment.centerRight : Alignment.centerLeft,
|
|
decoration: BoxDecoration(
|
|
color: enable ? AppColor.green50.withValues(alpha: 0.25) : AppColor.neutral10,
|
|
borderRadius: const BorderRadius.all(Radius.circular(30)),
|
|
),
|
|
padding: const EdgeInsets.all(2),
|
|
child: Container(
|
|
height: 20.toScreenHeight,
|
|
width: 20.toScreenWidth,
|
|
decoration: BoxDecoration(shape: BoxShape.circle, color: enable ? AppColor.green50 : AppColor.switchBgColor),
|
|
));
|
|
}
|
|
}
|