如何更新Flutter中的ListView.separated?

时间:2019-10-31 10:03:09

标签: flutter dart

当我按下按钮时,我的列表应该被更新,但是没有更新。 按钮的代码为:

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

和ListView。分隔的代码:

                 ```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), // Разделитель для покупок
                  )```

仅当我重新打开带有此列表的页面时,它才会更新

2 个答案:

答案 0 :(得分:0)

尝试

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

答案 1 :(得分:0)

我不知道您如何实现其余代码,因此这是一个使用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});
}