Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
343 views
in Technique[技术] by (71.8m points)

dart - Flutter list items in mixed layout for each two items

I want to show the list items in mixed layout, where the first two items will be in full-width Card layout and the next two items will be in Grid layout.

so far I have tried to divide the items by separating even, add numbers, but am stuck at the next condition, am not sure whether it's a good approach to achieve this.

any suggestion would be appreciated.

this is what i need to achieve

ListView.builder(
            key: PageStorageKey("topNewsListKey"),
            itemCount: _newsList.length + (_hasMore ? 1 : 0),
            itemBuilder: (context, index) {
              if(index % 2 == 0) {
                print('even numbers $index');
                if( ) {  // condition to divide index 0,4,8...
                   return fullWidthCardLayout();
                } else {
                  return gridLayout();
                }
              } else {
                print('odd numbers $index');
                if() {  // condition to divide index 1,5,9...
                  return fullWidthCardLayout();
                } else {
                  return gridLayout();
                }
              }
            });

question from:https://stackoverflow.com/questions/65949171/flutter-list-items-in-mixed-layout-for-each-two-items

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

use ListView.separated and build the grid in separator builder with check the index if you want only to repeat after 2 or three widgets.

  ListView.separated(
      itemCount: 25,
      separatorBuilder: (BuildContext context, int index){
        if (index.isEven) {
          return YourGridWidget();
        } },                   
        itemBuilder: (BuildContext context, int index) { 
        return YourListTileWidget(); },
   )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...