Skip to content

Defining updating theme causes lot of rebuilds #89127

@hbali

Description

@hbali

If you define textButtonTheme with TextButton.styleFrom in the MaterialApp's theme property, there will be a lot of unnecessary (?) rebuilds.
Also Theme.of(context) must be used to reproduce this bug.

You can see on the attached pictures that adding TextButton.styleFrom(primary: Colors.red), will cause 12 times more rebuilds than with the code without it.

If I remove Theme.of(context) from the Scaffold then it only gets rebuilt once.

note: I forced a rebuild with cmd+s (hot reload). In the actual application, I'm using a BlocBuilder and this causes the initial rebuild.

I know that flutter can call rebuild whenever it wants and I should calculate with it but rebuilding it 12 times is obviously noticeable (there is a little flickering in the elements in my actual app)

Steps to Reproduce

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        theme: ThemeData(
            textButtonTheme: TextButtonThemeData(
          style: TextButton.styleFrom(),
        )),
        debugShowCheckedModeBanner: false,
        home: Home());
  }
}

class Home extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    print('Home build');
    return Scaffold(
      backgroundColor: Theme.of(context).primaryColor,
    );
  }
}

channel: stable
version: 2.2.3
macOS 11.5.2
simulator: iPhone 8 (iOS 14.5)

Expected results:

image

Actual results:

image

Logs
info • Avoid `print` calls in production code • example/lib/main.dart:21:5 • avoid_print
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.2.3, on macOS 11.5.2 20G95 darwin-x64, locale en-JP)
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 3.5)
[✓] VS Code (version 1.59.1)
[✓] Connected device (2 available)

• No issues found!

Metadata

Metadata

Assignees

Labels

Bot is counting down the days until it unassigns the issuef: material designflutter/packages/flutter/material repository.found in release: 2.2Found to occur in 2.2found in release: 2.6Found to occur in 2.6frameworkflutter/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

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions