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 createState() => SettingPageState(); } class SettingPageState extends State { 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(context, listen: false); if (token != null) { bool? confirm = await showDialog( context: context, builder: (BuildContext context) { return AlertDialog( title: Text('Konfirmasi Logout'), content: Text('Apakah Anda yakin ingin logout?'), actions: [ 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(context, listen: false); // if (token != null) { // bool? confirm = await showDialog( // context: context, // builder: (BuildContext context) { // return AlertDialog( // title: Text('Konfirmasi Logout'), // content: Text('Apakah Anda yakin ingin logout?'), // actions: [ // 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(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 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, ), ], ); } }