在flutter上交换标签时,我如何保留我的内容?

q9rjltbz  于 2023-08-07  发布在  Flutter
关注(0)|答案(1)|浏览(108)

当我更改标签时,我的信息丢失了
我试图保持与提供商的信息,但当我改变到另一个我的信息丢失,这是我的代码。我不知道如何保持所有的内容,如果你们需要更多的代码,说出来,我张贴其余的代码。现在这是我能在代码中找到的唯一问题,但我认为还有更多的问题。

import 'package:provider/provider.dart';
import '../../providers/item_provider.dart';
import '../../search/product_search.dart';
import '../../models/product.dart';

class Busqueda extends StatefulWidget {
  final TabController tabController;

  const Busqueda(this.tabController);

  @override
  State<Busqueda> createState() => _BusquedaState();
}

class _BusquedaState extends State<Busqueda> {
  late Product productoSeleccionado = Product(
      fotoUrl: '',
      itemId: 0,
      codItem: '',
      descripcion: '',
      monedaId: 0,
      precioVentaActual: 0,
      existenciaActual: 0,
      precioIvaIncluido: 0,
      existenciaTotal: 0,
      ivaId: 0,
      valor: 0);
  List<Product> historial = [];

  Widget historialProducts(List<Product> products){
    return ListView.builder(
      itemCount: products.length,
      itemBuilder: (context, i){

        final producto = products[i];
        return ListTile(
          title: Text(context.watch<ItemProvider>().product.descripcion),
          subtitle: Text(producto.codItem),
          trailing: Text(producto.precioVentaActual.toString()),
        );
      }
    );
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: SearchBar(
              hintText: context.watch<ItemProvider>().product.descripcion.toString(),
              textStyle: MaterialStatePropertyAll(TextStyle(color: Colors.grey)),
              onTap: () async {
                final producto = await showSearch(
                    context: context,
                    delegate:
                        ProductSearchDelegate('Buscar Producto', historial));
                if (producto != null) {
                  setState(() {
                    productoSeleccionado = producto;
                    final int productoExiste = historial.indexWhere(
                      (element) => element.codItem == producto.codItem);
                    if (productoExiste == -1) {
                      historial.insert(0, producto);
                    }
                    context.read<ItemProvider>().setProduct(producto);
                    widget.tabController.animateTo(2);
                  });
                }
              },
            ),
          ),
          Expanded(
            child: historialProducts(this.historial)
          )
        ],
      ),
    );
  }
}```

字符串

1cklez4t

1cklez4t1#

你可以试试这个widget。

class KeepAlivePage extends StatefulWidget {
  const KeepAlivePage({Key? key, required this.child, required this.keepAlive}) : super(key: key);

  final Widget child;
  final bool keepAlive;

  @override
  State<KeepAlivePage> createState() => _KeepAlivePageState();
}

class _KeepAlivePageState extends State<KeepAlivePage> with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return widget.child;
  }

  @override
  bool get wantKeepAlive => widget.keepAlive;
}

字符串
你可以这样使用它;

Expanded(
  child: TabBarView(
    controller: tabController,
    children: const [
      KeepAlivePage(keepAlive: true, child: Tab1()),
      KeepAlivePage(keepAlive: true, child: Tab2()),
    ],
  ),
 ),

相关问题