flutter - ListView 中的Flutter组合框项目

标签 flutter dart mobile combobox

我试图制作一个组合框,当我选择一个项目时,我希望在按下按钮时将其添加到列表 View 中。
我尝试了多种解决方案,但似乎没有一个可行的方法,就我所做的而言,我可以创建一个输入字段并手动添加项目,但是我不能使用组合框来完成此操作(不让用户手动编写)。
这是我所做的:



import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:searchable_dropdown/searchable_dropdown.dart';


class LocationTab extends StatefulWidget {
  @override
  _LocationTabState createState() => _LocationTabState();
}

class _LocationTabState extends State<LocationTab> {
  List<String> mylist = List();


  List<ParentCategoryComboBox> _visibility =
      ParentCategoryComboBox.getParentCategory();
  List<DropdownMenuItem<ParentCategoryComboBox>> _dropdownMenuItems;

  List<PlatformReachComboBox> _platformReach =
      PlatformReachComboBox.getPlatformReach();
  List<DropdownMenuItem<PlatformReachComboBox>> _dropdownPlatformReach;

  PlatformReachComboBox _selectedPlatformReach;

  ParentCategoryComboBox _selectedVisibility;

  @override
  void initState() {
    _dropdownMenuItems = buildDropDownMenuItems(_visibility);
    _selectedVisibility = _dropdownMenuItems[0].value;

    _dropdownPlatformReach =
        buildDropdownMenuItemsPlatformReach(_platformReach);
    _selectedPlatformReach = _dropdownPlatformReach[0].value;

    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: EdgeInsets.only(top: 10.0, left: 20.0, right: 20.0),
      child: SingleChildScrollView(
        physics: BouncingScrollPhysics(),
        scrollDirection: Axis.vertical,
        child: Column(
          children: <Widget>[
            SizedBox(
              height: 20,
            ),
            Column(
              children: [
                Align(
                  alignment: Alignment.centerLeft,
                  child: Text(
                    "Select visibility:",
                    style: TextStyle(
                      color: Colors.indigoAccent,
                      fontWeight: FontWeight.bold,
                      fontSize: 16,
                    ),
                  ),
                ),
              ],
            ),
            DropdownButton(
              icon: Icon(
                Icons.visibility,
                color: Colors.black45,
              ),
              isExpanded: true,
              value: _selectedVisibility,
              items: _dropdownMenuItems,
              onChanged: (ParentCategoryComboBox selectedVisibility) {
                setState(() {
                  _selectedVisibility = selectedVisibility;
                });
              },
            ),
            SizedBox(
              height: 35,
            ),
            Container(
              padding: new EdgeInsets.symmetric(horizontal: 5.0),
              decoration: BoxDecoration(
                  border: Border.all(
                    width: 1,
                    color: Colors.grey,
                  ),
                  borderRadius: BorderRadius.circular((10))),
              child: Column(
                children: <Widget>[
                  SizedBox(
                    height: 10,
                  ),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "Select platform reach:",
                      style: TextStyle(
                        color: Colors.indigoAccent,
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: SearchableDropdown(
                          isExpanded: true,
                          value: _selectedPlatformReach,
                          items: _dropdownPlatformReach,
                          isCaseSensitiveSearch: false,
                          onChanged:
                              (PlatformReachComboBox selectedPlatformReach) {
                            setState(() {
                              _selectedPlatformReach = selectedPlatformReach;
                            });
                          },
                        ),
                        flex: 2,
                      ),
                      Expanded(
                        child: Container(
                          height: 50.0,
                          width: 80,
                          child: Material(
                            borderRadius: BorderRadius.circular(20.0),
                            shadowColor: Colors.blue,
                            color: Colors.deepPurpleAccent,
                            elevation: 0.0,
                            child: GestureDetector(
                              onTap: () {
                                setState(() {
                                  mylist.add(_selectedPlatformReach.toString());
                                });
                              },
                              child: Center(
                                child: Text(
                                  'ADD',
                                  style: TextStyle(
                                    color: Colors.white,
                                    fontWeight: FontWeight.bold,
                                  ),
                                ),
                              ),
                            ),
                          ),
                        ),
                        flex: 0,
                      ),
                    ],
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  Align(
                    alignment: Alignment.centerLeft,
                    child: Text(
                      "Added:",
                      style: TextStyle(
                        color: Colors.indigoAccent,
                        fontWeight: FontWeight.bold,
                        fontSize: 16,
                      ),
                    ),
                  ),
                  SizedBox(
                    height: 10,
                  ),
                  ListView.builder(
                    padding: EdgeInsets.only(left: 10),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: mylist.length,
                    itemBuilder: (BuildContext ctxt, int Index) {
                      return Text(
                          mylist[Index]
                      );

                    },
                  ),
//                  ListView.builder(
//                    padding: EdgeInsets.only(left: 10),
//                    scrollDirection: Axis.vertical,
//                    shrinkWrap: true,
//                    itemCount: _platformReach.length,
//                    itemBuilder: (BuildContext ctxt, int Index) {
//                      return Text(
//                        "Galati"
//                      );
////                      return DropdownButton(
////                        value: _selectedPlatformReach.toString(),
////                        items: _dropdownPlatformReach,
////                        onChanged: (value) {
////                          _selectedPlatformReach = value;
////                          setState(() {});
////                        },
////                        hint: Text('Select drowdown'),
////                      );
////                                return new Text(_dropdownPlatformReach[Index].value);
//                    },
//                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

模拟数据类:
import 'package:flutter/material.dart';

class PlatformReachComboBox {
  String name;
  String hint;

  PlatformReachComboBox(this.name, this.hint);

  static List<PlatformReachComboBox> getPlatformReach() {
    return <PlatformReachComboBox>[
      PlatformReachComboBox('Jud Galati', '(RO, [Galati] County)'),
      PlatformReachComboBox('Jud Braila', '(RO, [Braila] County)'),
      PlatformReachComboBox('Jud Prahova', '(RO, [Ploiesti] County)'),
      PlatformReachComboBox('Jud Maramures', '(RO, [Baia Mare] County)'),
    ];
  }
}

List<DropdownMenuItem<PlatformReachComboBox>>
    buildDropdownMenuItemsPlatformReach(List platforms) {
  List<DropdownMenuItem<PlatformReachComboBox>> items = List();
  for (PlatformReachComboBox platform in platforms) {
    items.add(
      DropdownMenuItem(
        value: platform,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Text(
              platform.name,
              style: TextStyle(fontWeight: FontWeight.bold),
              textAlign: TextAlign.start,
            ),
            Text(
              platform.hint,
              style:
                  TextStyle(fontWeight: FontWeight.normal, color: Colors.grey),
              textAlign: TextAlign.end,
            )
          ],
        ),
      ),
    );
  }
  return items;
}
这就是我遇到问题的地方,我不知道要按下按钮,然后在其中添加的项目,对于ListView我将返回什么:
   ListView.builder(
                    padding: EdgeInsets.only(left: 10),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    itemCount: _platformReach.length,
                    itemBuilder: (BuildContext ctxt, int Index) {
                      return Text(
                        "Galati"
                      );

                    },
                  ),

最佳答案

请尝试这种方式...定义一个列表

List<String> mylist = List();
并在下面使用
   ListView.builder(
                padding: EdgeInsets.only(left: 10),
                scrollDirection: Axis.vertical,
                shrinkWrap: true,
                itemCount: mylist.length,
                itemBuilder: (BuildContext ctxt, int Index) {
                  return Text(
                    mylist[Index]
                  );

                },
              ),
和之后按钮单击
setState(() {
   mylist.add(_selectedVisibility.name);
});

关于flutter - ListView 中的Flutter组合框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63950506/

相关文章:

flutter - 如何在 flutter 中从图库中获取图像或视频?

android - 如何在 Flutter 的 TextFormField 中添加提示?

mobile - 在 AppCenter 中构建使用 Carthage 的应用程序

android - 在Android Eclipse项目中导入OpenCV

flutter - 如何在 FLUTTER 中几秒钟后重定向到 Stream Builder 中的另一个屏幕/页面?

ios - Collection View 完成处理程序的 PerformBatchUpdates 未执行

list - flutter/Dart : How to get list value where key equals

firebase - flutter 依赖 firebase_analytics 0.0.4 需要 SDK 版本 >=1.8.0 <2.0.0,版本解决失败

json - 如何在 dart 中映射动态 json api 响应

dart - 如何减少 ListView.builder 中 RaisedButton 的宽度?