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.
322 lines
8.8 KiB
322 lines
8.8 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(); |
|
if (mounted) { |
|
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 Dialog( |
|
shape: RoundedRectangleBorder( |
|
borderRadius: BorderRadius.circular(16.0), |
|
), |
|
elevation: 0, |
|
backgroundColor: Colors.transparent, |
|
child: Container( |
|
padding: EdgeInsets.all(20), |
|
decoration: BoxDecoration( |
|
shape: BoxShape.rectangle, |
|
color: Colors.white, |
|
borderRadius: BorderRadius.circular(16.0), |
|
), |
|
child: Column( |
|
mainAxisSize: MainAxisSize.min, |
|
children: <Widget>[ |
|
Row( |
|
mainAxisAlignment: MainAxisAlignment.spaceBetween, |
|
children: [ |
|
Text( |
|
'Konfirmasi Keluar Akun', |
|
style: TextStyle( |
|
fontSize: 16, fontWeight: FontWeight.w600), |
|
), |
|
IconButton( |
|
icon: Icon(Icons.close), |
|
onPressed: () { |
|
Navigator.of(context).pop(false); |
|
}, |
|
), |
|
], |
|
), |
|
Divider( |
|
height: 10, |
|
thickness: 5, |
|
), |
|
SizedBox( |
|
height: 15, |
|
), |
|
Text( |
|
'Apakah Anda yakin ingin keluar akun?', |
|
style: TextStyle(fontSize: 14), |
|
textAlign: TextAlign.center, |
|
), |
|
SizedBox( |
|
height: 22, |
|
), |
|
Row( |
|
mainAxisAlignment: MainAxisAlignment.center, |
|
children: <Widget>[ |
|
SizedBox(width: 16), |
|
TextButton.icon( |
|
onPressed: () { |
|
Navigator.of(context).pop(true); |
|
}, |
|
icon: Icon( |
|
Icons.exit_to_app, |
|
color: Colors.red, |
|
), |
|
label: Text( |
|
'Keluar', |
|
style: TextStyle(color: Colors.red), |
|
), |
|
), |
|
], |
|
), |
|
], |
|
), |
|
), |
|
); |
|
}, |
|
); |
|
|
|
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("Pengaturan"), |
|
automaticallyImplyLeading: false, |
|
), |
|
body: Center( |
|
child: Container( |
|
constraints: const BoxConstraints(maxWidth: 400), |
|
child: ListView( |
|
children: [ |
|
SizedBox( |
|
height: 20, |
|
), |
|
_SingleSection( |
|
title: "General", |
|
children: [], |
|
), |
|
const Divider(), |
|
_SingleSection( |
|
children: [ |
|
_Logout( |
|
title: "Keluar", |
|
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, |
|
), |
|
], |
|
); |
|
} |
|
}
|
|
|