Search code examples
flutterdartflutter-listview

Horizontal ListView builder end with one button using row


I need horizontal list view (ListView.builder) with scrollable and these list end with one button, I am using row but screen cannot able to scroll

I am newbie in flutter.

enter image description here

Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
                height: MediaQuery.of(context).size.height/2,
                child: ListView.builder(
                  physics: ClampingScrollPhysics(),
                  shrinkWrap: true,
                  scrollDirection: Axis.horizontal,
                  itemCount: newList!.content.length,
                  itemBuilder: (BuildContext context, int index) => Column(
                    children: [
                      Container(
                        padding: newList!.content.length == index
                            ? EdgeInsets.only(right: 13)
                            : 0 == index
                                ? EdgeInsets.only(left: 13, right: 13)
                                : EdgeInsets.only(right: 13),
                        child: Column(
                          children: [
                            Column(
                              children: [
                                ImageCard(
                                    contentItem: newList.content[index],
                                  
                                    isInINR: newList.isInINR)
                              ],
                            )
                          ],
                        ),
                      ),
                    ],
                  ),
                )),
          ],
        ),

Solution

  • Instead of adding new element in list you add the condition like below. If index is last then you can show the view all button. I hope you will get the answer. You can ask more if you have any queries.

    import 'package:flutter/material.dart';
    
    const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
    
    void main() {
    runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
    @override
    Widget build(BuildContext context) {
      return MaterialApp(
        theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
          ),
        ),
      );
     } 
    }
    class _MyWidgetState extends State<MyWidget> {
    bool displayAllList = false;
    int minimumCount = 4;
    
    @override
    Widget build(BuildContext context) {
    
    List list = List.generate(8, (index) => index).toList();
    return ListView.builder(
      scrollDirection: Axis.horizontal,
      itemBuilder: (BuildContext context, int index) {
        return Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            children: [
              const SizedBox(
                  width: 50, height: 50, child: ColoredBox(color: Colors.red)),
              if ((list.length > minimumCount && (index == minimumCount - 1)) && (list.length > 4 && displayAllList == false)) ...[
                TextButton(onPressed: () {
                  setState((){
                    displayAllList = true;
                  });
                }, child: Text('View All'))
              ]
            ],
          ),
        );
      },
      itemCount: (list.length > 4 && displayAllList == false) ? 4 : list.length,
    );
    }
    }