我可以向列表视图构建器添加更多条件吗?

时间:2019-09-04 16:06:05

标签: listview flutter filter

我在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();
          }))
]));

} }

1 个答案:

答案 0 :(得分:0)

您不必一次返回,可以使用if和多次返回来分离复杂的条件,或者将其提取到函数中

代码段

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]}'),
              );
            }

          },
        ),
      ),
    );
  }
}

enter image description here