如何在Listview.Builder中添加底部一些值?

时间:2019-05-14 10:40:43

标签: dart flutter flutter-layout

我需要在最后添加 General 。我该怎么做? 我向数据库添加了新列。列名称为 sortOrder ,其他所有值 SortOrder 为空,不包含常规 SortOrder 的值仅在常规中为1。 我的API json是这样的,

  

{“ deF_CAT_ID”:140,               “ description”:“ General”,               “使用中”:1               “ sortOrder”:1}

  Widget createListView(BuildContext context, AsyncSnapshot snapshot) {
    List<CategoryItem> values = snapshot.data;
    values.sort((a,b) => a.description.toLowerCase().compareTo(b.description.toLowerCase()));

    int count = 1;
    return Container(
      color: const Color(0xFFFFFFFF),
      child: ListView.builder(
        padding: EdgeInsets.only(top: 8.0, right: 0.0, left: 0.0),
        itemCount: count,
        itemBuilder: (BuildContext context, int index) {
          return GridView.count(
            physics: ScrollPhysics(),
            shrinkWrap: true,
            crossAxisCount: 4,
            children: List.generate(values.length, (index) {
              return GridTile(
                child: GestureDetector(
                  onTap: () => sub(values[index].childId),
                  child: Column(
                    children: [
                      SvgPicture.asset(
                        'assets/images/Defect/icon-${values[index].childId}.svg',
                        height: 50.0,
                       color: Colors.blue,
                      ),
                      Expanded(
                        child: Text(
                          values[index].description,
                          textAlign: TextAlign.center,
                          style: TextStyle(
                              fontSize: 10.0
                          ),),),],),),);}),);},),);}

enter image description here

1 个答案:

答案 0 :(得分:0)

这个怎么样?如果您有多个具有非null sortOrder的值,则它们将按字母顺序排序。

  values.sort((a, b) {
    if (a.sortOrder == null && b.sortOrder != null) {
      return -1;
    } else if (a.sortOrder != null && b.sortOrder == null) {
      return 1;
    } else {
      return a.description.toLowerCase().compareTo(b.description.toLowerCase());
    }
  });