0

I have implement several no of expanded items using expansion widget.

These are the steps

  1. All the widgets are collapsed at the beginning
  2. First widget was expanded
  3. Second widget also expanded without collapse first one

I want to automatically collapse first one when expanding second one

     import 'package:expansion_widget/expansion_widget.dart';
     import 'package:flutter/material.dart';

     class CustomExpansionTile extends StatefulWidget {
     final Widget HeaderBody;
     final Widget ExpandedBody;
     final Color HeaderColor;
     final Color ExpandedBodyColor;
     final double Padding;

     const CustomExpansionTile({
     Key? key,
     required this.HeaderBody,
     required this.ExpandedBody,
     required this.HeaderColor,
     required this.ExpandedBodyColor,
     required this.Padding,
     }) : super(key: key);
     @override
    _CustomExpansionTileState createState() => _CustomExpansionTileState();
     }

     class _CustomExpansionTileState extends State<CustomExpansionTile> {
     @override
     Widget build(BuildContext context) {
     return Column(
     children: [
     Card(
      elevation: 0,
      child: ExpansionWidget(
        initiallyExpanded: false,
        titleBuilder:
            (double animationValue, _, bool isExpaned, toogleFunction) {
          return Container(
            decoration: BoxDecoration(
                color: widget.HeaderColor,
                borderRadius: BorderRadius.circular(6)),
            height: 59,
            child: InkWell(
                onTap: () {
                  toogleFunction(animated: true);
                },
                child: Padding(
                  padding: EdgeInsets.symmetric(horizontal: widget.Padding),
                  child: Row(
                    children: [
                      widget.HeaderBody,
                      const Spacer(),
                      Transform.rotate(
                        angle: 0,
                        child: Icon(
                            isExpaned
                                ? Icons.keyboard_arrow_down_rounded
                                : Icons.keyboard_arrow_right,
                            size: 40),
                        alignment: Alignment.centerRight,
                      )
                    ],
                  ),
                )),
          );
        },
        content: Container(
          color: widget.ExpandedBodyColor,
          width: double.infinity,
          padding: const EdgeInsets.all(20),
          child: Column(
            children: [widget.ExpandedBody],
          ),
          ),
          ),
          ),
          ],
           );
          }
          }

This is my Code for calling custom widget

  CustomExpansionTile(
                              HeaderBody: Row(
                                children: [
                                  Text('Hellooo'),
                                  Text('Hellooo'),
                                ],
                              ),
                              ExpandedBody: Column(
                                crossAxisAlignment:
                                    CrossAxisAlignment.start,
                                children: [
                                  Text('Hellooo'),
                                   Text('Hellooo'),                                  
                                ],
                              ),
                              HeaderColor: Colors.white,
                              ExpandedBodyColor: Colors.white,
                              Padding: 0,
                            ),

This is the screenshot I want to auto collapse

NavodDinidu
  • 383
  • 3
  • 6
  • Hey NavodDinidu, you can create a list of booleans for the number of expanded widgets and assign the initial expanded to this list's index. Call state change on each tap, this way you can toggle each widget's expansion – WBG Nov 11 '21 at 18:23

1 Answers1

0

To achieve that you can do one of the following:

1- [O(n): O(1)] create a new property called currentSelectedItem its type is as your data model type and onTap method change its value to your tapped item and do not forget to add the check to the expansion property of the expanded card as -> expanded: currentSelectedItem == itemModel,

2- [O(n): O(n)] add a new boolean property to your model item called "isExpanded" and config on tap action to loop throw all models list changing items' isExpanded to false and update current tapped item's isExpanded to true

Muhamad Jalal
  • 312
  • 2
  • 14