129 lines
3.5 KiB
Dart
129 lines
3.5 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:salonbooking/pelanggan/home_pelanggan.dart';
|
|
import 'package:salonbooking/pelanggan/keranjang_page.dart';
|
|
import 'package:salonbooking/pelanggan/riwayat_bookings_page.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'dart:convert';
|
|
|
|
class MainNavigation extends StatefulWidget {
|
|
final String userName;
|
|
final String token;
|
|
|
|
const MainNavigation({
|
|
Key? key,
|
|
required this.userName,
|
|
required this.token,
|
|
required Map user,
|
|
}) : super(key: key);
|
|
|
|
@override
|
|
State<MainNavigation> createState() => _MainNavigationState();
|
|
}
|
|
|
|
class _MainNavigationState extends State<MainNavigation> {
|
|
int _selectedIndex = 0;
|
|
int cartItemCount = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
fetchCartCount();
|
|
}
|
|
|
|
Future<void> fetchCartCount() async {
|
|
try {
|
|
final response = await http.get(
|
|
Uri.parse('http://angeliasalon.my.id/api/cart'),
|
|
headers: {
|
|
'Authorization': 'Bearer ${widget.token}',
|
|
'Accept': 'application/json',
|
|
},
|
|
);
|
|
|
|
if (response.statusCode == 200) {
|
|
final data = json.decode(response.body);
|
|
final List cart = data is List ? data : data['cart'] ?? [];
|
|
setState(() {
|
|
cartItemCount = cart.length;
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print('Error: $e');
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final pages = [
|
|
PelangganHomePage(
|
|
widget.userName,
|
|
token: widget.token,
|
|
onCartUpdated: fetchCartCount,
|
|
),
|
|
RiwayatBookingPage(token: widget.token), // Kirim token di sini
|
|
KeranjangPage(
|
|
token: widget.token,
|
|
onCartUpdated: fetchCartCount,
|
|
),
|
|
];
|
|
|
|
return Scaffold(
|
|
body: pages[_selectedIndex],
|
|
bottomNavigationBar: BottomNavigationBar(
|
|
currentIndex: _selectedIndex,
|
|
onTap: (index) {
|
|
setState(() {
|
|
_selectedIndex = index;
|
|
});
|
|
},
|
|
type: BottomNavigationBarType.fixed,
|
|
selectedItemColor: const Color(0xFFF06292),
|
|
items: [
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.home),
|
|
label: 'Beranda',
|
|
),
|
|
const BottomNavigationBarItem(
|
|
icon: Icon(Icons.history),
|
|
label: 'Riwayat',
|
|
),
|
|
BottomNavigationBarItem(
|
|
icon: Stack(
|
|
clipBehavior: Clip.none,
|
|
children: [
|
|
const Icon(Icons.shopping_cart),
|
|
if (cartItemCount > 0)
|
|
Positioned(
|
|
right: -6,
|
|
top: -4,
|
|
child: Container(
|
|
padding: const EdgeInsets.all(2),
|
|
decoration: const BoxDecoration(
|
|
color: Colors.red,
|
|
shape: BoxShape.circle,
|
|
),
|
|
constraints: const BoxConstraints(
|
|
minWidth: 16,
|
|
minHeight: 16,
|
|
),
|
|
child: Text(
|
|
'$cartItemCount',
|
|
style: const TextStyle(
|
|
color: Colors.white,
|
|
fontSize: 10,
|
|
fontWeight: FontWeight.bold,
|
|
),
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
label: 'Keranjang',
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|