如何在Flutter中切换选项卡时保留我的内容?

huangapple go评论59阅读模式
英文:

How can I keep my content when swapping tabs on flutter?

问题

I lost my information when I change the tab
当我切换选项卡时,我的信息丢失了

I was trying to keep information with a provider but when I change to another my information get lost this is my code.
我试图使用提供程序保存信息,但当我切换到另一个时,我的信息丢失了,这是我的代码。

Right now this is the only problem that I can find on the code but I think there's more problems on it.
现在这是我在代码中找到的唯一问题,但我认为它还有更多问题。

Widget historialProducts(List products){
小部件 historialProducts(List products){

return ListView.builder(
返回ListView.builder(

itemCount: products.length,
项目数:products.length

itemBuilder: (context, i){
项目生成器:(context,i){

final producto = products[i];
最终产品=产品[i];

return ListTile(
返回ListTile(

title: Text(context.watch().product.descripcion),
标题:Text(context.watch().product.descripcion),

subtitle: Text(producto.codItem),
副标题:Text(producto.codItem),

trailing: Text(producto.precioVentaActual.toString()),
尾随:Text(producto.precioVentaActual.toString()),

);
);
}
);
}

The code you provided seems to be related to a Flutter application, but without the full context, it's challenging to provide a comprehensive translation. If you have specific questions or need further assistance with your code, please feel free to ask.

英文:

I lost my information when I change the tab

I was trying to keep information with a provider but when I change to another my information get lost this is my code. I don't know how to keep all the content, if you guys need more code say it and I post the rest of the code.
Right now this is the only problem that I can find on the code but I think there's more problems on it.

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)
          )
        ],
      ),
    );
  }
}```

</details>


# 答案1
**得分**: 1

Here is the translated code portion:

```dart
你可以尝试使用这个小部件;

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()),
    ],
  ),
 ),

Please let me know if you need any further assistance or have more questions!

英文:

You can try this 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&lt;KeepAlivePage&gt; createState() =&gt; _KeepAlivePageState();
}

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

  @override
  bool get wantKeepAlive =&gt; widget.keepAlive;
}

You can use it like this;

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

huangapple
  • 本文由 发表于 2023年7月31日 22:03:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76804383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定