MIF_E31221388/salonbooking/lib/page/logout_page.dart

91 lines
2.2 KiB
Dart

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
class LogoutPage extends StatefulWidget {
final String token;
const LogoutPage({Key? key, required this.token}) : super(key: key);
@override
State<LogoutPage> createState() => _LogoutPageState();
}
class _LogoutPageState extends State<LogoutPage> {
bool isLoading = false;
String message = '';
Future<void> logout() async {
setState(() {
isLoading = true;
message = '';
});
final url = Uri.parse('http://angeliasalon.my.id/api/logout');
try {
final response = await http.post(
url,
headers: {
'Accept': 'application/json',
'Authorization': 'Bearer ${widget.token}',
},
);
if (response.statusCode == 200) {
setState(() {
message = 'Logout berhasil';
});
// Kembali ke halaman login atau halaman lain
Navigator.pushReplacementNamed(context, '/login');
} else {
final data = jsonDecode(response.body);
setState(() {
message = data['message'] ?? 'Logout gagal';
});
}
} catch (e) {
setState(() {
message = 'Terjadi kesalahan: $e';
});
} finally {
setState(() {
isLoading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Logout'),
),
body: Center(
child: isLoading
? const CircularProgressIndicator()
: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Apakah anda yakin ingin logout?',
style: TextStyle(fontSize: 18),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: logout,
child: const Text('Logout'),
),
const SizedBox(height: 20),
Text(
message,
style: const TextStyle(color: Colors.red),
)
],
),
),
);
}
}