Search code examples
flutterdartpackagedropdowncode-snippets

Drop down widget with detail showing on pressing this dropdown in flutter


How can i make a drop down widget like showing in picture.

enter image description here

On pressing the drop down button, we have to show details related to that button only. Can any one share some repository/dart package or code snippet to achieve that in flutter


Solution

  • You can copy paste run full code below
    You can use package https://pub.dev/packages/expandable
    With controller, you can manually set expanded to false or true
    So you can achieve collapse others

    code snippet

    List<ExpandableController> controllerList = [
      ExpandableController(),
      ExpandableController(),
      ExpandableController()
    ];
    
     ExpandablePanel(
                controller: controllerList[2],
    
     for (int i = 0; i < controllerList.length; i++) {
          if (i == currentIndex) {
            controllerList[i].expanded = true;
          } else {
            controllerList[i].expanded = false;
          }
        }
    
     ScrollOnExpand(
          scrollOnExpand: true,
          scrollOnCollapse: false,
          child: ExpandablePanel(
            theme: const ExpandableThemeData(
              ...
            header: Container(
              color: Colors.blue,
            ...
            collapsed: Container(),
            expanded: Container(
              color: Colors.white,
              child: Padding(
                padding: EdgeInsets.all(10),
                child: Column(
    

    working demo

    enter image description here

    full code

    import 'package:expandable/expandable.dart';
    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    import 'dart:math' as math;
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Expandable Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      State createState() {
        return MyHomePageState();
      }
    }
    
    class MyHomePageState extends State<MyHomePage> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("Expandable Demo"),
          ),
          body: ExpandableTheme(
            data: const ExpandableThemeData(
              iconColor: Colors.blue,
              useInkWell: true,
            ),
            child: ListView(
              physics: const BouncingScrollPhysics(),
              children: <Widget>[
                Card1(),
                Card2(),
                Card3(),
              ],
            ),
          ),
        );
      }
    }
    
    const loremIpsum =
        "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
    
    List<ExpandableController> controllerList = [
      ExpandableController(),
      ExpandableController(),
      ExpandableController()
    ];
    
    int currentIndex = -1;
    
    class Card1 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ExpandableNotifier(
            child: Padding(
          padding: const EdgeInsets.all(10),
          child: Card(
            color: Colors.blue,
            clipBehavior: Clip.antiAlias,
            child: Column(
              children: <Widget>[
                ScrollOnExpand(
                  scrollOnExpand: true,
                  scrollOnCollapse: false,
                  child: ExpandablePanel(
                    controller: controllerList[0],
                    theme: const ExpandableThemeData(
                      iconColor: Colors.white,
                      headerAlignment: ExpandablePanelHeaderAlignment.center,
                      tapBodyToCollapse: true,
                    ),
                    header: InkWell(
                      onTap: () {
                        currentIndex = 0;
                        for (int i = 0; i < controllerList.length; i++) {
                          if (i == currentIndex) {
                            controllerList[i].expanded = true;
                          } else {
                            controllerList[i].expanded = false;
                          }
                        }
                      },
                      child: Container(
                        color: Colors.blue,
                        child: Padding(
                            padding: EdgeInsets.all(10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Expanded(
                                    flex: 1,
                                    child: Icon(
                                      Icons.calendar_today,
                                      color: Colors.white,
                                    )),
                                Expanded(flex: 1, child: Container()),
                                Expanded(
                                  flex: 4,
                                  child: Text(
                                    "15/06/2020",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ],
                            )),
                      ),
                    ),
                    collapsed: Container(),
                    expanded: Container(
                      color: Colors.white,
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Bill Date",
                                    style: TextStyle(color: Colors.blue)),
                                Text("15/05/2020",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "Adjustment",
                                  style: TextStyle(color: Colors.blue),
                                ),
                                Text(".00", style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Total due",
                                    style: TextStyle(color: Colors.blue)),
                                Text("413.27",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    builder: (_, collapsed, expanded) {
                      return Expandable(
                        collapsed: collapsed,
                        expanded: expanded,
                        theme: const ExpandableThemeData(crossFadePoint: 0),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ));
      }
    }
    
    class Card2 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ExpandableNotifier(
            child: Padding(
          padding: const EdgeInsets.all(10),
          child: Card(
            color: Colors.blue,
            clipBehavior: Clip.antiAlias,
            child: Column(
              children: <Widget>[
                ScrollOnExpand(
                  scrollOnExpand: true,
                  scrollOnCollapse: false,
                  child: ExpandablePanel(
                    controller: controllerList[1],
                    theme: const ExpandableThemeData(
                      iconColor: Colors.white,
                      headerAlignment: ExpandablePanelHeaderAlignment.center,
                      tapBodyToCollapse: true,
                    ),
                    header: InkWell(
                      onTap: () {
                        currentIndex = 1;
                        for (int i = 0; i < controllerList.length; i++) {
                          if (i == currentIndex) {
                            controllerList[i].expanded = true;
                          } else {
                            controllerList[i].expanded = false;
                          }
                        }
                      },
                      child: Container(
                        color: Colors.blue,
                        child: Padding(
                            padding: EdgeInsets.all(10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Expanded(
                                    flex: 1,
                                    child: Icon(
                                      Icons.calendar_today,
                                      color: Colors.white,
                                    )),
                                Expanded(flex: 1, child: Container()),
                                Expanded(
                                  flex: 4,
                                  child: Text(
                                    "15/05/2020",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ],
                            )),
                      ),
                    ),
                    collapsed: Container(),
                    expanded: Container(
                      color: Colors.white,
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Bill Date",
                                    style: TextStyle(color: Colors.blue)),
                                Text("15/06/2020",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "Adjustment",
                                  style: TextStyle(color: Colors.blue),
                                ),
                                Text(".00", style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Total due",
                                    style: TextStyle(color: Colors.blue)),
                                Text("413.27",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    builder: (_, collapsed, expanded) {
                      return Expandable(
                        collapsed: collapsed,
                        expanded: expanded,
                        theme: const ExpandableThemeData(crossFadePoint: 0),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ));
      }
    }
    
    class Card3 extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return ExpandableNotifier(
            child: Padding(
          padding: const EdgeInsets.all(10),
          child: Card(
            color: Colors.blue,
            clipBehavior: Clip.antiAlias,
            child: Column(
              children: <Widget>[
                ScrollOnExpand(
                  scrollOnExpand: true,
                  scrollOnCollapse: false,
                  child: ExpandablePanel(
                    controller: controllerList[2],
                    theme: const ExpandableThemeData(
                      iconColor: Colors.white,
                      headerAlignment: ExpandablePanelHeaderAlignment.center,
                      tapBodyToCollapse: true,
                    ),
                    header: InkWell(
                      onTap: () {
                        currentIndex = 2;
                        for (int i = 0; i < controllerList.length; i++) {
                          if(i == currentIndex) {
                            controllerList[i].expanded = true;
                          } else {
                            controllerList[i].expanded = false;
                          }
                        }
                      },
                      child: Container(
                        color: Colors.blue,
                        child: Padding(
                            padding: EdgeInsets.all(10),
                            child: Row(
                              mainAxisAlignment: MainAxisAlignment.start,
                              children: [
                                Expanded(
                                    flex: 1,
                                    child: Icon(
                                      Icons.calendar_today,
                                      color: Colors.white,
                                    )),
                                Expanded(flex: 1, child: Container()),
                                Expanded(
                                  flex: 4,
                                  child: Text(
                                    "14/04/2020",
                                    style: TextStyle(color: Colors.white),
                                  ),
                                ),
                              ],
                            )),
                      ),
                    ),
                    collapsed: Container(),
                    expanded: Container(
                      color: Colors.white,
                      child: Padding(
                        padding: EdgeInsets.all(10),
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          children: <Widget>[
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Bill Date",
                                    style: TextStyle(color: Colors.blue)),
                                Text("15/05/2020",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text(
                                  "Adjustment",
                                  style: TextStyle(color: Colors.blue),
                                ),
                                Text(".00", style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                            Divider(
                              color: Colors.blue,
                              thickness: 2.0,
                            ),
                            Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                Text("Total due",
                                    style: TextStyle(color: Colors.blue)),
                                Text("413.27",
                                    style: TextStyle(color: Colors.blue)),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                    builder: (_, collapsed, expanded) {
                      return Expandable(
                        collapsed: collapsed,
                        expanded: expanded,
                        theme: const ExpandableThemeData(crossFadePoint: 0),
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
        ));
      }
    }