thecode
thecode

Reputation: 87

Can I add more conditions to my list view builder?

I have a ListView builder in Flutter. I use it as a search function. If a item in a list contains the letters in the input field it builds the item with the specific index. Everything works fine. The problem is that I want that the User can find one item with multiple key words. Therefore i thought it would be nice to add some more conditions. If the first keyword list search was not correct, look in the next list if the keyword is there. The index in the different Lists refers to the same Object. How can I add this condition. I tried it with copy and past but after the first condition AS just oversees the second condition.

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

} }

Upvotes: 1

Views: 6954

Answers (1)

chunhunghan
chunhunghan

Reputation: 54365

You do not have to do it in one return, you can separate your complex condition with if and multiple return like this or extract it to a function

code snippet

itemBuilder: (context, index) {

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

          },

full code

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

Upvotes: 3

Related Questions