-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Description
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:
Actual results:
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!

