【问题标题】:Can I add more conditions to my list view builder?我可以向我的列表视图构建器添加更多条件吗?
【发布时间】:2019-09-04 16:06:05
【问题描述】:

我在 Flutter 中有一个 ListView 构建器。我将其用作搜索功能。如果列表中的项目包含输入字段中的字母,它会使用特定索引构建项目。一切正常。 问题是我希望用户可以找到具有多个关键字的项目。因此,我认为添加更多条件会很好。如果第一个关键字列表搜索不正确,请在下一个列表中查找关键字是否存在。不同列表中的索引引用同一个对象。我怎样才能添加这个条件。我用复制和过去尝试过,但在第一个条件之后,AS 只是监督第二个条件。

 Widget build(BuildContext context) {
return new Material(
    child: new Column(children: <Widget>[
  new Padding(
    padding: new EdgeInsets.only(top: 20.0),
  ),
  new Expanded(
      child: new ListView.builder(
          itemCount: shops.length,
          itemBuilder: (BuildContext context, int index) {
            return filter == null || filter == ""
                ? new Card(
                    child: ListTile(
                    leading: ConstrainedBox(
                      constraints: BoxConstraints(
                        minWidth: 115,
                        minHeight: 44,
                        maxWidth: 115,
                        maxHeight: 64,
                      ),
                      child: Image.asset(images[index], fit: BoxFit.cover),
                    ),
                    title: Text(shops[index]),
                    subtitle: Text(streets[index]),
                    trailing: Icon(Icons.more_vert),
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => BarberHome()),
                      );
                    },
                  ))
                : shops[index].toLowerCase().contains(filter.toLowerCase()) ? //the first condition
                     new Card(
                        child: ListTile(
                        leading: ConstrainedBox(
                          constraints: BoxConstraints(
                            minWidth: 115,
                            minHeight: 44,
                            maxWidth: 115,
                            maxHeight: 64,
                          ),
                          child:
                              Image.asset(images[index], fit: BoxFit.cover),
                        ),
                        title: Text(shops[index]),
                        subtitle: Text(streets[index]),
                        trailing: Icon(Icons.more_vert),
                        onTap: () {
                          Navigator.push(
                            context,
                            MaterialPageRoute(
                                builder: (context) => BarberHome()),
                          );
                        },
                      ))

// here i tried to insert the second condition like that

//: streets[index].toLowerCase().contains(filter.toLowerCase()) ?
// ....
                    : new Container();
          }))
]));

} }

【问题讨论】:

    标签: listview flutter filter


    【解决方案1】:

    您不必一次返回,您可以像这样将复杂条件与 if 和多次返回分开,或者将其提取到一个函数中

    代码sn-p

    itemBuilder: (context, index) {
    
                if(index.isEven){
                  return ListTile(
                    title: Text('even ${items[index]}'),
                  );
                } else {
                  return ListTile(
                    title: Text('odd ${items[index]}'),
                  );
                }
    
              },
    

    完整代码

    import 'package:flutter/foundation.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp(
        items: List<String>.generate(10000, (i) => "Item $i"),
      ));
    }
    
    class MyApp extends StatelessWidget {
      final List<String> items;
    
      MyApp({Key key, @required this.items}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        final title = 'Long List';
    
        return MaterialApp(
          title: title,
          home: Scaffold(
            appBar: AppBar(
              title: Text(title),
            ),
            body: ListView.builder(
              itemCount: items.length,
              itemBuilder: (context, index) {
    
                if(index.isEven){
                  return ListTile(
                    title: Text('even ${items[index]}'),
                  );
                } else {
                  return ListTile(
                    title: Text('odd ${items[index]}'),
                  );
                }
    
              },
            ),
          ),
        );
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-08-18
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 2017-05-07
      • 1970-01-01
      • 2021-02-17
      • 1970-01-01
      • 2011-03-29
      相关资源
      最近更新 更多