Initial commit - app-padrao-1.0
This commit is contained in:
69
lib/features/haircuts/data/models/haircut_model.dart
Normal file
69
lib/features/haircuts/data/models/haircut_model.dart
Normal file
@@ -0,0 +1,69 @@
|
||||
import 'package:hive/hive.dart';
|
||||
|
||||
part 'haircut_model.g.dart';
|
||||
|
||||
@HiveType(typeId: 1)
|
||||
class HaircutModel extends HiveObject {
|
||||
@HiveField(0)
|
||||
final String id;
|
||||
|
||||
@HiveField(1)
|
||||
final String userId;
|
||||
|
||||
@HiveField(2)
|
||||
final String clientName;
|
||||
|
||||
@HiveField(3)
|
||||
final String serviceType;
|
||||
|
||||
@HiveField(4)
|
||||
final double price;
|
||||
|
||||
@HiveField(5)
|
||||
final DateTime dateTime;
|
||||
|
||||
@HiveField(6)
|
||||
final String? notes;
|
||||
|
||||
@HiveField(7)
|
||||
final String? transactionId; // Link com a transação financeira
|
||||
|
||||
@HiveField(8, defaultValue: 'Dinheiro')
|
||||
final String paymentMethod;
|
||||
|
||||
HaircutModel({
|
||||
required this.id,
|
||||
required this.userId,
|
||||
required this.clientName,
|
||||
required this.serviceType,
|
||||
required this.price,
|
||||
required this.dateTime,
|
||||
this.notes,
|
||||
this.transactionId,
|
||||
this.paymentMethod = 'Dinheiro',
|
||||
});
|
||||
|
||||
HaircutModel copyWith({
|
||||
String? id,
|
||||
String? userId,
|
||||
String? clientName,
|
||||
String? serviceType,
|
||||
double? price,
|
||||
DateTime? dateTime,
|
||||
String? notes,
|
||||
String? transactionId,
|
||||
String? paymentMethod,
|
||||
}) {
|
||||
return HaircutModel(
|
||||
id: id ?? this.id,
|
||||
userId: userId ?? this.userId,
|
||||
clientName: clientName ?? this.clientName,
|
||||
serviceType: serviceType ?? this.serviceType,
|
||||
price: price ?? this.price,
|
||||
dateTime: dateTime ?? this.dateTime,
|
||||
notes: notes ?? this.notes,
|
||||
transactionId: transactionId ?? this.transactionId,
|
||||
paymentMethod: paymentMethod ?? this.paymentMethod,
|
||||
);
|
||||
}
|
||||
}
|
||||
65
lib/features/haircuts/data/models/haircut_model.g.dart
Normal file
65
lib/features/haircuts/data/models/haircut_model.g.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'haircut_model.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// TypeAdapterGenerator
|
||||
// **************************************************************************
|
||||
|
||||
class HaircutModelAdapter extends TypeAdapter<HaircutModel> {
|
||||
@override
|
||||
final int typeId = 1;
|
||||
|
||||
@override
|
||||
HaircutModel read(BinaryReader reader) {
|
||||
final numOfFields = reader.readByte();
|
||||
final fields = <int, dynamic>{
|
||||
for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(),
|
||||
};
|
||||
return HaircutModel(
|
||||
id: fields[0] as String,
|
||||
userId: fields[1] as String,
|
||||
clientName: fields[2] as String,
|
||||
serviceType: fields[3] as String,
|
||||
price: fields[4] as double,
|
||||
dateTime: fields[5] as DateTime,
|
||||
notes: fields[6] as String?,
|
||||
transactionId: fields[7] as String?,
|
||||
paymentMethod: fields[8] == null ? 'Dinheiro' : fields[8] as String,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void write(BinaryWriter writer, HaircutModel obj) {
|
||||
writer
|
||||
..writeByte(9)
|
||||
..writeByte(0)
|
||||
..write(obj.id)
|
||||
..writeByte(1)
|
||||
..write(obj.userId)
|
||||
..writeByte(2)
|
||||
..write(obj.clientName)
|
||||
..writeByte(3)
|
||||
..write(obj.serviceType)
|
||||
..writeByte(4)
|
||||
..write(obj.price)
|
||||
..writeByte(5)
|
||||
..write(obj.dateTime)
|
||||
..writeByte(6)
|
||||
..write(obj.notes)
|
||||
..writeByte(7)
|
||||
..write(obj.transactionId)
|
||||
..writeByte(8)
|
||||
..write(obj.paymentMethod);
|
||||
}
|
||||
|
||||
@override
|
||||
int get hashCode => typeId.hashCode;
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is HaircutModelAdapter &&
|
||||
runtimeType == other.runtimeType &&
|
||||
typeId == other.typeId;
|
||||
}
|
||||
126
lib/features/haircuts/data/repositories/haircut_repository.dart
Normal file
126
lib/features/haircuts/data/repositories/haircut_repository.dart
Normal file
@@ -0,0 +1,126 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
import 'package:barber_app/core/database/database_service.dart';
|
||||
import 'package:barber_app/features/haircuts/data/models/haircut_model.dart';
|
||||
import 'package:barber_app/features/finances/data/repositories/transaction_repository.dart';
|
||||
|
||||
class HaircutRepository {
|
||||
final _uuid = const Uuid();
|
||||
final _transactionRepo = TransactionRepository();
|
||||
|
||||
String? get _currentUserId => DatabaseService.getCurrentUserId();
|
||||
|
||||
// Criar corte (cria transação automaticamente)
|
||||
Future<HaircutModel?> createHaircut({
|
||||
required String clientName,
|
||||
required String serviceType,
|
||||
required double price,
|
||||
required DateTime dateTime,
|
||||
String? notes,
|
||||
bool isPaid = true,
|
||||
String paymentMethod = 'Dinheiro',
|
||||
}) async {
|
||||
if (_currentUserId == null) return null;
|
||||
|
||||
final haircutId = _uuid.v4();
|
||||
|
||||
// Criar transação financeira (Adicionando método de pagamento na descrição p/ facilitar controle)
|
||||
final transaction = await _transactionRepo.createRevenueFromHaircut(
|
||||
haircutId: haircutId,
|
||||
amount: price,
|
||||
clientName: '$clientName ($paymentMethod)',
|
||||
serviceType: serviceType,
|
||||
isPaid: isPaid,
|
||||
);
|
||||
|
||||
final haircut = HaircutModel(
|
||||
id: haircutId,
|
||||
userId: _currentUserId!,
|
||||
clientName: clientName.trim(),
|
||||
serviceType: serviceType,
|
||||
price: price,
|
||||
dateTime: dateTime,
|
||||
notes: notes?.trim(),
|
||||
transactionId: transaction?.id,
|
||||
paymentMethod: paymentMethod,
|
||||
);
|
||||
|
||||
await DatabaseService.haircutsBoxInstance.put(haircutId, haircut);
|
||||
return haircut;
|
||||
}
|
||||
|
||||
// Listar todos os cortes do usuário
|
||||
List<HaircutModel> getAllHaircuts() {
|
||||
if (_currentUserId == null) return [];
|
||||
return DatabaseService.haircutsBoxInstance.values
|
||||
.where((h) => h.userId == _currentUserId)
|
||||
.toList()
|
||||
..sort((a, b) => b.dateTime.compareTo(a.dateTime));
|
||||
}
|
||||
|
||||
// Cortes de hoje
|
||||
List<HaircutModel> getTodayHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final todayStart = DateTime(now.year, now.month, now.day);
|
||||
final todayEnd = todayStart.add(const Duration(days: 1));
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(todayStart) && h.dateTime.isBefore(todayEnd))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Cortes da semana
|
||||
List<HaircutModel> getWeekHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final weekStart = now.subtract(Duration(days: now.weekday - 1));
|
||||
final weekStartDay = DateTime(weekStart.year, weekStart.month, weekStart.day);
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(weekStartDay))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Cortes do mês
|
||||
List<HaircutModel> getMonthHaircuts() {
|
||||
final now = DateTime.now();
|
||||
final monthStart = DateTime(now.year, now.month, 1);
|
||||
|
||||
return getAllHaircuts()
|
||||
.where((h) => h.dateTime.isAfter(monthStart))
|
||||
.toList();
|
||||
}
|
||||
|
||||
// Faturamento do dia
|
||||
double getTodayRevenue() {
|
||||
return getTodayHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Faturamento da semana
|
||||
double getWeekRevenue() {
|
||||
return getWeekHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Faturamento do mês
|
||||
double getMonthRevenue() {
|
||||
return getMonthHaircuts().fold(0.0, (sum, h) => sum + h.price);
|
||||
}
|
||||
|
||||
// Atualizar corte
|
||||
Future<HaircutModel?> updateHaircut(HaircutModel haircut) async {
|
||||
await DatabaseService.haircutsBoxInstance.put(haircut.id, haircut);
|
||||
return haircut;
|
||||
}
|
||||
|
||||
// Deletar corte
|
||||
Future<void> deleteHaircut(String id) async {
|
||||
final haircut = DatabaseService.haircutsBoxInstance.get(id);
|
||||
if (haircut?.transactionId != null) {
|
||||
await _transactionRepo.deleteTransaction(haircut!.transactionId!);
|
||||
}
|
||||
await DatabaseService.haircutsBoxInstance.delete(id);
|
||||
}
|
||||
|
||||
// Buscar corte por ID
|
||||
HaircutModel? getHaircutById(String id) {
|
||||
return DatabaseService.haircutsBoxInstance.get(id);
|
||||
}
|
||||
}
|
||||
360
lib/features/haircuts/presentation/pages/add_haircut_page.dart
Normal file
360
lib/features/haircuts/presentation/pages/add_haircut_page.dart
Normal file
@@ -0,0 +1,360 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:barber_app/core/theme/app_theme.dart';
|
||||
import 'package:barber_app/core/constants/app_strings.dart';
|
||||
import 'package:barber_app/features/haircuts/data/repositories/haircut_repository.dart';
|
||||
import 'package:barber_app/features/services/data/models/service_model.dart';
|
||||
import 'package:barber_app/features/services/data/repositories/service_repository.dart';
|
||||
import 'package:barber_app/features/services/presentation/pages/services_page.dart';
|
||||
import 'package:barber_app/shared/widgets/custom_text_field.dart';
|
||||
import 'package:barber_app/shared/widgets/loading_button.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class AddHaircutPage extends StatefulWidget {
|
||||
const AddHaircutPage({super.key});
|
||||
|
||||
@override
|
||||
State<AddHaircutPage> createState() => _AddHaircutPageState();
|
||||
}
|
||||
|
||||
class _AddHaircutPageState extends State<AddHaircutPage> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _clientNameController = TextEditingController();
|
||||
final _priceController = TextEditingController();
|
||||
final _notesController = TextEditingController();
|
||||
|
||||
final _serviceRepo = ServiceRepository();
|
||||
List<ServiceModel> _services = [];
|
||||
ServiceModel? _selectedService;
|
||||
|
||||
bool _isLoading = false;
|
||||
final _paymentMethods = [
|
||||
'Dinheiro',
|
||||
'PIX',
|
||||
'Cartão de Crédito',
|
||||
'Cartão de Débito',
|
||||
'Fiado',
|
||||
];
|
||||
String _selectedPaymentMethod = 'Dinheiro';
|
||||
|
||||
final _currencyFormat = NumberFormat.currency(locale: 'pt_BR', symbol: 'R\$');
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadServices();
|
||||
}
|
||||
|
||||
void _loadServices() {
|
||||
setState(() {
|
||||
_services = _serviceRepo.getAllServices();
|
||||
});
|
||||
}
|
||||
|
||||
void _onServiceSelected(ServiceModel? service) {
|
||||
if (service == null) return;
|
||||
setState(() {
|
||||
_selectedService = service;
|
||||
// Preenche o valor automaticamente
|
||||
_priceController.text = service.price.toStringAsFixed(2);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _saveHaircut() async {
|
||||
if (!(_formKey.currentState?.validate() ?? false)) return;
|
||||
|
||||
// Validação flexível: Se selecionou serviço ou digitou preço
|
||||
// Aqui assumimos que serviceType será o nome do serviço selecionado
|
||||
// Se não selecionou, impedimos? Sim, melhor forçar seleção ou ter um 'Outro'
|
||||
if (_selectedService == null) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(const SnackBar(content: Text('Selecione um serviço')));
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _isLoading = true);
|
||||
|
||||
try {
|
||||
final haircutRepo = HaircutRepository();
|
||||
|
||||
final price =
|
||||
double.tryParse(_priceController.text.replaceAll(',', '.')) ??
|
||||
_selectedService!.price;
|
||||
|
||||
await haircutRepo.createHaircut(
|
||||
clientName: _clientNameController.text,
|
||||
serviceType: _selectedService!.name,
|
||||
price: price,
|
||||
dateTime: DateTime.now(),
|
||||
notes: _notesController.text.isNotEmpty ? _notesController.text : null,
|
||||
isPaid: _selectedPaymentMethod != 'Fiado',
|
||||
paymentMethod: _selectedPaymentMethod,
|
||||
);
|
||||
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text(AppStrings.savedSuccessfully),
|
||||
backgroundColor: AppColors.success,
|
||||
),
|
||||
);
|
||||
Navigator.pop(context);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Erro: $e'), backgroundColor: AppColors.error),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _isLoading = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_clientNameController.dispose();
|
||||
_priceController.dispose();
|
||||
_notesController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text(AppStrings.newHaircut),
|
||||
leading: IconButton(
|
||||
icon: const Icon(Icons.arrow_back_ios),
|
||||
onPressed: () => Navigator.pop(context),
|
||||
),
|
||||
),
|
||||
body: SafeArea(
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
// Cliente
|
||||
CustomTextField(
|
||||
controller: _clientNameController,
|
||||
label: AppStrings.clientName,
|
||||
hint: 'Nome do cliente',
|
||||
prefixIcon: Icons.person_outline,
|
||||
textCapitalization: TextCapitalization.words,
|
||||
validator: (v) =>
|
||||
v?.isEmpty ?? true ? 'Informe o nome do cliente' : null,
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Lista de Serviços (Chips)
|
||||
const Text(
|
||||
'Selecione o Serviço',
|
||||
style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
if (_services.isEmpty)
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surfaceLight,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Column(
|
||||
children: [
|
||||
const Text(
|
||||
'Nenhum serviço cadastrado.',
|
||||
style: TextStyle(color: AppColors.textSecondary),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
ElevatedButton.icon(
|
||||
onPressed: () async {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (_) => const ServicesPage(),
|
||||
),
|
||||
);
|
||||
_loadServices();
|
||||
},
|
||||
icon: const Icon(Icons.add, size: 18),
|
||||
label: const Text('Cadastrar Serviços'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: AppColors.primaryColor,
|
||||
foregroundColor: Colors.black,
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 8,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
else
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: _services.map((service) {
|
||||
final isSelected = _selectedService?.id == service.id;
|
||||
return ChoiceChip(
|
||||
label: Text(
|
||||
'${service.name} - ${_currencyFormat.format(service.price)}',
|
||||
style: TextStyle(
|
||||
color: isSelected
|
||||
? Colors.black
|
||||
: (Theme.of(context).brightness ==
|
||||
Brightness.dark
|
||||
? AppColors.textPrimary
|
||||
: Colors.black87),
|
||||
fontWeight: isSelected
|
||||
? FontWeight.bold
|
||||
: FontWeight.normal,
|
||||
),
|
||||
),
|
||||
selected: isSelected,
|
||||
selectedColor: AppColors.primaryColor,
|
||||
backgroundColor: AppColors.surface,
|
||||
side: BorderSide(
|
||||
color: isSelected
|
||||
? AppColors.primaryColor
|
||||
: AppColors.surfaceLight,
|
||||
),
|
||||
onSelected: (selected) {
|
||||
if (selected) _onServiceSelected(service);
|
||||
},
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Valor (Editável)
|
||||
CustomTextField(
|
||||
controller: _priceController,
|
||||
label: AppStrings.price,
|
||||
hint: '0,00',
|
||||
prefixIcon: Icons.attach_money,
|
||||
keyboardType: TextInputType.number,
|
||||
validator: (v) {
|
||||
if (v?.isEmpty ?? true) return 'Informe o valor';
|
||||
final parsed = double.tryParse(v!.replaceAll(',', '.'));
|
||||
if (parsed == null || parsed < 0) return 'Valor inválido';
|
||||
return null;
|
||||
},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// Observações
|
||||
CustomTextField(
|
||||
controller: _notesController,
|
||||
label: AppStrings.notes,
|
||||
hint: 'Observações (opcional)',
|
||||
prefixIcon: Icons.note_outlined,
|
||||
maxLines: 3,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Forma de Pagamento
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(
|
||||
horizontal: 16,
|
||||
vertical: 4,
|
||||
),
|
||||
decoration: BoxDecoration(
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? AppColors.surface
|
||||
: Colors.white,
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
border: Border.all(color: AppColors.surfaceLight),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 8, bottom: 4),
|
||||
child: Text(
|
||||
'Forma de Pagamento',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
fontWeight: FontWeight.bold,
|
||||
color:
|
||||
Theme.of(context).brightness == Brightness.dark
|
||||
? AppColors.textSecondary
|
||||
: Colors.black54,
|
||||
),
|
||||
),
|
||||
),
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: _selectedPaymentMethod,
|
||||
decoration: const InputDecoration(
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
isDense: true,
|
||||
),
|
||||
dropdownColor:
|
||||
Theme.of(context).brightness == Brightness.dark
|
||||
? AppColors.surface
|
||||
: Colors.white,
|
||||
style: TextStyle(
|
||||
color: Theme.of(context).brightness == Brightness.dark
|
||||
? AppColors.textPrimary
|
||||
: Colors.black87,
|
||||
fontSize: 16,
|
||||
),
|
||||
onChanged: (String? newValue) {
|
||||
if (newValue != null) {
|
||||
setState(() {
|
||||
_selectedPaymentMethod = newValue;
|
||||
});
|
||||
}
|
||||
},
|
||||
items: _paymentMethods.map<DropdownMenuItem<String>>((
|
||||
String value,
|
||||
) {
|
||||
return DropdownMenuItem<String>(
|
||||
value: value,
|
||||
child: Text(value),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
|
||||
// Feedback visual do status
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 8),
|
||||
child: Text(
|
||||
_selectedPaymentMethod == 'Fiado'
|
||||
? 'Status: Pendente (A Receber)'
|
||||
: 'Status: Pago (Entra no Caixa)',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: _selectedPaymentMethod == 'Fiado'
|
||||
? AppColors.warning
|
||||
: AppColors.success,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
LoadingButton(
|
||||
text: 'Salvar Corte',
|
||||
isLoading: _isLoading,
|
||||
onPressed: _saveHaircut,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
380
lib/features/haircuts/presentation/pages/haircuts_page.dart
Normal file
380
lib/features/haircuts/presentation/pages/haircuts_page.dart
Normal file
@@ -0,0 +1,380 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:hive_flutter/hive_flutter.dart';
|
||||
import 'package:barber_app/core/database/database_service.dart';
|
||||
import 'package:barber_app/core/theme/app_theme.dart';
|
||||
import 'package:barber_app/core/constants/app_strings.dart';
|
||||
import 'package:barber_app/features/haircuts/data/models/haircut_model.dart';
|
||||
import 'package:barber_app/features/haircuts/data/repositories/haircut_repository.dart';
|
||||
import 'package:barber_app/features/haircuts/presentation/pages/add_haircut_page.dart';
|
||||
|
||||
class HaircutsPage extends StatefulWidget {
|
||||
const HaircutsPage({super.key});
|
||||
|
||||
@override
|
||||
State<HaircutsPage> createState() => _HaircutsPageState();
|
||||
}
|
||||
|
||||
class _HaircutsPageState extends State<HaircutsPage>
|
||||
with SingleTickerProviderStateMixin {
|
||||
late TabController _tabController;
|
||||
final _haircutRepo = HaircutRepository();
|
||||
final _currencyFormat = NumberFormat.currency(locale: 'pt_BR', symbol: 'R\$');
|
||||
String _searchQuery = '';
|
||||
DateTime? _selectedDate;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_tabController = TabController(length: 3, vsync: this);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_tabController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _refreshData() {
|
||||
setState(() {});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text(AppStrings.haircuts),
|
||||
bottom: PreferredSize(
|
||||
preferredSize: const Size.fromHeight(110),
|
||||
child: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 0, 16, 8),
|
||||
child: TextField(
|
||||
onChanged: (value) => setState(() => _searchQuery = value),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Buscar cliente ou serviço...',
|
||||
prefixIcon: Icon(
|
||||
Icons.search,
|
||||
color: AppColors.textSecondary,
|
||||
),
|
||||
filled: true,
|
||||
fillColor: AppColors.surface,
|
||||
contentPadding: const EdgeInsets.symmetric(vertical: 0),
|
||||
border: OutlineInputBorder(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
borderSide: BorderSide.none,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
TabBar(
|
||||
controller: _tabController,
|
||||
labelColor: AppColors.primaryColor,
|
||||
unselectedLabelColor: AppColors.textSecondary,
|
||||
indicatorColor: AppColors.primaryColor,
|
||||
tabs: const [
|
||||
Tab(text: 'Hoje'),
|
||||
Tab(text: 'Semana'),
|
||||
Tab(text: 'Mês'),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: Icon(
|
||||
Icons.calendar_month,
|
||||
color: _selectedDate != null ? AppColors.primaryColor : null,
|
||||
),
|
||||
onPressed: () async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: _selectedDate ?? DateTime.now(),
|
||||
firstDate: DateTime(2020),
|
||||
lastDate: DateTime(2030),
|
||||
);
|
||||
setState(() => _selectedDate = picked);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: ValueListenableBuilder(
|
||||
valueListenable: DatabaseService.haircutsBoxInstance.listenable(),
|
||||
builder: (context, _, _) {
|
||||
return TabBarView(
|
||||
controller: _tabController,
|
||||
children: [
|
||||
_buildHaircutList(
|
||||
_filterHaircuts(_haircutRepo.getTodayHaircuts()),
|
||||
),
|
||||
_buildHaircutList(
|
||||
_filterHaircuts(_haircutRepo.getWeekHaircuts()),
|
||||
),
|
||||
_buildHaircutList(
|
||||
_filterHaircuts(_haircutRepo.getMonthHaircuts()),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
floatingActionButton: Padding(
|
||||
padding: const EdgeInsets.only(bottom: 80),
|
||||
child: FloatingActionButton.extended(
|
||||
heroTag: 'haircuts_fab',
|
||||
onPressed: () async {
|
||||
await Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(builder: (_) => const AddHaircutPage()),
|
||||
);
|
||||
_refreshData();
|
||||
},
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text(AppStrings.newHaircut),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
List<HaircutModel> _filterHaircuts(List<HaircutModel> haircuts) {
|
||||
var filtered = haircuts;
|
||||
|
||||
if (_selectedDate != null) {
|
||||
filtered = filtered
|
||||
.where(
|
||||
(h) =>
|
||||
h.dateTime.year == _selectedDate!.year &&
|
||||
h.dateTime.month == _selectedDate!.month &&
|
||||
h.dateTime.day == _selectedDate!.day,
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
if (_searchQuery.isNotEmpty) {
|
||||
filtered = filtered
|
||||
.where(
|
||||
(h) =>
|
||||
h.clientName.toLowerCase().contains(
|
||||
_searchQuery.toLowerCase(),
|
||||
) ||
|
||||
h.serviceType.toLowerCase().contains(
|
||||
_searchQuery.toLowerCase(),
|
||||
),
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
return filtered;
|
||||
}
|
||||
|
||||
Widget _buildHaircutList(List<HaircutModel> haircuts) {
|
||||
final revenue = haircuts.fold(0.0, (sum, h) => sum + h.price);
|
||||
if (haircuts.isEmpty) {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.content_cut,
|
||||
size: 64,
|
||||
color: AppColors.textSecondary.withValues(alpha: 0.5),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'Nenhum corte registrado',
|
||||
style: TextStyle(color: AppColors.textSecondary, fontSize: 16),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
// Resumo
|
||||
Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
margin: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
colors: [
|
||||
AppColors.primaryColor,
|
||||
AppColors.primaryColor.withValues(alpha: 0.7),
|
||||
],
|
||||
),
|
||||
borderRadius: BorderRadius.circular(16),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceAround,
|
||||
children: [
|
||||
Column(
|
||||
children: [
|
||||
Text(
|
||||
'${haircuts.length}',
|
||||
style: const TextStyle(
|
||||
fontSize: 28,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.background,
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
'Cortes',
|
||||
style: TextStyle(color: AppColors.background),
|
||||
),
|
||||
],
|
||||
),
|
||||
Container(
|
||||
width: 1,
|
||||
height: 40,
|
||||
color: AppColors.background.withValues(alpha: 0.3),
|
||||
),
|
||||
Column(
|
||||
children: [
|
||||
Text(
|
||||
_currencyFormat.format(revenue),
|
||||
style: const TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: AppColors.background,
|
||||
),
|
||||
),
|
||||
const Text(
|
||||
'Faturamento',
|
||||
style: TextStyle(color: AppColors.background),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Lista
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
itemCount: haircuts.length,
|
||||
itemBuilder: (context, index) {
|
||||
final haircut = haircuts[index];
|
||||
return _buildHaircutCard(haircut);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildHaircutCard(HaircutModel haircut) {
|
||||
final dateFormat = DateFormat('dd/MM HH:mm');
|
||||
|
||||
return Container(
|
||||
margin: const EdgeInsets.only(bottom: 12),
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.surface,
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: AppColors.surfaceLight),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 50,
|
||||
height: 50,
|
||||
decoration: BoxDecoration(
|
||||
color: AppColors.primaryColor.withValues(alpha: 0.1),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
),
|
||||
child: Center(
|
||||
child: Text(
|
||||
haircut.clientName[0].toUpperCase(),
|
||||
style: TextStyle(
|
||||
color: AppColors.primaryColor,
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 20,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
haircut.clientName,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.w600,
|
||||
fontSize: 16,
|
||||
color: AppColors.textPrimary,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
haircut.serviceType,
|
||||
style: TextStyle(color: AppColors.textSecondary),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
dateFormat.format(haircut.dateTime),
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: AppColors.textSecondary.withValues(alpha: 0.7),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
Text(
|
||||
_currencyFormat.format(haircut.price),
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
fontSize: 18,
|
||||
color: AppColors.primaryColor,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(Icons.delete_outline, color: AppColors.error),
|
||||
onPressed: () => _confirmDelete(haircut),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _confirmDelete(HaircutModel haircut) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: AppColors.surface,
|
||||
title: const Text('Excluir Corte?'),
|
||||
content: Text('Deseja excluir o corte de ${haircut.clientName}?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text(AppStrings.cancel),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
final navigator = Navigator.of(context);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
await _haircutRepo.deleteHaircut(haircut.id);
|
||||
navigator.pop();
|
||||
_refreshData();
|
||||
scaffoldMessenger.showSnackBar(
|
||||
const SnackBar(content: Text(AppStrings.deletedSuccessfully)),
|
||||
);
|
||||
},
|
||||
child: Text(
|
||||
AppStrings.delete,
|
||||
style: TextStyle(color: AppColors.error),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user