mirror of
https://github.com/SyncrowIOT/web.git
synced 2025-07-16 18:16:34 +00:00
Enhance AddDeviceTypeWidget to support initial product counts and update selection logic. Modify AssignTagsDialog to pass initial products from space allocations, improving user experience and maintainability.
This commit is contained in:
@ -10,7 +10,12 @@ import 'package:syncrow_web/utils/color_manager.dart';
|
|||||||
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
import 'package:syncrow_web/utils/extension/build_context_x.dart';
|
||||||
|
|
||||||
class AddDeviceTypeWidget extends StatefulWidget {
|
class AddDeviceTypeWidget extends StatefulWidget {
|
||||||
const AddDeviceTypeWidget({super.key});
|
const AddDeviceTypeWidget({
|
||||||
|
super.key,
|
||||||
|
this.initialProducts = const [],
|
||||||
|
});
|
||||||
|
|
||||||
|
final List<Product> initialProducts;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
State<AddDeviceTypeWidget> createState() => _AddDeviceTypeWidgetState();
|
State<AddDeviceTypeWidget> createState() => _AddDeviceTypeWidgetState();
|
||||||
@ -18,6 +23,16 @@ class AddDeviceTypeWidget extends StatefulWidget {
|
|||||||
|
|
||||||
class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
||||||
final Map<Product, int> _selectedProducts = {};
|
final Map<Product, int> _selectedProducts = {};
|
||||||
|
final Map<Product, int> _initialProductCounts = {};
|
||||||
|
|
||||||
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
for (final product in widget.initialProducts) {
|
||||||
|
_initialProductCounts[product] = (_initialProductCounts[product] ?? 0) + 1;
|
||||||
|
}
|
||||||
|
_selectedProducts.addAll(_initialProductCounts);
|
||||||
|
}
|
||||||
|
|
||||||
void _onIncrement(Product product) {
|
void _onIncrement(Product product) {
|
||||||
setState(() {
|
setState(() {
|
||||||
@ -27,8 +42,12 @@ class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
|||||||
|
|
||||||
void _onDecrement(Product product) {
|
void _onDecrement(Product product) {
|
||||||
setState(() {
|
setState(() {
|
||||||
if ((_selectedProducts[product] ?? 0) > 0) {
|
final initialCount = _initialProductCounts[product] ?? 0;
|
||||||
_selectedProducts[product] = _selectedProducts[product]! - 1;
|
final currentCount = _selectedProducts[product] ?? 0;
|
||||||
|
if (currentCount > initialCount) {
|
||||||
|
_selectedProducts[product] = currentCount - 1;
|
||||||
|
} else if (currentCount > 0 && initialCount == 0) {
|
||||||
|
_selectedProducts[product] = currentCount - 1;
|
||||||
if (_selectedProducts[product] == 0) {
|
if (_selectedProducts[product] == 0) {
|
||||||
_selectedProducts.remove(product);
|
_selectedProducts.remove(product);
|
||||||
}
|
}
|
||||||
@ -63,7 +82,22 @@ class _AddDeviceTypeWidgetState extends State<AddDeviceTypeWidget> {
|
|||||||
actions: [
|
actions: [
|
||||||
SpaceDetailsActionButtons(
|
SpaceDetailsActionButtons(
|
||||||
onSave: () {
|
onSave: () {
|
||||||
final result = _selectedProducts.entries
|
final resultMap = <Product, int>{};
|
||||||
|
resultMap.addAll(_selectedProducts);
|
||||||
|
|
||||||
|
for (final entry in _initialProductCounts.entries) {
|
||||||
|
final product = entry.key;
|
||||||
|
final initialCount = entry.value;
|
||||||
|
final currentCount = resultMap[product] ?? 0;
|
||||||
|
|
||||||
|
if (currentCount > initialCount) {
|
||||||
|
resultMap[product] = currentCount - initialCount;
|
||||||
|
} else {
|
||||||
|
resultMap.remove(product);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
final result = resultMap.entries
|
||||||
.expand((entry) => List.generate(entry.value, (_) => entry.key))
|
.expand((entry) => List.generate(entry.value, (_) => entry.key))
|
||||||
.toList();
|
.toList();
|
||||||
Navigator.of(context).pop(result);
|
Navigator.of(context).pop(result);
|
||||||
|
@ -205,7 +205,14 @@ class _AssignTagsDialogState extends State<AssignTagsDialog> {
|
|||||||
onCancel: () async {
|
onCancel: () async {
|
||||||
final newProducts = await showDialog<List<Product>>(
|
final newProducts = await showDialog<List<Product>>(
|
||||||
context: context,
|
context: context,
|
||||||
builder: (context) => const AddDeviceTypeWidget(),
|
builder: (context) => AddDeviceTypeWidget(
|
||||||
|
initialProducts: [
|
||||||
|
..._space.productAllocations.map((e) => e.product),
|
||||||
|
..._space.subspaces
|
||||||
|
.expand((s) => s.productAllocations)
|
||||||
|
.map((e) => e.product),
|
||||||
|
],
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (newProducts == null || newProducts.isEmpty) return;
|
if (newProducts == null || newProducts.isEmpty) return;
|
||||||
|
Reference in New Issue
Block a user