Просмотр исходного кода

Sincronizacion de toppings seleccionables y sincronizacion de toppings del pedido seleccionados

OscarGil03 месяцев назад: 6
Родитель
Сommit
be2fb5533f

+ 1 - 0
lib/models/pedido_producto_model.dart

@@ -50,6 +50,7 @@ class PedidoProducto extends Basico {
       'descuento': descuento,
       'descuento': descuento,
       'cantidad': cantidad,
       'cantidad': cantidad,
       'comentario': comentario,
       'comentario': comentario,
+      'toppings': toppings.map((topping) => topping.toApi()).toList(),
     };
     };
   }
   }
 
 

+ 13 - 0
lib/models/pedido_producto_toping_model.dart

@@ -4,12 +4,14 @@ import 'basico_model.dart';
 class PedidoProductoTopping extends Basico {
 class PedidoProductoTopping extends Basico {
   int? idPedidoProducto;
   int? idPedidoProducto;
   int? idTopping;
   int? idTopping;
+  int? idCategoria;
   Producto? topping;
   Producto? topping;
 
 
   PedidoProductoTopping({
   PedidoProductoTopping({
     super.id,
     super.id,
     this.idPedidoProducto,
     this.idPedidoProducto,
     this.idTopping,
     this.idTopping,
+    this.idCategoria,
     this.topping,
     this.topping,
   });
   });
 
 
@@ -19,12 +21,23 @@ class PedidoProductoTopping extends Basico {
       'id': id,
       'id': id,
       'idPedidoProducto': idPedidoProducto,
       'idPedidoProducto': idPedidoProducto,
       'idTopping': idTopping,
       'idTopping': idTopping,
+      'idCategoria': idCategoria,
     }..addAll(super.toJson());
     }..addAll(super.toJson());
   }
   }
 
 
+  Map<String, dynamic> toApi() {
+    return {
+      'idTopping': idTopping,
+      'idCategoria': idCategoria,
+      'nombreTopping': topping?.nombre ?? '',
+      'precio': topping?.precio ?? 0.0,
+    };
+  }
+
   PedidoProductoTopping.fromJson(Map<String, dynamic> json) {
   PedidoProductoTopping.fromJson(Map<String, dynamic> json) {
     super.parseJson(json);
     super.parseJson(json);
     idPedidoProducto = Basico.parseInt(json['idPedidoProducto']);
     idPedidoProducto = Basico.parseInt(json['idPedidoProducto']);
     idTopping = Basico.parseInt(json['idTopping']);
     idTopping = Basico.parseInt(json['idTopping']);
+    idCategoria = Basico.parseInt(json['idCategoria']);
   }
   }
 }
 }

+ 8 - 0
lib/services/repo_service.dart

@@ -559,6 +559,14 @@ class RepoService<T> {
             ALTER TABLE ProductoTopping ADD COLUMN idLocal INTEGER;
             ALTER TABLE ProductoTopping ADD COLUMN idLocal INTEGER;
           ''');
           ''');
 
 
+          await db.execute('''
+            ALTER TABLE PedidoProductoTopping ADD COLUMN idLocal INTEGER;
+          ''');
+
+          await db.execute('''
+            ALTER TABLE PedidoProductoTopping ADD COLUMN idCategoria INTEGER;
+          ''');
+
           break;
           break;
       }
       }
       oldVersion++;
       oldVersion++;

+ 1 - 0
lib/viewmodels/pedido_view_model.dart

@@ -60,6 +60,7 @@ class PedidoViewModel extends ChangeNotifier {
           PedidoProductoTopping pedidoProductoTopping = PedidoProductoTopping(
           PedidoProductoTopping pedidoProductoTopping = PedidoProductoTopping(
             idPedidoProducto: idPedidoProducto,
             idPedidoProducto: idPedidoProducto,
             idTopping: topping.idTopping,
             idTopping: topping.idTopping,
+            idCategoria: topping.idCategoria,
           );
           );
           await repoPedidoProductoTopping.guardarLocal(pedidoProductoTopping);
           await repoPedidoProductoTopping.guardarLocal(pedidoProductoTopping);
 
 

+ 72 - 84
lib/views/pedido/pedido_form.dart

@@ -755,6 +755,7 @@ class _PedidoFormState extends State<PedidoForm> {
       item.selectedToppings.forEach((categoryId, selectedToppingIds) {
       item.selectedToppings.forEach((categoryId, selectedToppingIds) {
         for (int toppingId in selectedToppingIds) {
         for (int toppingId in selectedToppingIds) {
           selectedToppings.add(PedidoProductoTopping(
           selectedToppings.add(PedidoProductoTopping(
+            idCategoria: categoryId,
             idTopping: toppingId,
             idTopping: toppingId,
           ));
           ));
         }
         }
@@ -1103,99 +1104,87 @@ class _PedidoFormState extends State<PedidoForm> {
                         ],
                         ],
                       ),
                       ),
                     ),
                     ),
+                    // ExpansionTile para todos los toppings
                     if (item.selectableToppings.isNotEmpty)
                     if (item.selectableToppings.isNotEmpty)
                       ExpansionTile(
                       ExpansionTile(
-                        title: Text('Toppings',
-                            style: const TextStyle(
-                                fontWeight: FontWeight.bold, fontSize: 16)),
+                        title: const Text(
+                          'Toppings',
+                          style: TextStyle(
+                              fontWeight: FontWeight.bold, fontSize: 16),
+                        ),
                         children: item.selectableToppings.entries.map((entry) {
                         children: item.selectableToppings.entries.map((entry) {
                           final categoryId = entry.key;
                           final categoryId = entry.key;
                           final availableToppings = entry.value;
                           final availableToppings = entry.value;
                           final categoria =
                           final categoria =
                               categorias.firstWhere((c) => c.id == categoryId);
                               categorias.firstWhere((c) => c.id == categoryId);
 
 
-                          return Column(
-                            crossAxisAlignment: CrossAxisAlignment.start,
-                            children: [
-                              Padding(
-                                padding: const EdgeInsets.only(left: 16.0),
-                                child: Text(
-                                  categoria.descripcion!,
-                                  style: const TextStyle(
-                                      fontWeight: FontWeight.bold,
-                                      fontSize: 16),
-                                ),
-                              ),
-                              ...availableToppings.map((topping) {
-                                ValueNotifier<bool> isSelectedNotifier =
-                                    ValueNotifier<bool>(
-                                  item.selectedToppings[categoryId]
-                                          ?.contains(topping.id) ??
-                                      false,
-                                );
-
-                                return ValueListenableBuilder<bool>(
-                                  valueListenable: isSelectedNotifier,
-                                  builder: (context, isSelected, _) {
-                                    return CheckboxListTile(
-                                      activeColor: AppTheme.primary,
-                                      title: Row(
-                                        mainAxisAlignment:
-                                            MainAxisAlignment.spaceBetween,
-                                        children: [
-                                          Text(topping.nombre!),
-                                          if (topping.precio != 0.0)
-                                            Text(
-                                              '+\$${topping.precio}',
-                                              style: TextStyle(
-                                                color: Colors.black,
-                                                fontSize: 13,
-                                              ),
-                                            ),
-                                        ],
+                          return ExpansionTile(
+                            initiallyExpanded: true,
+                            title: Text(
+                              categoria.nombre!,
+                              style: const TextStyle(
+                                  fontWeight: FontWeight.bold, fontSize: 16),
+                            ),
+                            children: availableToppings.map((topping) {
+                              bool isSelected = item
+                                      .selectedToppings[categoryId]
+                                      ?.contains(topping.id) ??
+                                  false;
+                              return CheckboxListTile(
+                                activeColor: AppTheme.primary,
+                                title: Row(
+                                  mainAxisAlignment:
+                                      MainAxisAlignment.spaceBetween,
+                                  children: [
+                                    Text(topping.nombre!),
+                                    if (topping.precio != 0.0)
+                                      Text(
+                                        '+\$${topping.precio}',
+                                        style: const TextStyle(
+                                          color: Colors.black,
+                                          fontSize: 13,
+                                        ),
                                       ),
                                       ),
-                                      value: isSelected,
-                                      onChanged: (bool? value) {
-                                        final maximoToppings =
-                                            categoria.maximo ?? 0;
-
-                                        if (value == true) {
-                                          if ((item.selectedToppings[categoryId]
-                                                      ?.length ??
-                                                  0) >=
-                                              maximoToppings) {
-                                            item.selectedToppings[categoryId]!
-                                                .remove(item
-                                                    .selectedToppings[
-                                                        categoryId]!
-                                                    .first);
-                                          }
-                                          item.selectedToppings[categoryId] ??=
-                                              <int>{};
-                                          item.selectedToppings[categoryId]!
-                                              .add(topping.id!);
-                                        } else {
-                                          item.selectedToppings[categoryId]
-                                              ?.remove(topping.id!);
-                                          if (item.selectedToppings[categoryId]
-                                                  ?.isEmpty ??
-                                              false) {
-                                            item.selectedToppings
-                                                .remove(categoryId);
-                                          }
-                                        }
-                                        setState(() {});
-                                        _recalcularTotal();
-                                      },
-                                    );
-                                  },
-                                );
-                              }).toList(),
-                            ],
+                                  ],
+                                ),
+                                value: isSelected,
+                                onChanged: (bool? value) {
+                                  final maximoToppings = categoria.maximo ?? 0;
+
+                                  setState(() {
+                                    if (value == true) {
+                                      if ((item.selectedToppings[categoryId]
+                                                  ?.length ??
+                                              0) >=
+                                          maximoToppings) {
+                                        item.selectedToppings[categoryId]!
+                                            .remove(item
+                                                .selectedToppings[categoryId]!
+                                                .first);
+                                      }
+                                      item.selectedToppings[categoryId] ??=
+                                          <int>{};
+                                      item.selectedToppings[categoryId]!
+                                          .add(topping.id!);
+                                    } else {
+                                      item.selectedToppings[categoryId]
+                                          ?.remove(topping.id!);
+                                      if (item.selectedToppings[categoryId]
+                                              ?.isEmpty ??
+                                          false) {
+                                        item.selectedToppings
+                                            .remove(categoryId);
+                                      }
+                                    }
+                                    _recalcularTotal();
+                                  });
+                                },
+                              );
+                            }).toList(),
                           );
                           );
                         }).toList(),
                         }).toList(),
                       ),
                       ),
-                    Divider(),
+                    const Divider(),
                   ],
                   ],
                 );
                 );
               },
               },
@@ -1210,7 +1199,7 @@ class _PedidoFormState extends State<PedidoForm> {
             child: ElevatedButton(
             child: ElevatedButton(
               onPressed: _finalizeOrder,
               onPressed: _finalizeOrder,
               style: ElevatedButton.styleFrom(
               style: ElevatedButton.styleFrom(
-                primary: AppTheme.tertiary,
+                backgroundColor: AppTheme.tertiary,
                 textStyle: const TextStyle(fontSize: 22),
                 textStyle: const TextStyle(fontSize: 22),
                 fixedSize: const Size(250, 50),
                 fixedSize: const Size(250, 50),
               ),
               ),
@@ -1352,10 +1341,9 @@ class _PedidoFormState extends State<PedidoForm> {
                   });
                   });
                 },
                 },
                 style: ElevatedButton.styleFrom(
                 style: ElevatedButton.styleFrom(
-                  primary: isSelected ? AppTheme.tertiary : Colors.grey,
+                  backgroundColor: isSelected ? AppTheme.tertiary : Colors.grey,
                   foregroundColor:
                   foregroundColor:
                       isSelected ? AppTheme.quaternary : AppTheme.secondary,
                       isSelected ? AppTheme.quaternary : AppTheme.secondary,
-                  onPrimary: AppTheme.secondary,
                 ),
                 ),
                 child: Text(categoria.nombre!),
                 child: Text(categoria.nombre!),
               ),
               ),

+ 4 - 37
lib/views/pedido/pedido_screen.dart

@@ -10,7 +10,6 @@ import '../../models/models.dart';
 import '../../viewmodels/viewmodels.dart';
 import '../../viewmodels/viewmodels.dart';
 import '../../widgets/widgets_components.dart' as clase;
 import '../../widgets/widgets_components.dart' as clase;
 import 'pedido_form.dart';
 import 'pedido_form.dart';
-import 'pedido_sync.dart';
 
 
 class PedidoScreen extends StatefulWidget {
 class PedidoScreen extends StatefulWidget {
   const PedidoScreen({Key? key}) : super(key: key);
   const PedidoScreen({Key? key}) : super(key: key);
@@ -20,7 +19,6 @@ class PedidoScreen extends StatefulWidget {
 }
 }
 
 
 class _PedidoScreenState extends State<PedidoScreen> {
 class _PedidoScreenState extends State<PedidoScreen> {
-  int _syncAgainTapCount = 0;
   final _busqueda = TextEditingController(text: '');
   final _busqueda = TextEditingController(text: '');
   DateTime? fechaInicio;
   DateTime? fechaInicio;
   DateTime? fechaFin;
   DateTime? fechaFin;
@@ -214,19 +212,10 @@ class _PedidoScreenState extends State<PedidoScreen> {
 
 
     return Scaffold(
     return Scaffold(
       appBar: AppBar(
       appBar: AppBar(
-          title: GestureDetector(
-            onTap: () {
-              _syncAgainTapCount++;
-              if (_syncAgainTapCount == 5) {
-                alertaSync(context);
-                _syncAgainTapCount = 0;
-              }
-            },
-            child: Text(
-              'Pedidos',
-              style: TextStyle(
-                  color: AppTheme.secondary, fontWeight: FontWeight.w500),
-            ),
+          title: Text(
+            'Pedidos',
+            style: TextStyle(
+                color: AppTheme.secondary, fontWeight: FontWeight.w500),
           ),
           ),
           actions: <Widget>[
           actions: <Widget>[
             IconButton(
             IconButton(
@@ -432,28 +421,6 @@ class _PedidoScreenState extends State<PedidoScreen> {
               backgroundColor: AppTheme.tertiary,
               backgroundColor: AppTheme.tertiary,
             ),
             ),
           ),
           ),
-          // Positioned(
-          //   bottom: 16,
-          //   left: 16,
-          //   child: FloatingActionButton.extended(
-          //     heroTag: 'sincronizacion',
-          //     onPressed: () {
-          //       alerta(context, etiqueta: "Sincronización Empezada");
-          //       PedidoSync().startSync(
-          //           Provider.of<PedidoViewModel>(context, listen: false));
-          //     },
-          //     icon: Icon(Icons.sync, size: 30, color: AppTheme.quaternary),
-          //     label: Text(
-          //       "Sincronización",
-          //       style: TextStyle(fontSize: 20, color: AppTheme.quaternary),
-          //     ),
-          //     shape: RoundedRectangleBorder(
-          //       borderRadius: BorderRadius.circular(8),
-          //     ),
-          //     materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
-          //     backgroundColor: AppTheme.tertiary,
-          //   ),
-          // ),
         ],
         ],
       ),
       ),
     );
     );