Skip to content

TextField style property is not material state aware #132212

@slaci

Description

@slaci

Is there an existing issue for this?

Steps to reproduce

Pass a MaterialStateTextStyle to TextField's style. For example by using MaterialStateTextStyle.resolveWith((states) {});

There was a PR which attempted to create support for this, but that only solves this behavior for the current theme, not the style parameter.

Some findings:

  • In Line 1244 the provided theme is merged into some defaults (without first resolving), which already removes the capability.
  • Then the _getInputStyleForState() method receives the already merged wrong style. It should receive widget.style.
  • At the end the providedStyle.merge(stateStyle); call just overwrites colors anyway. It should be the other way around, isn't it? As stateStyle (the theme styles) have colors, they will override providedStyle's colors. So I think it should be stateStyle.merge(providedStyle).

Expected results

The text in the input should be red.

This kind of styling works for all InputDecoration text styles and based on the code looks like it should work here too.

Actual results

The text in the input is in the theme's default provided color.

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const Scaffold(body: SafeArea(child: TextFieldContainer())),
    );
  }
}

class TextFieldContainer extends StatefulWidget {
  const TextFieldContainer({super.key});

  @override
  State<TextFieldContainer> createState() => _TextFieldContainerState();
}

class _TextFieldContainerState extends State<TextFieldContainer> {
  final _controller = TextEditingController(text: 'This text should be red');

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      controller: _controller,
      enabled: false,
      style: MaterialStateTextStyle.resolveWith((states) {
        if (states.contains(MaterialState.disabled)) {
          return const TextStyle(color: Colors.red);
        }
        return const TextStyle(color: Colors.blue);
      }),
    );
  }
}

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.10.6, on Microsoft Windows [Version 10.0.19045.3208], locale hu-HU)
    • Flutter version 3.10.6 on channel stable at C:\Users\Dell\.puro\envs\stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision f468f3366c (4 weeks ago), 2023-07-12 15:19:05 -0700
    • Engine revision cdbeda788a
    • Dart version 3.0.6
    • DevTools version 2.23.1

[✓] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at D:\android\SDK
    • Platform android-34, build-tools 34.0.0
    • ANDROID_SDK_ROOT = D:\android\SDK
    • Java binary at: C:\Users\Dell\AppData\Local\Programs\Android Studio\jbr\bin\java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-10027231)
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[✓] Chrome - develop for the web
    • Chrome at C:\Program Files (x86)\Google\Chrome\Application\chrome.exe

[✓] Visual Studio - develop for Windows (Visual Studio Build Tools 2022 17.5.4)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools
    • Visual Studio Build Tools 2022 version 17.5.33530.505
    • Windows 10 SDK version 10.0.19041.0

[✓] Android Studio (version 2022.3)
    • Android Studio at C:\Users\Dell\AppData\Local\Programs\Android Studio
    • 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 17.0.6+0-b2043.56-10027231)

[✓] VS Code (version 1.80.2)
    • VS Code at C:\Users\Dell\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.70.0

[✓] Connected device (4 available)
    • sdk gphone x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Windows (desktop)          • windows       • windows-x64    • Microsoft Windows [Version 10.0.19045.3208]
    • Chrome (web)               • chrome        • web-javascript • Google Chrome 115.0.5790.170
    • Edge (web)                 • edge          • web-javascript • Microsoft Edge 115.0.1901.188

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Metadata

Metadata

Labels

a: text inputEntering text in a text field or keyboard related problemsf: material designflutter/packages/flutter/material repository.found in release: 3.10Found to occur in 3.10found in release: 3.13Found to occur in 3.13frameworkflutter/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 team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions