forked from hesabix/arc
248 lines
7.9 KiB
Dart
Executable file
248 lines
7.9 KiB
Dart
Executable file
import 'package:flutter/material.dart';
|
|
import '../../services/warehouse_service.dart';
|
|
import '../../models/warehouse_model.dart';
|
|
import '../../utils/responsive_helper.dart';
|
|
|
|
class WarehouseComboboxWidget extends StatefulWidget {
|
|
final int businessId;
|
|
final Future<List<Warehouse>>? warehousesFuture;
|
|
final int? selectedWarehouseId;
|
|
final ValueChanged<int?> onChanged;
|
|
final String label;
|
|
final String hintText;
|
|
final bool isRequired;
|
|
final double? height; // ارتفاع اختیاری برای هماهنگی با سایر فیلدها
|
|
|
|
/// اگر true باشد، پس از بارگذاری لیست انبارها و وقتی هنوز انباری انتخاب نشده،
|
|
/// نخستین انباری که در API با [Warehouse.isDefault] مشخص شده انتخاب و به [onChanged] اعلام میشود.
|
|
/// برای فیلتر گزارشها که null یعنی «همه انبارها» است، باید false بماند.
|
|
final bool selectDefaultWhenUnset;
|
|
|
|
const WarehouseComboboxWidget({
|
|
super.key,
|
|
required this.businessId,
|
|
this.warehousesFuture,
|
|
required this.onChanged,
|
|
this.selectedWarehouseId,
|
|
this.label = 'انبار',
|
|
this.hintText = 'انتخاب انبار',
|
|
this.isRequired = false,
|
|
this.height,
|
|
this.selectDefaultWhenUnset = false,
|
|
});
|
|
|
|
@override
|
|
State<WarehouseComboboxWidget> createState() => _WarehouseComboboxWidgetState();
|
|
}
|
|
|
|
class _WarehouseComboboxWidgetState extends State<WarehouseComboboxWidget> {
|
|
final WarehouseService _service = WarehouseService();
|
|
List<Warehouse> _items = const <Warehouse>[];
|
|
bool _loading = false;
|
|
int? _selectedValue;
|
|
bool _resolvedInitialDefault = false;
|
|
bool _loadFailed = false;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_selectedValue = widget.selectedWarehouseId;
|
|
_load();
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(WarehouseComboboxWidget oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.businessId != widget.businessId) {
|
|
_resolvedInitialDefault = false;
|
|
_selectedValue = widget.selectedWarehouseId;
|
|
_load();
|
|
return;
|
|
}
|
|
if (oldWidget.selectedWarehouseId != widget.selectedWarehouseId) {
|
|
_selectedValue = widget.selectedWarehouseId;
|
|
}
|
|
if (oldWidget.selectDefaultWhenUnset != widget.selectDefaultWhenUnset) {
|
|
_resolvedInitialDefault = false;
|
|
}
|
|
}
|
|
|
|
int? _firstDefaultWarehouseId(List<Warehouse> items) {
|
|
for (final w in items) {
|
|
if (w.isDefault && w.id != null) {
|
|
return w.id;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
void _applyInitialDefaultIfNeeded(List<Warehouse> items) {
|
|
if (_resolvedInitialDefault || !widget.selectDefaultWhenUnset) {
|
|
_resolvedInitialDefault = true;
|
|
return;
|
|
}
|
|
if (widget.selectedWarehouseId != null || _selectedValue != null) {
|
|
_resolvedInitialDefault = true;
|
|
return;
|
|
}
|
|
final defId = _firstDefaultWarehouseId(items);
|
|
if (defId != null) {
|
|
_selectedValue = defId;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
widget.onChanged(defId);
|
|
});
|
|
}
|
|
_resolvedInitialDefault = true;
|
|
}
|
|
|
|
Future<void> _load({bool retry = false}) async {
|
|
if (mounted) {
|
|
setState(() {
|
|
_loading = true;
|
|
_loadFailed = false;
|
|
});
|
|
}
|
|
try {
|
|
final items = await (retry
|
|
? _service.listWarehouses(businessId: widget.businessId)
|
|
: widget.warehousesFuture ??
|
|
_service.listWarehouses(businessId: widget.businessId));
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_items = items;
|
|
_applyInitialDefaultIfNeeded(items);
|
|
});
|
|
} catch (_) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_items = const <Warehouse>[];
|
|
_loadFailed = true;
|
|
_resolvedInitialDefault = true;
|
|
});
|
|
} finally {
|
|
if (mounted) setState(() => _loading = false);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
final effectiveHeight = widget.height ?? 48.0; // پیشفرض 48px
|
|
|
|
if (_loading) {
|
|
return SizedBox(
|
|
height: effectiveHeight,
|
|
child: Center(
|
|
child: SizedBox(width: 18, height: 18, child: CircularProgressIndicator(strokeWidth: 2, color: theme.colorScheme.primary)),
|
|
),
|
|
);
|
|
}
|
|
if (_loadFailed) {
|
|
return SizedBox(
|
|
height: effectiveHeight,
|
|
child: OutlinedButton.icon(
|
|
onPressed: () => _load(retry: true),
|
|
icon: const Icon(Icons.refresh, size: 18),
|
|
label: const Text('بارگذاری انبار ناموفق بود؛ تلاش مجدد'),
|
|
),
|
|
);
|
|
}
|
|
// ساخت لیست آیتمها
|
|
final List<DropdownMenuItem<int?>> menuItems = [
|
|
if (!widget.isRequired)
|
|
const DropdownMenuItem<int?>(
|
|
value: null,
|
|
child: Text('بدون انبار', maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
),
|
|
..._items.map((w) {
|
|
final title = (w.code.isNotEmpty) ? '${w.code} - ${w.name}' : w.name;
|
|
return DropdownMenuItem<int?>(
|
|
value: w.id!,
|
|
child: Text(title, maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
);
|
|
}),
|
|
];
|
|
|
|
// استفاده از همان contentPadding که در سایر فیلدها استفاده میشود
|
|
final useDense = false; // همیشه false برای هماهنگی با سایر فیلدها
|
|
final contentPadding = ResponsiveHelper.isMobile(context)
|
|
? const EdgeInsets.symmetric(horizontal: 16, vertical: 12)
|
|
: const EdgeInsets.symmetric(horizontal: 12, vertical: 10);
|
|
|
|
return SizedBox(
|
|
height: effectiveHeight,
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: DropdownButtonFormField<int?>(
|
|
value: _selectedValue,
|
|
isExpanded: true,
|
|
decoration: InputDecoration(
|
|
isDense: useDense,
|
|
contentPadding: contentPadding,
|
|
labelText: widget.label,
|
|
hintText: widget.hintText,
|
|
border: const OutlineInputBorder(),
|
|
suffixIcon: _selectedValue != null && !widget.isRequired
|
|
? IconButton(
|
|
icon: const Icon(Icons.clear, size: 18),
|
|
onPressed: () {
|
|
// بهروزرسانی state داخلی و فراخوانی callback
|
|
setState(() {
|
|
_selectedValue = null;
|
|
});
|
|
widget.onChanged(null);
|
|
},
|
|
tooltip: 'پاک کردن',
|
|
)
|
|
: null,
|
|
),
|
|
selectedItemBuilder: (BuildContext context) {
|
|
// برگرداندن لیست ویجتها با همان ترتیب items برای نمایش متن انتخاب شده
|
|
final List<Widget> result = [];
|
|
if (!widget.isRequired) {
|
|
result.add(
|
|
Align(
|
|
alignment: AlignmentDirectional.centerEnd,
|
|
child: Text(
|
|
'بدون انبار',
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
textAlign: TextAlign.right,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
for (final w in _items) {
|
|
final title = (w.code.isNotEmpty) ? '${w.code} - ${w.name}' : w.name;
|
|
result.add(
|
|
Align(
|
|
alignment: AlignmentDirectional.centerEnd,
|
|
child: Text(
|
|
title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
textAlign: TextAlign.right,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
return result;
|
|
},
|
|
items: menuItems,
|
|
onChanged: (int? value) {
|
|
setState(() {
|
|
_selectedValue = value;
|
|
});
|
|
widget.onChanged(value);
|
|
},
|
|
validator: widget.isRequired
|
|
? (value) => value == null ? '${widget.label} الزامی است' : null
|
|
: null,
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
|