|
| 1 | +import 'package:flutter/material.dart'; |
| 2 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 3 | +import 'package:e_shoppe/data/models/user.dart'; |
| 4 | +import 'package:e_shoppe/data/repositories/customer_repository.dart'; |
| 5 | +import 'order_app_bar.dart'; |
| 6 | + |
| 7 | +class CustomerSearchPage extends ConsumerStatefulWidget { |
| 8 | + const CustomerSearchPage({super.key}); |
| 9 | + |
| 10 | + @override |
| 11 | + ConsumerState<CustomerSearchPage> createState() => _CustomerSearchPageState(); |
| 12 | +} |
| 13 | + |
| 14 | +class _CustomerSearchPageState extends ConsumerState<CustomerSearchPage> { |
| 15 | + final _searchCtrl = TextEditingController(); |
| 16 | + final _repo = CustomerRepository(); |
| 17 | + Future<List<User>>? _future; |
| 18 | + |
| 19 | + void _search() { |
| 20 | + setState(() { |
| 21 | + _future = _repo.searchCustomers(_searchCtrl.text); |
| 22 | + }); |
| 23 | + } |
| 24 | + |
| 25 | + @override |
| 26 | + void dispose() { |
| 27 | + _repo.dispose(); |
| 28 | + _searchCtrl.dispose(); |
| 29 | + super.dispose(); |
| 30 | + } |
| 31 | + |
| 32 | + @override |
| 33 | + Widget build(BuildContext context) { |
| 34 | + return Scaffold( |
| 35 | + backgroundColor: const Color(0xFFE0E0E0), |
| 36 | + appBar: const OrderAppBar(), |
| 37 | + body: Column( |
| 38 | + children: [ |
| 39 | + Padding( |
| 40 | + padding: const EdgeInsets.all(12.0), |
| 41 | + child: Row( |
| 42 | + children: [ |
| 43 | + Expanded( |
| 44 | + child: TextField( |
| 45 | + controller: _searchCtrl, |
| 46 | + decoration: const InputDecoration( |
| 47 | + prefixIcon: Icon(Icons.search), |
| 48 | + hintText: 'Nhập SDT/ tên khách hàng', |
| 49 | + border: OutlineInputBorder(), |
| 50 | + ), |
| 51 | + onSubmitted: (_) => _search(), |
| 52 | + ), |
| 53 | + ), |
| 54 | + const SizedBox(width: 8), |
| 55 | + ElevatedButton(onPressed: _search, child: const Text('Tìm')), |
| 56 | + ], |
| 57 | + ), |
| 58 | + ), |
| 59 | + if (_future == null) |
| 60 | + const Expanded( |
| 61 | + child: Center(child: Text('Nhập từ khoá để tìm khách hàng'))) |
| 62 | + else |
| 63 | + Expanded( |
| 64 | + child: FutureBuilder<List<User>>( |
| 65 | + future: _future, |
| 66 | + builder: (context, snapshot) { |
| 67 | + if (snapshot.connectionState == ConnectionState.waiting) { |
| 68 | + return const Center(child: CircularProgressIndicator()); |
| 69 | + } |
| 70 | + if (snapshot.hasError) { |
| 71 | + return Center(child: Text('Error: ${snapshot.error}')); |
| 72 | + } |
| 73 | + final users = snapshot.data ?? []; |
| 74 | + if (users.isEmpty) { |
| 75 | + return const Center( |
| 76 | + child: Text('Không tìm thấy khách hàng')); |
| 77 | + } |
| 78 | + return ListView.separated( |
| 79 | + itemCount: users.length, |
| 80 | + separatorBuilder: (_, __) => const Divider(height: 1), |
| 81 | + itemBuilder: (context, index) { |
| 82 | + final u = users[index]; |
| 83 | + return ListTile( |
| 84 | + title: Text(u.name), |
| 85 | + subtitle: Text(u.phone ?? u.email), |
| 86 | + onTap: () { |
| 87 | + Navigator.of(context).pop(u); |
| 88 | + }, |
| 89 | + ); |
| 90 | + }, |
| 91 | + ); |
| 92 | + }, |
| 93 | + ), |
| 94 | + ), |
| 95 | + ], |
| 96 | + ), |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
0 commit comments