Skip to content

ExpansionTile properties aren't updated with setState #24493

@pzierahn

Description

@pzierahn

Steps to Reproduce

  1. Create a new ExpansionTile and set a backgroundColor.
  2. Change the ExpansionTile's backgroundColor with setState.
import 'dart:math';
import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: MyHomePage());
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  /// Background color for the ExpansionTile
  Color _backgroundColor = randomColor();

  /// Generate random color for the background
  static Color randomColor() => new Color(new Random().nextInt(0xffffffff));

  /// Change event from the ExpansionTile
  void _onExpansion(bool value) {
    /// Change background color. The ExpansionTile doesn't change to the new
    /// _backgroundColor value. The Text element does.
    setState(() {
      _backgroundColor = randomColor();
    });

    print("_onExpansion: value=" + value.toString());
    print("_onExpansion: _backgroundColor=" + _backgroundColor.toString());
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> children = <Widget>[
      /// This view updates to _backgroundColor.
      Text("_backgroundColor=" + _backgroundColor.toString(),
          style: TextStyle(
            color: _backgroundColor,
          )),

      /// The ExpansionTile doesn't change to the new color value.
      ExpansionTile(
        title: Text("_backgroundColor=" + _backgroundColor.toString(),
            style: TextStyle(color: Color(0xff000000))),

        /// backgroundColor: This value will not be updated...
        backgroundColor: _backgroundColor,
        onExpansionChanged: _onExpansion,
        children: <Widget>[
          Text("Text 1"),
          Text("Text 2"),
          Text("Text 3"),
        ],
      )
    ];

    return Scaffold(
        appBar: AppBar(title: Text('Flutter Demo Home Page')),
        body: Center(
            child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: children,
        )));
  }
}

Logs

A log file is attached.
log.txt

flutter doctor -v

[✓] Flutter (Channel beta, v0.11.3, on Mac OS X 10.14.1 18B75, locale en-GB)
    • Flutter version 0.11.3 at /Users/patrick/Tools/flutter
    • Framework revision 72bf075e8d (8 days ago), 2018-11-09 20:36:17 -0800
    • Engine revision 5646e86a6f
    • Dart version 2.1.0 (build 2.1.0-dev.9.3 9c07fb64c4)

[!] Android toolchain - develop for Android devices (Android SDK 28.0.3)
    • Android SDK at /Users/patrick/Library/Android/sdk/
    • Android NDK at /Users/patrick/Library/Android/sdk/ndk-bundle
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /Users/patrick/Library/Android/sdk/
    • Java binary at: /Applications/Android Studio 3.3 Preview.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1248-b01)
    ✗ Android license status unknown.

[✓] iOS toolchain - develop for iOS devices (Xcode 10.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 10.1, Build version 10B61
    • ios-deploy 2.0.0
    • CocoaPods version 1.5.3

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listf: material designflutter/packages/flutter/material repository.found in release: 3.10Found to occur in 3.10found 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 onr: fixedIssue is closed as already fixed in a newer versionteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

Status

Done (PR merged)

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions