Skip to content

ExpansionPanelCallback shows incorrect state while opening and closing ExpansionPanelList. #74114

Closed

Description

@kuyaC

Hello, I might have found a bug/error in the framework. I describe how the actual results differ from the ones I expected.
(copied from where I posted this first https://stackoverflow.com/questions/65743515/flutter-expansionpanellist-radio-callback-returns-wrong-value)


The line I marked with the comment (in CAPS) doesn't do what it's expected to do.

console output when I toggle a certain ExpansionPanelRadio() (of language english for example):

  • I open the ExpansionPanelRadio(): flutter: english false (<-- should be true, right?)
  • I close the ExpansionPanelRadio(): flutter: english true (<-- should be false, right?)

console output when language english is selected, and I click another one (for example mandarin):

  1. flutter: mandarin false (<--this one went from close to open, shouldn't it return true??)
  2. flutter: english false (<--this one went from open to close)

Why do they both say false, when obviously one opened and the other one closed? Both panels were in different states before, and also after the change.

With this information^^, HOW am I supposed to know WHICH ExpansionPanelRadio() just got tapped?

            List<String> languages = Languages.languageToImagePath.keys.toList();

            return SingleChildScrollView(
              child: ExpansionPanelList.radio(
                initialOpenPanelValue: targetLanguage, //see value in ExpansionPanelRadio
                expansionCallback: (int panelIndex, bool isExpanded) {
                  print('${languages[panelIndex]} $isExpanded'); //<-- HEEEEEEERRREEEEE
                },
                children: languages.map<ExpansionPanel>((language) {
                  return ExpansionPanelRadio(
                    value: language,
                    headerBuilder: (context, bool isExpanded) { //and what's this bool for?
                      return Text(language);
                    },
                    body: Text('Choose writing system(s) todo...'),
                  );
                }).toList(),
              ),
            );

I think these might be errors/bugs in the framework, but I wanted to ask here on stackoverflow before reporting it.
Maybe I just don't understand how these Widgets are supposed to be used.
Any help is appreciated.

I first thought ExpansionPanelList.radio might be the perfect Widget for what I intend to do, but it looks like I just have to make my own custom Widget

Activity

  1. iapicca commented on Jan 16, 2021

    @iapicca

    @kuyaC
    I've tried to reproduce the behavior myself code sample here and I agree with you
    the docs for ExpansionPanelList state

    expansionCallback → ExpansionPanelCallback
    The callback that gets called whenever one of the expand/collapse buttons is pressed. The arguments passed to the callback are the index of the pressed panel and whether the panel is currently expanded or not. [...]

    which seems to make sense... except that it's inconsistent with other widgets
    for example CheckBox's onChange

    even the official code sample as this unusual approach

     setState(() {
              _data[index].isExpanded = !isExpanded;
            });

    more than a bug, I see this as afeature request to make this widget's behavior consistent with the framework

  2. darshankawar commented on Jan 18, 2021

    @darshankawar
    Member

    Opening and closing ExpansionPanelList is showing wrong state, ie, expanding the panel list returns false while closing it returns true:

    when clicked on, index was 0 expanded? false
    when clicked on, index was 0 expanded? true
    when clicked on, index was 1 expanded? false
    when clicked on, index was 1 expanded? true
    when clicked on, index was 2 expanded? false
    when clicked on, index was 2 expanded? true
    
    flutter doctor -v
    [✓] Flutter (Channel stable, 1.22.5, on Mac OS X 10.15.4 19E2269 darwin-x64,
        locale en-IN)
        • Flutter version 1.22.5 at /Users/dhs/documents/Fluttersdk/flutter
        • Framework revision 7891006299 (5 weeks ago), 2020-12-10 11:54:40 -0800
        • Engine revision ae90085a84
        • Dart version 2.10.4
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
        • Android SDK at /Users/dhs/Library/Android/sdk
        • Platform android-30, build-tools 30.0.0
        • Java binary at: /Applications/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6915495)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 12.3)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.9.3
    
    [!] Android Studio (version 4.1)
        • Android Studio at /Applications/Android Studio.app/Contents
        ✗ Flutter plugin not installed; this adds Flutter specific functionality.
        ✗ Dart plugin not installed; this adds Dart specific functionality.
        • Java version OpenJDK Runtime Environment (build
          1.8.0_242-release-1644-b3-6915495)
    
    [✓] VS Code (version 1.52.1)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.16.0
    
    [✓] Connected device (1 available)
        • SM A260G (mobile) • 5200763ebcfa861f • android-arm • Android 8.1.0 (API
          27)
    
    ! Doctor found issues in 1 category.
    
    
    
  3. changed the title [-]Possible bug/error in the framework? (ExpansionPanelList)[/-] [+]ExpansionPanelCallback shows incorrect state while opening and closing ExpansionPanelList.[/+] on Jan 18, 2021
  4. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    on Jan 18, 2021
  5. lodgmar commented on Jun 22, 2021

    @lodgmar

    I've suffered this bug too.
    Any news about fix?

  6. lodgmar commented on Jun 22, 2021

    @lodgmar

    I've found the bug in _ExpansionPanelListState._handlePressed(bool isExpanded, int index).

    It calls expansionCallback with the state of the panel BEFORE it opens/closes.

    But then, this is what it does:

          // If another ExpansionPanelRadio was already open, apply its
          // expansionCallback (if any) to false, because it's closing.

    But, this is the state of the second panel AFTER closing. This is inconsistent with the opening/closing panel that fired the event. So, I receive two calls to expansionCallback, both with expanded = false, but one for the panel opening, and one for the panel closing, imposible to know what is each.

    Posible solution: change first line of this method, from:

        widget.expansionCallback?.call(index, isExpanded);

    to:

        widget.expansionCallback?.call(index, !isExpanded);
  7. evanholt1 commented on Jun 24, 2021

    @evanholt1

    I am facing this issue as well.
    I am trying to use ExpansionPanelList.radio and make use of it's expansionCallback,
    but since clicking on a panel while another is open calls "expansionCallback" twice,
    once with the panel that's going to open with the boolean 'false', and another time with the panel that's to close with also the boolean 'false' as it's to close - which is inconsistent -,
    and thus it is impossible without managing external state for ExpansionCallback to know which is panel is to close and which is to open,
    and kind of defeats the purpose of .radio, as by using external state for this issue, .radio doesn't do anything useful.

  8. lodgmar commented on Jun 25, 2021

    @lodgmar

    The best behaviour would be to call expansionCallback for the closing panel FIRST, and THEN for the opening panel. This is the correct way to implement a "radio": two panels cannot be opened at the same time, one panel has to close BEFORE another one can open; and the last event is that one panel has been opened, not closed.

    So, more changes to do in _ExpansionPanelListState._handlePressed(bool isExpanded, int index).

  9. lodgmar commented on Jun 25, 2021

    @lodgmar

    This would do the trick:

      void _handlePressed(bool isExpanded, int index) {
        if (widget._allowOnlyOnePanelOpen) {
          final ExpansionPanelRadio pressedChild = widget.children[index] as ExpansionPanelRadio;
    
          // If another ExpansionPanelRadio was already open, apply its
          // expansionCallback (if any) to false, because it's closing.
          for (int childIndex = 0; childIndex < widget.children.length; childIndex += 1) {
            final ExpansionPanelRadio child = widget.children[childIndex] as ExpansionPanelRadio;
            if (widget.expansionCallback != null &&
              childIndex != index &&
              child.value == _currentOpenPanel?.value)
              widget.expansionCallback!(childIndex, false);
          }
    
          widget.expansionCallback?.call(index, !isExpanded);  // ***** LINE CHANGED AND MOVED
    
          setState(() {
            _currentOpenPanel = isExpanded ? null : pressedChild;
          });
        }
      }
  10. 86driver commented on Jul 2, 2021

    @86driver

    I've suffered this bug too.

  11. andreadaoud commented on Aug 13, 2022

    @andreadaoud

    Hello, any progress on this bug?

  12. maheshj01 commented on Jan 2, 2023

    @maheshj01
    Member

    Reproducible on stable and the master channel incorrect expansion status (isExpanded) is reported when closing and opening the ExpansionPanel.

    code sample
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      const MyHomePage({Key? key, required this.title}) : super(key: key);
      final String title;
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      String targetLanguage = 'English';
      List<String> languages = <String>['English', 'French', 'Spanish'];
      @override
      Widget build(BuildContext context) {
        return Center(
          child: SingleChildScrollView(
            child: ExpansionPanelList.radio(
              initialOpenPanelValue:
                  targetLanguage, //see value in ExpansionPanelRadio
              expansionCallback: (int panelIndex, bool isExpanded) {
                print(
                    '${languages[panelIndex]} $isExpanded'); //<-- HEEEEEEERRREEEEE
              },
              children: languages.map<ExpansionPanel>((language) {
                return ExpansionPanelRadio(
                  value: language,
                  headerBuilder: (context, bool isExpanded) {
                    //and what's this bool for?
                    return Text(language);
                  },
                  body: const Text('Choose writing system(s) todo...'),
                );
              }).toList(),
            ),
          ),
        );
      }
    }
    
    flutter doctor -v (mac)
    [✓] Flutter (Channel master, 3.7.0-3.0.pre.33, on macOS 12.6 21G115 darwin-arm64, locale en-IN)
        • Flutter version 3.7.0-3.0.pre.33 on channel master 
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 5201856805 (38 minutes ago), 2022-12-05 18:27:21 -0800
        • Engine revision a309d239c4
        • Dart version 2.19.0 (build 2.19.0-463.0.dev)
        • DevTools version 2.20.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly
          to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.55.20221129
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm64
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 108.0.5359.94
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
    [✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
        • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
        • Engine revision 8f2221fbef
        • Dart version 2.18.5
        • DevTools version 2.15.0
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
        • Android SDK at /Users/mahesh/Library/Android/sdk
        • Platform android-33, build-tools 33.0.0-rc4
        • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Build 14A400
        • CocoaPods version 1.11.3
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2021.2)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    
    [✓] IntelliJ IDEA Community Edition (version 2021.2.1)
        • IntelliJ at /Applications/IntelliJ IDEA CE.app
        • Flutter plugin version 61.2.4
        • Dart plugin version 212.5080.8
    
    [✓] VS Code (version 1.70.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.53.20221101
    
    [✓] Connected device (3 available)
        • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
          com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
        • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
        • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110
    
    [✓] HTTP Host Availability
        • All required HTTP hosts are available
    
    • No issues found!
    
  13. 2 remaining items

  14. robpot95 commented on Feb 17, 2023

    @robpot95

    2 years later and still having this issue

  15. andreadaoud commented on Feb 22, 2023

    @andreadaoud

    Hi, I think this is a very important issue, could you please fix it?

  16. dleyba042 commented on May 9, 2023

    @dleyba042
    Contributor

    @darshankawar , @maheshmnj , could I be assigned this issue? I could have a PR up pretty quickly.

  17. dleyba042 commented on May 9, 2023

    @dleyba042
    Contributor

    I made a PR if anyone can take a look. dleyba042#2

  18. dleyba042 commented on May 9, 2023

    @dleyba042
    Contributor

    Here's what the behavior looks like:

    demoExpansion

  19. lodgmar commented on Jun 10, 2023

    @lodgmar

    Thanks!!!!

  20. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jun 12, 2023
  21. alierdogan7 commented on Jun 22, 2023

    @alierdogan7

    Finally @dleyba042's fix was published in 3.12.0-7.0.pre. I changed my flutter version to it (despite being an unstable channel), and the problem has gone. Thanks.

  22. github-actions commented on Jul 6, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  23. locked as resolved and limited conversation to collaborators on Jul 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    found in release: 3.3Found to occur in 3.3found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer version

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions