如何更新Flutter中的ListView.separate?

问题描述 投票:0回答:2

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

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

仅在我重新打开带有此列表的页面时更新

flutter dart
2个回答
0
投票

尝试此

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

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});
}
© www.soinside.com 2019 - 2024. All rights reserved.