cashumit/lib/widgets/store_info_widget.dart

146 lines
4.4 KiB
Dart

import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:io';
/// Widget untuk menampilkan informasi toko dan admin.
/// Dapat di-tap untuk membuka dialog konfigurasi toko.
/// Baris tanggal dapat di-tap untuk mengatur tanggal transaksi.
class StoreInfoWidget extends StatefulWidget {
final VoidCallback onTap; // Buka dialog konfigurasi toko
const StoreInfoWidget({
super.key,
required this.onTap,
});
@override
State<StoreInfoWidget> createState() => _StoreInfoWidgetState();
}
class _StoreInfoWidgetState extends State<StoreInfoWidget> {
String storeName = 'TOKO SEMBAKO MURAH';
String storeAddress = 'Jl. Merdeka No. 123';
String adminName = 'Budi Santoso';
String adminPhone = '08123456789';
String? logoPath;
// Cache style agar tidak dibuat ulang tiap rebuild (perbaikan performa).
static final TextStyle _courierPrime = () {
try {
return GoogleFonts.courierPrime(
textStyle: const TextStyle(fontSize: 14, height: 1.2),
);
} catch (_) {
return const TextStyle(
fontFamily: 'CourierPrime, Courier, monospace',
fontSize: 14,
height: 1.2,
);
}
}();
@override
void initState() {
super.initState();
_loadStoreInfo();
}
@override
void didUpdateWidget(covariant StoreInfoWidget oldWidget) {
super.didUpdateWidget(oldWidget);
_loadStoreInfo();
}
/// Memuat informasi toko dari shared preferences. Hanya memanggil
/// setState jika ada perubahan, untuk menghindari rebuild loop.
Future<void> _loadStoreInfo() async {
final prefs = await SharedPreferences.getInstance();
if (!mounted) return;
final newStoreName = prefs.getString('store_name') ?? 'TOKO SEMBAKO MURAH';
final newStoreAddress =
prefs.getString('store_address') ?? 'Jl. Merdeka No. 123';
final newAdminName = prefs.getString('admin_name') ?? 'Budi Santoso';
final newAdminPhone = prefs.getString('admin_phone') ?? '08123456789';
final newLogoPath = prefs.getString('store_logo_path');
if (newStoreName == storeName &&
newStoreAddress == storeAddress &&
newAdminName == adminName &&
newAdminPhone == adminPhone &&
newLogoPath == logoPath) {
return;
}
setState(() {
storeName = newStoreName;
storeAddress = newStoreAddress;
adminName = newAdminName;
adminPhone = newAdminPhone;
logoPath = newLogoPath;
});
}
/// Refresh info toko (dipanggil saat kembali dari dialog konfigurasi).
void refresh() {
_loadStoreInfo();
}
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: widget.onTap,
child: Container(
width: double.infinity,
padding: const EdgeInsets.all(8.0),
color: Colors.white,
child: Column(
children: [
// Logo jika tersedia
if (logoPath != null)
Container(
height: 80,
width: 80,
margin: const EdgeInsets.only(bottom: 8),
child: Image.file(
File(logoPath!),
fit: BoxFit.contain,
cacheWidth:
160, // Resolusi render lebih kecil (perbaikan performa)
errorBuilder: (context, error, stackTrace) {
return Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(4),
),
child: const Icon(Icons.image_not_supported, size: 40),
);
},
),
),
Text(
storeName,
style: _courierPrime.copyWith(
fontSize: 16,
fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
const SizedBox(height: 4),
Text(storeAddress,
style: _courierPrime, textAlign: TextAlign.center),
const SizedBox(height: 4),
Text('Admin: $adminName',
style: _courierPrime, textAlign: TextAlign.center),
Text('Telp: $adminPhone',
style: _courierPrime, textAlign: TextAlign.center),
],
),
),
);
}
}