You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
4.7 KiB
Dart
133 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:test_sa/api/devices_api_client.dart';
|
|
|
|
import '../../../controllers/localization/localization.dart';
|
|
import '../../../controllers/providers/api/devices_provider.dart';
|
|
import '../../../models/device/device.dart';
|
|
import '../../../models/subtitle.dart';
|
|
import '../app_text_form_field.dart';
|
|
import '../loaders/loading_manager.dart';
|
|
import '../loaders/no_item_found.dart';
|
|
import '../qr/scan_qr.dart';
|
|
import 'device_item.dart';
|
|
|
|
class SingleDevicePicker extends StatefulWidget {
|
|
static const String id = "/single-device-Picker";
|
|
final bool sandraChoice = true;
|
|
|
|
const SingleDevicePicker({super.key});
|
|
|
|
@override
|
|
SingleDevicePickerState createState() => SingleDevicePickerState();
|
|
}
|
|
|
|
class SingleDevicePickerState extends State<SingleDevicePicker> {
|
|
late DevicesProvider _devicesProvider;
|
|
Subtitle? _subtitle;
|
|
|
|
_getDevice(String? result) async {
|
|
if (result == null) return;
|
|
showDialog(
|
|
barrierDismissible: false,
|
|
context: context,
|
|
builder: (dialogContext) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
});
|
|
List<Device> devices = await _devicesProvider.getDevicesListBySN(serialNumber: result);
|
|
Navigator.of(context).pop();
|
|
if (devices.isEmpty) {
|
|
Fluttertoast.showToast(msg: _subtitle?.noDeviceFound ?? "");
|
|
return;
|
|
}
|
|
Navigator.of(context).pop(devices.first);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_devicesProvider.reset();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
_devicesProvider = Provider.of<DevicesProvider>(context);
|
|
_subtitle = AppLocalization.of(context)?.subtitle;
|
|
return Scaffold(
|
|
resizeToAvoidBottomInset: false,
|
|
body: LoadingManager(
|
|
isLoading: _devicesProvider.loading,
|
|
stateCode: _devicesProvider.stateCode,
|
|
onRefresh: () async {
|
|
_devicesProvider.reset();
|
|
await _devicesProvider.getEquipment();
|
|
},
|
|
child: Column(
|
|
children: [
|
|
const SizedBox(height: 48),
|
|
Padding(
|
|
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 16),
|
|
child: Column(
|
|
children: [
|
|
ATextFormField(
|
|
hintText: _subtitle?.searchBySn ?? "",
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
suffixIcon: const Icon(Icons.search_rounded),
|
|
onChange: (value) {
|
|
_devicesProvider.searchableList.clear();
|
|
_devicesProvider.searchableList.addAll(DevicesApiClient().devices.where((element) => element.serialNumber!.toLowerCase().contains(value.toLowerCase())).toList());
|
|
setState(() {});
|
|
},
|
|
),
|
|
const SizedBox(height: 8),
|
|
ATextFormField(
|
|
hintText: "Search by Number",
|
|
style: Theme.of(context).textTheme.titleMedium,
|
|
suffixIcon: const Icon(Icons.search_rounded),
|
|
onChange: (value) {
|
|
_devicesProvider.searchableList.clear();
|
|
_devicesProvider.searchableList.addAll(DevicesApiClient().devices.where((element) => element.number!.toLowerCase().contains(value.toLowerCase())).toList());
|
|
setState(() {});
|
|
},
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Expanded(
|
|
child: _devicesProvider.searchableList.isEmpty
|
|
? NoItemFound(message: _subtitle?.noDeviceFound ?? "")
|
|
: ListView.builder(
|
|
padding: EdgeInsets.zero,
|
|
shrinkWrap: true,
|
|
itemCount: _devicesProvider.searchableList.length,
|
|
itemBuilder: (listContext, itemIndex) {
|
|
return DeviceItem(
|
|
device: _devicesProvider.searchableList[itemIndex],
|
|
onPressed: (device) {
|
|
Navigator.of(context).pop(device);
|
|
},
|
|
);
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
floatingActionButton: FloatingActionButton(
|
|
heroTag: "some tag 2",
|
|
child: const Icon(Icons.qr_code_scanner),
|
|
onPressed: () async {
|
|
String result = await Navigator.of(context).push(MaterialPageRoute(builder: (_) => const ScanQr())) as String;
|
|
_getDevice(result);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|