Siopas Inventory PETI for ISTW Mobile
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

281 lines
7.3 KiB

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import '../../models/user_model.dart';
import '../../providers/auth_provider.dart';
class SettingPage extends StatefulWidget {
const SettingPage({Key? key});
@override
State<SettingPage> createState() => SettingPageState();
}
class SettingPageState extends State<SettingPage> {
bool _isDark = false;
String? token;
// bool isLoading = true; // Tambahkan loading
@override
void initState() {
super.initState();
_getUserToken();
}
void _getUserToken() async {
try {
SharedPreferences prefs = await SharedPreferences.getInstance();
setState(() {
token = prefs.getString('token');
});
} catch (e) {
print("Error: $e");
}
}
void handleGetLogout(BuildContext context) async {
AuthProvider authProvider =
Provider.of<AuthProvider>(context, listen: false);
if (token != null) {
bool? confirm = await showDialog<bool>(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text('Konfirmasi Logout'),
content: Text('Apakah Anda yakin ingin logout?'),
actions: <Widget>[
TextButton(
child: Text('Tidak'),
onPressed: () {
Navigator.of(context).pop(false);
},
),
TextButton(
child: Text('Ya'),
onPressed: () {
Navigator.of(context).pop(true);
},
),
],
);
},
);
if (confirm != null && confirm) {
bool logoutSuccess = await authProvider.logout(token!);
if (logoutSuccess) {
print('Berhasil Logout');
Navigator.pushNamedAndRemoveUntil(
context, '/sign-in', (route) => false);
} else {
print('Gagal Logout');
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
backgroundColor: Colors.red,
content: Text(
'Gagal Logout',
textAlign: TextAlign.center,
),
),
);
}
}
} else {
print('Token pengguna tidak tersedia.');
}
}
// void _getUserToken() async {
// SharedPreferences prefs = await SharedPreferences.getInstance();
// if (mounted) {
// setState(() {
// token = prefs.getString('token');
// });
// }
// }
// void handleGetLogout(BuildContext context) async {
// AuthProvider authProvider =
// Provider.of<AuthProvider>(context, listen: false);
// if (token != null) {
// bool? confirm = await showDialog<bool>(
// context: context,
// builder: (BuildContext context) {
// return AlertDialog(
// title: Text('Konfirmasi Logout'),
// content: Text('Apakah Anda yakin ingin logout?'),
// actions: <Widget>[
// TextButton(
// child: Text('Tidak'),
// onPressed: () {
// Navigator.of(context).pop(false);
// },
// ),
// TextButton(
// child: Text('Ya'),
// onPressed: () {
// Navigator.of(context).pop(true);
// },
// ),
// ],
// );
// },
// );
// if (confirm != null && confirm) {
// bool logoutSuccess = await authProvider.logout(token!);
// print('logoutSuccess: $logoutSuccess');
// if (logoutSuccess) {
// print('Berhasil Logout');
// Navigator.pushNamedAndRemoveUntil(
// context, '/sign-in', (route) => false);
// } else {
// print('Gagal Logout');
// ScaffoldMessenger.of(context).showSnackBar(
// SnackBar(
// backgroundColor: Colors.red,
// content: Text(
// 'Gagal Logout',
// textAlign: TextAlign.center,
// ),
// ),
// );
// }
// }
// } else {
// print('Token pengguna tidak tersedia.');
// }
// }
@override
Widget build(BuildContext context) {
AuthProvider authProvider = Provider.of<AuthProvider>(context);
UserModel user = authProvider.user;
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.indigo[700],
title: const Text("Settings"),
automaticallyImplyLeading: false,
),
body: Center(
child: Container(
constraints: const BoxConstraints(maxWidth: 400),
child: ListView(
children: [
SizedBox(
height: 20,
),
_SingleSection(
title: "General",
children: [
_CustomListTile(
title: "Dark Mode",
icon: Icons.dark_mode_outlined,
trailing: Switch(
value: _isDark,
onChanged: (value) {
setState(() {
_isDark = value;
});
},
),
),
],
),
const Divider(),
_SingleSection(
children: [
_Logout(
title: "Sign out",
icon: Icons.exit_to_app_rounded,
handleLogout: handleGetLogout,
),
],
),
],
),
),
),
);
}
}
class _CustomListTile extends StatelessWidget {
final String title;
final IconData icon;
final Widget? trailing;
const _CustomListTile(
{Key? key, required this.title, required this.icon, this.trailing})
: super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),
leading: Icon(icon),
trailing: trailing,
onTap: () {},
);
}
}
class _Logout extends StatelessWidget {
final String title;
final IconData icon;
final Function handleLogout;
const _Logout(
{Key? key,
required this.title,
required this.icon,
required this.handleLogout})
: super(key: key);
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(title),
leading: Icon(icon),
onTap: () => handleLogout(context),
);
}
}
class _SingleSection extends StatelessWidget {
final String? title;
final List<Widget> children;
const _SingleSection({
Key? key,
this.title,
required this.children,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (title != null)
Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
title!,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
),
),
Column(
children: children,
),
],
);
}
}