【问题标题】:Flutter how to add items in listFlutter如何在列表中添加项目
【发布时间】:2021-08-02 18:01:05
【问题描述】:

这是我的代码


    class _VariantsState extends State<Variants> {
      List<VariantDetails> variantsList = [];
    
      @override
      void initState() {
        super.initState();
        variantsList.clear();
      }
      
         return Card(
            shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(5.0),
            ),
            color: Colors.white,
            elevation: 5,
            margin:
                EdgeInsets.only(top: 40.0, bottom: 40.0, left: 20.0, right: 20.0),
            child: SingleChildScrollView(
              child: Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(
                        top: 10, bottom: 5.0, left: 20.0, right: 20.0),
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: <Widget>[
                        Text(
                          'Variants',
                          style: TextStyle(
                              fontWeight: FontWeight.bold, color: Colors.black),
                        ),
                        IconButton(
                            onPressed: () {
                              setUpData();
                              showModalBottomSheet(
                                  isScrollControlled: true,
                                  context: context,
                                  builder: (BuildContext context) {
                                    return SingleChildScrollView(
                                        child: Container(
                                      color: Colors.white,
                                      margin: EdgeInsets.all(10.0),
                                      child: Flexible(
                                        child: Padding(
                                          padding: EdgeInsets.only(
                                              left: 10.0, right: 10.0),
                                          child: Column(
                                            crossAxisAlignment:
                                                CrossAxisAlignment.stretch,
                                            mainAxisSize: MainAxisSize.max,
                                            mainAxisAlignment:
                                                MainAxisAlignment.start,
                                            children: <Widget>[
                                              Row(
                                                children: <Widget>[
                                                  Image.asset(
                                                    'assets/svg/barcode_reader.png',
                                                    height: 20.0,
                                                    width: 20.0,
                                                  ),
                                                  Padding(
                                                      padding: EdgeInsets.only(
                                                          left: 20.0)),
                                                  Text(
                                                    'Add Goods',
                                                    style: TextStyle(
                                                        fontWeight: FontWeight.bold,
                                                        color: Colors.black,
                                                        fontSize: 17.0),
                                                  )
                                                ],
                                              ),
                                              Form(
                                                  key: _formKey,
                                                  child: Column(
                                                    children: <Widget>[
                                                      Padding(
                                                        padding: EdgeInsets.only(
                                                          bottom: 20.0,
                                                          top: 40.0,
                                                        ),
                                                        child: TextFormField(
                                                          // initialValue:
                                                          //     variantDetails.name,
                                                          validator: (value) {
                                                            if (value
                                                                .trim()
                                                                .isEmpty) {
                                                              return 'Enter Name';
                                                            }
                                                            return null;
                                                          },
                                                          onChanged: (value) =>
                                                              variantDetails.name =
                                                                  value,
                                                          decoration:
                                                              InputDecoration(
                                                            border:
                                                                OutlineInputBorder(),
                                                            hintText: 'Name',
                                                            hintStyle: TextStyle(
                                                                fontSize: 13.0),
                                                            hintMaxLines: 1,
                                                            contentPadding:
                                                                EdgeInsets.fromLTRB(
                                                                    20.0,
                                                                    8.0,
                                                                    20.0,
                                                                    8.0),
                                                          ),
                                                        ),
                                                      ),
                                                      Padding(
                                                        padding: EdgeInsets.only(
                                                          bottom: 10.0,
                                                        ),
                                                        child: TextFormField(
                                                          // initialValue:
                                                          //     variantDetails
                                                          //         .description,
                                                          onChanged: (value) =>
                                                              variantDetails
                                                                      .description =
                                                                  value,
                                                          validator: (value) {
                                                            if (value
                                                                .trim()
                                                                .isEmpty) {
                                                              return 'Enter Description';
                                                            }
                                                            return null;
                                                          },
                                                          decoration:
                                                              InputDecoration(
                                                            border:
                                                                OutlineInputBorder(),
                                                            hintText: 'Description',
                                                            hintStyle: TextStyle(
                                                                fontSize: 13.0),
                                                            hintMaxLines: 1,
                                                            contentPadding:
                                                                EdgeInsets.fromLTRB(
                                                                    20.0,
                                                                    15.0,
                                                                    20.0,
                                                                    15.0),
                                                          ),
                                                        ),
                                                      ),
                                                      Row(
                                                        mainAxisAlignment:
                                                            MainAxisAlignment
                                                                .spaceAround,
                                                        children: <Widget>[
                                                          Expanded(
                                                            child: TextFormField(
                                                              // initialValue:
                                                              //     variantDetails
                                                              //         .price,
                                                              onChanged: (value) =>
                                                                  variantDetails
                                                                          .price =
                                                                      value,
                                                              validator: (value) {
                                                                if (value
                                                                    .trim()
                                                                    .isEmpty) {
                                                                  return 'Enter Price';
                                                                }
                                                                return null;
                                                              },
                                                              keyboardType:
                                                                  TextInputType
                                                                      .phone,
                                                              decoration:
                                                                  InputDecoration(
                                                                border:
                                                                    OutlineInputBorder(),
                                                                hintText: 'Price',
                                                                hintStyle:
                                                                    TextStyle(
                                                                        fontSize:
                                                                            13.0),
                                                                hintMaxLines: 1,
                                                                contentPadding:
                                                                    EdgeInsets
                                                                        .fromLTRB(
                                                                            20.0,
                                                                            15.0,
                                                                            20.0,
                                                                            15.0),
                                                              ),
                                                            ),
                                                          ),
                                                          Padding(
                                                              padding:
                                                                  EdgeInsets.only(
                                                                      left: 10.0)),
                                                          Expanded(
                                                            child: TextFormField(
                                                              // initialValue:
                                                              //     variantDetails
                                                              //         .nonTax,
                                                              decoration:
                                                                  InputDecoration(
                                                                border:
                                                                    OutlineInputBorder(),
                                                                hintText:
                                                                    'Inclusive of GST',
                                                                hintStyle:
                                                                    TextStyle(
                                                                        fontSize:
                                                                            13.0),
                                                                hintMaxLines: 1,
                                                                suffixIcon: Icon(Icons
                                                                    .arrow_drop_down),
                                                                contentPadding:
                                                                    EdgeInsets
                                                                        .fromLTRB(
                                                                            20.0,
                                                                            15.0,
                                                                            20.0,
                                                                            15.0),
                                                              ),
                                                            ),
                                                          )
                                                        ],
                                                      ),
                                                    ],
                                                  )),
                                              Padding(
                                                  padding:
                                                      EdgeInsets.only(top: 10.0)),
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.spaceBetween,
                                                children: <Widget>[
                                                  Text(
                                                    'Net Price',
                                                    style: TextStyle(
                                                        fontWeight: FontWeight.bold,
                                                        color: Colors.black,
                                                        fontSize: 17.0),
                                                  ),
                                                  Text(
                                                    '0',
                                                    style: TextStyle(
                                                        fontWeight: FontWeight.bold,
                                                        color: Colors.black,
                                                        fontSize: 17.0),
                                                  )
                                                ],
                                              ),
                                              Padding(
                                                  padding:
                                                      EdgeInsets.only(top: 10.0)),
                                              Row(
                                                mainAxisAlignment:
                                                    MainAxisAlignment.spaceBetween,
                                                children: <Widget>[
                                                  Text(
                                                    'Total',
                                                    style: TextStyle(
                                                        fontWeight: FontWeight.bold,
                                                        color: Colors.black,
                                                        fontSize: 17.0),
                                                  ),
                                                  Text(
                                                    '0',
                                                    style: TextStyle(
                                                        fontWeight: FontWeight.bold,
                                                        color: Colors.black,
                                                        fontSize: 17.0),
                                                  )
                                                ],
                                              ),
                                              Padding(
                                                  padding:
                                                      EdgeInsets.only(top: 10.0)),
                                              ElevatedButton(
                                                  onPressed: () {
                                                    trySubmit(context);
                                                  },
                                                  child: Text('UPDATE'))
                                            ],
                                          ),
                                        ),
                                      ),
                                    ));
                                  });
                            },
                            icon: Icon(Icons.add_circle_outline)),
                      ],
                    ),
                  ),
                  Visibility(
                    visible: isVisible,
                    child: new ListView.builder(
                      shrinkWrap: true,
                      itemBuilder: (BuildContext context, int index) {
                        return new ExpandableListView('Variant $index', ItemList);
                      },
                      itemCount: variantsList.length,
                    ),
                  )
              
                ],
              ),
            ));
     

//这是我显示列表视图的可扩展列表

class ExpandableListView extends StatefulWidget {
  final String title;
  final List<VariantDetails> variantList;

  ExpandableListView(this.title, this.variantList);

  @override
  _ExpandableListViewState createState() =>
      new _ExpandableListViewState(this.title, this.variantList);
}

class _ExpandableListViewState extends State<ExpandableListView> {
  bool expandFlag = false;
  final String title;
  final List<VariantDetails> variantList;
  _ExpandableListViewState(this.title, this.variantList);

  @override
  Widget build(BuildContext context) {
    return new Container(
      margin: new EdgeInsets.symmetric(vertical: 1.0),
      child: new Column(
        children: <Widget>[
          new Container(
            color: Colors.white,
            padding: new EdgeInsets.symmetric(horizontal: 20.0),
            child: new Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: <Widget>[
                new Text(
                  widget.title,
                  style: new TextStyle(
                      fontWeight: FontWeight.bold, color: Colors.black),
                ),
                new IconButton(
                    icon: new Container(
                      height: 20.0,
                      width: 20.0,
                      child: new Center(
                        child: new Icon(
                          expandFlag
                              ? Icons.keyboard_arrow_up
                              : Icons.keyboard_arrow_down,
                          color: Colors.black,
                          size: 30.0,
                        ),
                      ),
                    ),
                    onPressed: () {
                      setState(() {
                        expandFlag = !expandFlag;
                      });
                    }),
              ],
            ),
          ),
          new ExpandableContainer(
              expanded: expandFlag,
              child: new ListView.builder(
                itemBuilder: (BuildContext context, int index) {
                  return new Container(
                      child: Padding(
                    padding:
                        EdgeInsets.only(left: 20.0, right: 20.0, top: 10.0),
                    child: Column(
                      children: <Widget>[
                        Row(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisSize: MainAxisSize.max,
                          children: <Widget>[
                            Text(
                              'Edit Variants',
                              style: TextStyle(
                                  fontSize: ScreenUtil().setSp(14),
                                  fontFamily: 'poppins',
                                  color: Colors.black),
                            ),
                            Row(
                              children: <Widget>[
                                IconButton(
                                    onPressed: () {
                                      //  Dismissible(key: Key(variantList[index]), child: child)
                                    },
                                    icon: Icon(Icons.delete)),
                                Icon(Icons.edit),
                              ],
                            )
                          ],
                        ),
                        Padding(padding: EdgeInsets.only(top: 20)),
                        Row(
                          // mainAxisAlignment: MainAxisAlignment.spaceEvenly,
                          children: <Widget>[
                            Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: <Widget>[
                                Text('Name',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Price',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Description',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Price Type',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Non-taxable',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Code Type',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Stock Keeping Unit',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Net Price',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                                Text('Tax',
                                    style: new TextStyle(
                                        fontFamily: 'poppins',
                                        fontSize: ScreenUtil().setSp(12),
                                        color: Colors.black)),
                              ],
                            ),
                            Expanded(
                                child: Column(
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: [
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                                Text(variantList[index].description),
                              ],
                            ))
                          ],
                        ),
                      ],
                    ),
                  ));
                },
                itemCount: variantList.length,
              ))
        ],
      ),
    );
  }
}

                

我正在我的颤振应用程序中实现列表视图,每当我在 variablesList 中添加项目时,新添加的项目都会反映在列表的所有索引中。

例如,我要添加 A 变体列表[0]='A'

现在我正在添加另一个值 B 我得到的结果是

variantsList[0]='B',
variantsList[1]='B',

期待结果

variantsList[0]='A',
variantsList[1]='B',

提前致谢

【问题讨论】:

  • 什么是variantDetails?您如何向variantsList 添加项目?
  • 你不应该在 pop 之后调用 setState
  • 详细信息还不够,添加更多信息以获得您想要的?!
  • 为列表视图添加代码!
  • Dart's List 有一个名为add 的方法,您可以从中简单地添加项目!例如:variantsList.add('B')

标签: flutter dart


【解决方案1】:

根据我的理解,你的意思是删除同类列表数据? 你有两种方法:-

  1. 如果发现使用,请在添加到列表之前检查数据:-
if(!variantsList.contains('B'))
{//not found add data to list
variantsList.add('B');
}

2.使用最终结果toSet,然后toList

  variantsList = variantsList.toSet().toList();

希望能帮到你。

【讨论】:

  • @Ammer Amjed 我的问题是在列表中添加多个项目时,最近添加的项目会覆盖列表中的所有索引
猜你喜欢
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-06-20
  • 2021-05-03
  • 2021-07-14
  • 2020-12-06
  • 2018-09-06
  • 1970-01-01
相关资源
最近更新 更多