done with employee list dropdown updation
This commit is contained in:
parent
7c4f86dd94
commit
7a816ed26e
@ -1,3 +1,4 @@
|
|||||||
|
import 'dart:async';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:get/get.dart';
|
import 'package:get/get.dart';
|
||||||
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';
|
import 'package:on_field_work/helpers/utils/base_bottom_sheet.dart';
|
||||||
@ -24,33 +25,67 @@ class EmployeeSelectionBottomSheet extends StatefulWidget {
|
|||||||
class _EmployeeSelectionBottomSheetState
|
class _EmployeeSelectionBottomSheetState
|
||||||
extends State<EmployeeSelectionBottomSheet> {
|
extends State<EmployeeSelectionBottomSheet> {
|
||||||
final TextEditingController _searchController = TextEditingController();
|
final TextEditingController _searchController = TextEditingController();
|
||||||
|
|
||||||
final RxBool _isSearching = false.obs;
|
final RxBool _isSearching = false.obs;
|
||||||
final RxList<EmployeeModel> _searchResults = <EmployeeModel>[].obs;
|
final RxList<EmployeeModel> _allResults = <EmployeeModel>[].obs;
|
||||||
|
|
||||||
late RxList<EmployeeModel> _selectedEmployees;
|
late RxList<EmployeeModel> _selectedEmployees;
|
||||||
|
|
||||||
|
Timer? _debounce;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
_selectedEmployees = RxList<EmployeeModel>.from(widget.initiallySelected);
|
_selectedEmployees = RxList<EmployeeModel>.from(widget.initiallySelected);
|
||||||
_searchEmployees('');
|
|
||||||
|
_performSearch('');
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void dispose() {
|
void dispose() {
|
||||||
|
_debounce?.cancel();
|
||||||
_searchController.dispose();
|
_searchController.dispose();
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _searchEmployees(String query) async {
|
// ------------------------------------------------------
|
||||||
|
// 🔥 Optimized debounce-based search
|
||||||
|
// ------------------------------------------------------
|
||||||
|
void _onSearchChanged(String query) {
|
||||||
|
_debounce?.cancel();
|
||||||
|
_debounce = Timer(const Duration(milliseconds: 300), () {
|
||||||
|
_performSearch(query.trim());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _performSearch(String query) async {
|
||||||
_isSearching.value = true;
|
_isSearching.value = true;
|
||||||
|
|
||||||
final data = await ApiService.searchEmployeesBasic(searchString: query);
|
final data = await ApiService.searchEmployeesBasic(searchString: query);
|
||||||
|
|
||||||
final results = (data as List)
|
final results = (data as List)
|
||||||
.map((e) => EmployeeModel.fromJson(e as Map<String, dynamic>))
|
.map((e) => EmployeeModel.fromJson(e as Map<String, dynamic>))
|
||||||
.toList();
|
.toList();
|
||||||
_searchResults.assignAll(results);
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// 🔥 Auto-move selected employees to top
|
||||||
|
// ------------------------------------------------------
|
||||||
|
results.sort((a, b) {
|
||||||
|
final aSel = _selectedEmployees.contains(a) ? 0 : 1;
|
||||||
|
final bSel = _selectedEmployees.contains(b) ? 0 : 1;
|
||||||
|
|
||||||
|
if (aSel != bSel) return aSel.compareTo(bSel);
|
||||||
|
return a.name.toLowerCase().compareTo(b.name.toLowerCase());
|
||||||
|
});
|
||||||
|
|
||||||
|
_allResults.assignAll(results);
|
||||||
|
|
||||||
_isSearching.value = false;
|
_isSearching.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Handle tap & checkbox
|
||||||
|
// ------------------------------------------------------
|
||||||
void _toggleEmployee(EmployeeModel emp) {
|
void _toggleEmployee(EmployeeModel emp) {
|
||||||
if (widget.multipleSelection) {
|
if (widget.multipleSelection) {
|
||||||
if (_selectedEmployees.contains(emp)) {
|
if (_selectedEmployees.contains(emp)) {
|
||||||
@ -61,9 +96,14 @@ class _EmployeeSelectionBottomSheetState
|
|||||||
} else {
|
} else {
|
||||||
_selectedEmployees.assignAll([emp]);
|
_selectedEmployees.assignAll([emp]);
|
||||||
}
|
}
|
||||||
_selectedEmployees.refresh(); // important for Obx rebuild
|
|
||||||
|
// Re-sort list after each toggle
|
||||||
|
_performSearch(_searchController.text.trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Submit selection
|
||||||
|
// ------------------------------------------------------
|
||||||
void _handleSubmit() {
|
void _handleSubmit() {
|
||||||
if (widget.multipleSelection) {
|
if (widget.multipleSelection) {
|
||||||
Navigator.of(context).pop(_selectedEmployees.toList());
|
Navigator.of(context).pop(_selectedEmployees.toList());
|
||||||
@ -73,11 +113,14 @@ class _EmployeeSelectionBottomSheetState
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Search bar widget
|
||||||
|
// ------------------------------------------------------
|
||||||
Widget _searchBar() => Padding(
|
Widget _searchBar() => Padding(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||||
child: TextField(
|
child: TextField(
|
||||||
controller: _searchController,
|
controller: _searchController,
|
||||||
onChanged: _searchEmployees,
|
onChanged: _onSearchChanged,
|
||||||
decoration: InputDecoration(
|
decoration: InputDecoration(
|
||||||
hintText: 'Search employees...',
|
hintText: 'Search employees...',
|
||||||
filled: true,
|
filled: true,
|
||||||
@ -88,7 +131,7 @@ class _EmployeeSelectionBottomSheetState
|
|||||||
icon: const Icon(Icons.close, color: Colors.grey),
|
icon: const Icon(Icons.close, color: Colors.grey),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
_searchController.clear();
|
_searchController.clear();
|
||||||
_searchEmployees('');
|
_performSearch('');
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
: null,
|
: null,
|
||||||
@ -102,60 +145,52 @@ class _EmployeeSelectionBottomSheetState
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Employee list (optimized)
|
||||||
|
// ------------------------------------------------------
|
||||||
Widget _employeeList() => Expanded(
|
Widget _employeeList() => Expanded(
|
||||||
child: Obx(() {
|
child: Obx(() {
|
||||||
if (_isSearching.value) {
|
final results = _allResults;
|
||||||
return const Center(child: CircularProgressIndicator());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_searchResults.isEmpty) {
|
|
||||||
return const Center(child: Text("No employees found"));
|
|
||||||
}
|
|
||||||
|
|
||||||
return ListView.builder(
|
return ListView.builder(
|
||||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||||
itemCount: _searchResults.length,
|
itemCount: results.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
final emp = _searchResults[index];
|
final emp = results[index];
|
||||||
|
final isSelected = _selectedEmployees.contains(emp);
|
||||||
|
|
||||||
return Obx(() {
|
return ListTile(
|
||||||
final isSelected = _selectedEmployees.contains(emp);
|
leading: CircleAvatar(
|
||||||
return ListTile(
|
backgroundColor: Colors.blueAccent,
|
||||||
leading: CircleAvatar(
|
child: Text(
|
||||||
backgroundColor: Colors.blueAccent,
|
(emp.firstName.isNotEmpty ? emp.firstName[0] : 'U')
|
||||||
child: Text(
|
.toUpperCase(),
|
||||||
(emp.firstName.isNotEmpty ? emp.firstName[0] : 'U')
|
style: const TextStyle(color: Colors.white),
|
||||||
.toUpperCase(),
|
|
||||||
style: const TextStyle(color: Colors.white),
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
title: Text('${emp.firstName} ${emp.lastName}'),
|
),
|
||||||
subtitle: Text(emp.email),
|
title: Text('${emp.firstName} ${emp.lastName}'),
|
||||||
trailing: Checkbox(
|
subtitle: Text(emp.email),
|
||||||
value: isSelected,
|
trailing: Checkbox(
|
||||||
onChanged: (_) {
|
value: isSelected,
|
||||||
FocusScope.of(context).unfocus(); // hide keyboard
|
onChanged: (_) => _toggleEmployee(emp),
|
||||||
_toggleEmployee(emp);
|
fillColor: MaterialStateProperty.resolveWith<Color>(
|
||||||
},
|
(states) => states.contains(MaterialState.selected)
|
||||||
fillColor: MaterialStateProperty.resolveWith<Color>(
|
? Colors.blueAccent
|
||||||
(states) => states.contains(MaterialState.selected)
|
: Colors.white,
|
||||||
? Colors.blueAccent
|
|
||||||
: Colors.white,
|
|
||||||
),
|
|
||||||
),
|
),
|
||||||
onTap: () {
|
),
|
||||||
FocusScope.of(context).unfocus();
|
onTap: () => _toggleEmployee(emp),
|
||||||
_toggleEmployee(emp);
|
contentPadding:
|
||||||
},
|
const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
|
||||||
contentPadding:
|
);
|
||||||
const EdgeInsets.symmetric(horizontal: 0, vertical: 4),
|
|
||||||
);
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// ------------------------------------------------------
|
||||||
|
// Build bottom sheet
|
||||||
|
// ------------------------------------------------------
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return BaseBottomSheet(
|
return BaseBottomSheet(
|
||||||
@ -164,10 +199,12 @@ class _EmployeeSelectionBottomSheetState
|
|||||||
onSubmit: _handleSubmit,
|
onSubmit: _handleSubmit,
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
height: MediaQuery.of(context).size.height * 0.7,
|
height: MediaQuery.of(context).size.height * 0.7,
|
||||||
child: Column(children: [
|
child: Column(
|
||||||
_searchBar(),
|
children: [
|
||||||
_employeeList(),
|
_searchBar(),
|
||||||
]),
|
_employeeList(),
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user