Home > Blockchain >  Don't show the first two element of the list in flutter
Don't show the first two element of the list in flutter

Time:09-17

In my flutter app I have a Listview. In that Listview I dont want to display the first two item. Is there a way to do that? Here is my code:

ListView.builder(
                        shrinkWrap: true,
                        itemCount: toReturnTheme.length,
                        itemBuilder: (context, int index) {
                          return Container(
                            width: 300.0,
                            child: Column(
                              children: [
                                ListTile(
                                  title: GestureDetector(
                                    child: Text("Theme $index",style: TextStyle(
                                      fontSize: 22.0,
                                    color: Theme.of(context).accentColor),),
                                    onTap: () {
                                      getThemeManager(context).selectThemeAtIndex(index);
                                    },
                                  ),
                                  trailing: GestureDetector(
                                          child: Icon(Icons.delete,
                                            color: Theme.of(context).accentColor,),
                                          onTap: () {
                                            toReturnTheme.removeAt(index);
                                            setState(() {});
                                          },
                                        ),
                                  leading : Icon(Icons.palette_outlined,
                                    color: Theme.of(context).accentColor,),
                                ),
                                Divider(
                                  color: Colors.white,
                                )
                              ],
                            ),
                          );
                        }),

CodePudding user response:

You can just add an if condition and return an empty Container like

ListView.builder(
                        shrinkWrap: true,
                        itemCount: toReturnTheme.length,
                        itemBuilder: (context, int index) {
if (index < 2) return Container();
                          return Container(
                            width: 300.0,
                            child: Column(
                              children: [
                                ListTile(
                                  title: GestureDetector(
                                    child: Text("Theme $index",style: TextStyle(
                                      fontSize: 22.0,
                                    color: Theme.of(context).accentColor),),
                                    onTap: () {
                                      getThemeManager(context).selectThemeAtIndex(index);
                                    },
                                  ),
                                  trailing: GestureDetector(
                                          child: Icon(Icons.delete,
                                            color: Theme.of(context).accentColor,),
                                          onTap: () {
                                            toReturnTheme.removeAt(index);
                                            setState(() {});
                                          },
                                        ),
                                  leading : Icon(Icons.palette_outlined,
                                    color: Theme.of(context).accentColor,),
                                ),
                                Divider(
                                  color: Colors.white,
                                )
                              ],
                            ),
                          );
                        }),
  • Related