简体   繁体   English

如何更新 Flutter 中的 ListView.separated?

[英]How to update ListView.separated in Flutter?

When I press the button, my list should be updated, but it doesn't.当我按下按钮时,我的列表应该更新,但它没有。 The code of button is:按钮代码如下:

        ```onPressed: () {

              // ------------- Добавление покупки ----------------------------
              //List<Item> items;
              item.Name = 'Huy'; // Сделать имя покупки на выбор
              item.shopID = lastItemID + 1;  // TODO сделать правильное ID для каждой покупки
              item.Cost = 1000; // Цену на выбор
              item.Amount = 20; // Количество на выбор
              item.AmountType = 1; // Кг./шт.
              item.isDone = 0;
              item.ID = _newShopItemsID();

              newItems.add(item);

              // -------------------------------------------------------------
              setState(() {
                numberOfItems = newItems.length;
                print(numberOfItems.toString());
              });
              Navigator.of(context).pop();
          },```

And ListView.separated code:和 ListView.separated 代码:

                 ```ListView.separated(
                      itemCount: numberOfItems,
                      itemBuilder: (BuildContext context, int index) {
                          return Stack(
                            children: <Widget>[
                              Container(
                                  height: 40.0,
                                  color: Colors.blue[100],
                              )
                            ],
                          );
                      },
                      separatorBuilder: (BuildContext context,
                        int index) => const Divider(height: 0.0,
                        color: Colors.black), // Разделитель для покупок
                  )```

It updates only when I re-open the page with this list仅当我使用此列表重新打开页面时才会更新

Try this尝试这个

setState(() {
    newItems.add(item);
    numberOfItems = newItems.length;
    print(numberOfItems.toString());
});

I don't know how you implement the rest of your code, so here is a very simple working example using Dialog.我不知道您是如何实现代码的 rest 的,所以这里有一个使用 Dialog 的非常简单的工作示例。

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'App',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Home());
  }
}

class Home extends StatefulWidget {
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  List<Item> items = [];
  void _showDialog() {
    showDialog(
        context: context,
        builder: (context) {
          return AlertDialog(
            title: Text(' Add Item'),
            actions: <Widget>[
              FlatButton(
                onPressed: () {
                  Item item = Item(
                    name: 'Huy',
                    shopID: 1,
                    cost: 1000,
                    amount: 20,
                    amountType: 1,
                    isDone: 0,
                    iD: 1,
                  );
                  items.add(item);
                  setState(() {});
                  Navigator.of(context).pop();
                },
                child: Text('Add'),
              )
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ListView'),
      ),
      body: ListView.separated(
        itemCount: items.length,
        itemBuilder: (context, index) {
          return Stack(
            children: <Widget>[
              Container(
                height: 40.0,
                color: Colors.blue[100],
              )
            ],
          );
        },
        separatorBuilder: (BuildContext context, int index) =>
            const Divider(height: 0.0, color: Colors.black),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          _showDialog();
        },
      ),
    );
  }
}

class Item {
  final name;
  final shopID;
  final cost;
  final amount;
  final amountType;
  final isDone;
  final iD;

  Item(
      {this.name,
      this.shopID,
      this.cost,
      this.amount,
      this.amountType,
      this.isDone,
      this.iD});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM