feat: Agente Ventas SIGMAC — app de rutas y pedidos de campo
App Flutter 2.11 para vendedores de ruta de Telcotronics. Incluye ruta del día con mapa custom, catálogo, pedidos CRUD, firma digital, scanner QR/barcode, selector de clientes escalable, notificaciones in-app y soporte multiplataforma (Android + Linux desktop). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,299 @@
|
||||
import 'dart:async';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/cliente.dart';
|
||||
import '../../theme/app_theme.dart';
|
||||
|
||||
const int _kMaxResultados = 50;
|
||||
|
||||
/// Pantalla de búsqueda de clientes. Retorna el [Cliente] seleccionado o null.
|
||||
///
|
||||
/// Uso:
|
||||
/// ```dart
|
||||
/// final cliente = await Navigator.push<Cliente>(
|
||||
/// context,
|
||||
/// MaterialPageRoute(builder: (_) => ClienteSelectorScreen(clientes: provider.clientes)),
|
||||
/// );
|
||||
/// ```
|
||||
class ClienteSelectorScreen extends StatefulWidget {
|
||||
final List<Cliente> clientes;
|
||||
final String titulo;
|
||||
|
||||
const ClienteSelectorScreen({
|
||||
Key? key,
|
||||
required this.clientes,
|
||||
this.titulo = 'Seleccionar cliente',
|
||||
}) : super(key: key);
|
||||
|
||||
@override
|
||||
State<ClienteSelectorScreen> createState() => _ClienteSelectorScreenState();
|
||||
}
|
||||
|
||||
class _ClienteSelectorScreenState extends State<ClienteSelectorScreen> {
|
||||
final _searchCtrl = TextEditingController();
|
||||
final _focusNode = FocusNode();
|
||||
Timer? _debounce;
|
||||
|
||||
String _query = '';
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Autofocus al abrir
|
||||
WidgetsBinding.instance!.addPostFrameCallback((_) => _focusNode.requestFocus());
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_debounce?.cancel();
|
||||
_searchCtrl.dispose();
|
||||
_focusNode.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _onSearchChanged(String value) {
|
||||
_debounce?.cancel();
|
||||
_debounce = Timer(const Duration(milliseconds: 250), () {
|
||||
setState(() => _query = value.trim());
|
||||
});
|
||||
}
|
||||
|
||||
List<Cliente> get _filtrados {
|
||||
if (_query.isEmpty) return const [];
|
||||
final q = _query.toLowerCase();
|
||||
return widget.clientes.where((c) {
|
||||
return c.nombre.toLowerCase().contains(q) ||
|
||||
c.nit.toLowerCase().contains(q) ||
|
||||
c.direccion.toLowerCase().contains(q) ||
|
||||
c.categoria.toLowerCase().contains(q);
|
||||
}).take(_kMaxResultados + 1).toList(); // +1 para detectar si hay más
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final resultados = _filtrados;
|
||||
final hayMas = resultados.length > _kMaxResultados;
|
||||
final visibles = hayMas ? resultados.take(_kMaxResultados).toList() : resultados;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: AppTheme.bgColor,
|
||||
appBar: AppBar(
|
||||
titleSpacing: 0,
|
||||
title: TextField(
|
||||
controller: _searchCtrl,
|
||||
focusNode: _focusNode,
|
||||
onChanged: _onSearchChanged,
|
||||
style: const TextStyle(color: Colors.white, fontSize: 15),
|
||||
decoration: InputDecoration(
|
||||
hintText: 'Buscar por nombre, NIT o dirección...',
|
||||
hintStyle: TextStyle(color: Colors.white.withOpacity(0.55), fontSize: 14),
|
||||
border: InputBorder.none,
|
||||
prefixIcon: const Icon(Icons.search, color: Colors.white70, size: 22),
|
||||
suffixIcon: _searchCtrl.text.isNotEmpty
|
||||
? IconButton(
|
||||
icon: const Icon(Icons.clear, color: Colors.white70, size: 20),
|
||||
onPressed: () {
|
||||
_searchCtrl.clear();
|
||||
setState(() => _query = '');
|
||||
_focusNode.requestFocus();
|
||||
},
|
||||
)
|
||||
: null,
|
||||
),
|
||||
),
|
||||
),
|
||||
body: _buildBody(visibles, hayMas),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildBody(List<Cliente> visibles, bool hayMas) {
|
||||
if (_query.isEmpty) {
|
||||
return _buildEstadoInicial();
|
||||
}
|
||||
if (visibles.isEmpty) {
|
||||
return _buildSinResultados();
|
||||
}
|
||||
return Column(
|
||||
children: [
|
||||
_buildContador(visibles.length, hayMas),
|
||||
Expanded(
|
||||
child: ListView.builder(
|
||||
keyboardDismissBehavior: ScrollViewKeyboardDismissBehavior.onDrag,
|
||||
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
|
||||
itemCount: visibles.length + (hayMas ? 1 : 0),
|
||||
itemBuilder: (ctx, i) {
|
||||
if (i == visibles.length) return _buildMasResultados();
|
||||
return _buildClienteTile(visibles[i]);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEstadoInicial() {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.search, size: 64, color: Colors.grey.shade300),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'${widget.clientes.length} clientes disponibles',
|
||||
style: const TextStyle(fontSize: 15, fontWeight: FontWeight.w500, color: AppTheme.textSecondary),
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
const Text(
|
||||
'Escribe para buscar',
|
||||
style: TextStyle(fontSize: 13, color: AppTheme.textSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildSinResultados() {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(Icons.store_outlined, size: 56, color: Colors.grey.shade300),
|
||||
const SizedBox(height: 14),
|
||||
Text(
|
||||
'Sin resultados para "$_query"',
|
||||
style: const TextStyle(fontSize: 14, color: AppTheme.textSecondary),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
const SizedBox(height: 6),
|
||||
const Text(
|
||||
'Prueba con nombre, NIT o dirección',
|
||||
style: TextStyle(fontSize: 12, color: AppTheme.textSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildContador(int cantidad, bool hayMas) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 10, 16, 4),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
hayMas
|
||||
? 'Mostrando $_kMaxResultados de más resultados — refina la búsqueda'
|
||||
: '$cantidad resultado${cantidad == 1 ? '' : 's'}',
|
||||
style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildClienteTile(Cliente c) {
|
||||
return Card(
|
||||
margin: const EdgeInsets.only(bottom: 6),
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
side: const BorderSide(color: Color(0xFFE2E8F0)),
|
||||
),
|
||||
child: InkWell(
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
onTap: () => Navigator.pop(context, c),
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
CircleAvatar(
|
||||
radius: 20,
|
||||
backgroundColor: AppTheme.primaryColor.withOpacity(0.1),
|
||||
child: const Icon(Icons.store, size: 20, color: AppTheme.primaryColor),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildTextoResaltado(c.nombre, _query,
|
||||
const TextStyle(fontWeight: FontWeight.w600, fontSize: 14, color: AppTheme.textPrimary)),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
c.direccion,
|
||||
style: const TextStyle(fontSize: 12, color: AppTheme.textSecondary),
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Row(
|
||||
children: [
|
||||
_buildChip(c.categoria),
|
||||
if (c.nit.isNotEmpty) ...[
|
||||
const SizedBox(width: 6),
|
||||
Text('NIT ${c.nit}',
|
||||
style: const TextStyle(fontSize: 10, color: AppTheme.textSecondary)),
|
||||
],
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(Icons.chevron_right, color: AppTheme.textSecondary, size: 20),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildMasResultados() {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
child: Center(
|
||||
child: Text(
|
||||
'Hay más resultados — refina la búsqueda para verlos',
|
||||
style: TextStyle(fontSize: 12, color: Colors.grey.shade500),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildChip(String texto) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 6, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: AppTheme.primaryColor.withOpacity(0.08),
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
),
|
||||
child: Text(texto, style: const TextStyle(fontSize: 10, color: AppTheme.primaryColor, fontWeight: FontWeight.w500)),
|
||||
);
|
||||
}
|
||||
|
||||
/// Resalta el texto que coincide con la búsqueda en negrita
|
||||
Widget _buildTextoResaltado(String texto, String query, TextStyle baseStyle) {
|
||||
if (query.isEmpty) return Text(texto, style: baseStyle, maxLines: 1, overflow: TextOverflow.ellipsis);
|
||||
final lower = texto.toLowerCase();
|
||||
final idx = lower.indexOf(query.toLowerCase());
|
||||
if (idx < 0) return Text(texto, style: baseStyle, maxLines: 1, overflow: TextOverflow.ellipsis);
|
||||
|
||||
return RichText(
|
||||
maxLines: 1,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
text: TextSpan(
|
||||
style: baseStyle,
|
||||
children: [
|
||||
if (idx > 0) TextSpan(text: texto.substring(0, idx)),
|
||||
TextSpan(
|
||||
text: texto.substring(idx, idx + query.length),
|
||||
style: baseStyle.copyWith(
|
||||
backgroundColor: AppTheme.primaryColor.withOpacity(0.18),
|
||||
color: AppTheme.primaryColor,
|
||||
),
|
||||
),
|
||||
if (idx + query.length < texto.length)
|
||||
TextSpan(text: texto.substring(idx + query.length)),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user