Fix InputDecoration.applyDefaults ignoring some properties (#129010)

fixes [`InputDecoration.applyDefaults` ignores some properties](https://github.com/flutter/flutter/issues/127330)

<details> 
<summary>code sample</summary> 

```dart
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(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(useMaterial3: true),
      home: const Example(),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    const TextStyle textStyle = TextStyle(color: Color(0xFF00FFFF));
    const Color color = Color(0xFF00FF00);
    const InputBorder inputBorder = OutlineInputBorder(
      borderSide: BorderSide(
        color: Color(0xFF0000FF),
      ),
    );

    final InputDecoration appliedDefaults =
        const InputDecoration().applyDefaults(
      const InputDecorationTheme(
        labelStyle: textStyle,
        floatingLabelStyle: textStyle,
        helperStyle: textStyle,
        helperMaxLines: 2,
        hintStyle: textStyle,
        errorStyle: textStyle,
        errorMaxLines: 2,
        floatingLabelBehavior: FloatingLabelBehavior.never,
        floatingLabelAlignment: FloatingLabelAlignment.center,
        isDense: true,
        contentPadding: EdgeInsets.all(1.0),
        iconColor: color,
        prefixStyle: textStyle,
        prefixIconColor: color,
        suffixStyle: textStyle,
        suffixIconColor: color,
        counterStyle: textStyle,
        filled: true,
        fillColor: color,
        focusColor: color,
        hoverColor: color,
        errorBorder: inputBorder,
        focusedBorder: inputBorder,
        focusedErrorBorder: inputBorder,
        disabledBorder: inputBorder,
        enabledBorder: inputBorder,
        border: InputBorder.none,
        alignLabelWithHint: true,
        constraints: BoxConstraints(
            minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: const Text('InputDecoration().applyDefaults'),
      ),
      // Centered FilledButton.
      body: Center(
        child: SingleChildScrollView(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ColoredBox(
                color: appliedDefaults.labelStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.labelStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.floatingLabelStyle
                        .toString()
                        .contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.floatingLabelStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.helperStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.helperStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.hintStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.hintStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.errorStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.errorStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.iconColor.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.iconColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.prefixStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.prefixStyle.toString()),
              ),
              ColoredBox(
                color:
                    appliedDefaults.prefixIconColor.toString().contains('null')
                        ? Colors.red
                        : Colors.green,
                child: Text(appliedDefaults.prefixIconColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.suffixStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.suffixStyle.toString()),
              ),
              ColoredBox(
                color:
                    appliedDefaults.suffixIconColor.toString().contains('null')
                        ? Colors.red
                        : Colors.green,
                child: Text(appliedDefaults.suffixIconColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.counterStyle.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.counterStyle.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.fillColor.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.fillColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.focusColor.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.focusColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.hoverColor.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.hoverColor.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.errorBorder.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.errorBorder.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.focusedBorder.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.focusedBorder.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.focusedErrorBorder
                        .toString()
                        .contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.focusedErrorBorder.toString()),
              ),
              ColoredBox(
                color:
                    appliedDefaults.disabledBorder.toString().contains('null')
                        ? Colors.red
                        : Colors.green,
                child: Text(appliedDefaults.disabledBorder.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.enabledBorder.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.enabledBorder.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.border.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.border.toString()),
              ),
              ColoredBox(
                color: appliedDefaults.constraints.toString().contains('null')
                    ? Colors.red
                    : Colors.green,
                child: Text(appliedDefaults.constraints.toString()),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
``` 
	
</details>

### Before

![before screenshot](https://github.com/flutter/flutter/assets/48603081/ae564e15-4029-4feb-810f-e46b9312a257)

### After
![after screenshot](https://github.com/flutter/flutter/assets/48603081/8924a1d8-ffde-494b-bf44-66dab4d28845)
This commit is contained in:
Taha Tesser 2023-06-19 10:06:19 +03:00 committed by GitHub
parent 59e30ef421
commit 165d237792
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 101 additions and 17 deletions

View file

@ -3593,11 +3593,14 @@ class InputDecoration {
errorMaxLines: errorMaxLines ?? theme.errorMaxLines, errorMaxLines: errorMaxLines ?? theme.errorMaxLines,
floatingLabelBehavior: floatingLabelBehavior ?? theme.floatingLabelBehavior, floatingLabelBehavior: floatingLabelBehavior ?? theme.floatingLabelBehavior,
floatingLabelAlignment: floatingLabelAlignment ?? theme.floatingLabelAlignment, floatingLabelAlignment: floatingLabelAlignment ?? theme.floatingLabelAlignment,
isCollapsed: isCollapsed,
isDense: isDense ?? theme.isDense, isDense: isDense ?? theme.isDense,
contentPadding: contentPadding ?? theme.contentPadding, contentPadding: contentPadding ?? theme.contentPadding,
isCollapsed: isCollapsed,
iconColor: iconColor ?? theme.iconColor,
prefixStyle: prefixStyle ?? theme.prefixStyle, prefixStyle: prefixStyle ?? theme.prefixStyle,
prefixIconColor: prefixIconColor ?? theme.prefixIconColor,
suffixStyle: suffixStyle ?? theme.suffixStyle, suffixStyle: suffixStyle ?? theme.suffixStyle,
suffixIconColor: suffixIconColor ?? theme.suffixIconColor,
counterStyle: counterStyle ?? theme.counterStyle, counterStyle: counterStyle ?? theme.counterStyle,
filled: filled ?? theme.filled, filled: filled ?? theme.filled,
fillColor: fillColor ?? theme.fillColor, fillColor: fillColor ?? theme.fillColor,

View file

@ -4189,24 +4189,50 @@ void main() {
}); });
testWidgets('InputDecorationTheme.inputDecoration', (WidgetTester tester) async { testWidgets('InputDecorationTheme.inputDecoration', (WidgetTester tester) async {
const TextStyle themeStyle = TextStyle(color: Colors.green); const TextStyle themeStyle = TextStyle(color: Color(0xFF00FFFF));
const TextStyle decorationStyle = TextStyle(color: Colors.blue); const Color themeColor = Color(0xFF00FF00);
const InputBorder themeInputBorder = OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xFF0000FF),
),
);
const TextStyle decorationStyle = TextStyle(color: Color(0xFFFFFF00));
const Color decorationColor = Color(0xFF0000FF);
const InputBorder decorationInputBorder = OutlineInputBorder(
borderSide: BorderSide(
color: Color(0xFFFF00FF),
),
);
// InputDecorationTheme arguments define InputDecoration properties. // InputDecorationTheme arguments define InputDecoration properties.
InputDecoration decoration = const InputDecoration().applyDefaults( InputDecoration decoration = const InputDecoration().applyDefaults(
const InputDecorationTheme( const InputDecorationTheme(
labelStyle: themeStyle, labelStyle: themeStyle,
floatingLabelStyle: themeStyle,
helperStyle: themeStyle, helperStyle: themeStyle,
helperMaxLines: 2,
hintStyle: themeStyle, hintStyle: themeStyle,
errorStyle: themeStyle, errorStyle: themeStyle,
errorMaxLines: 2,
floatingLabelBehavior: FloatingLabelBehavior.never,
floatingLabelAlignment: FloatingLabelAlignment.center,
isDense: true, isDense: true,
contentPadding: EdgeInsets.all(1.0), contentPadding: EdgeInsets.all(1.0),
iconColor: themeColor,
prefixStyle: themeStyle, prefixStyle: themeStyle,
prefixIconColor: themeColor,
suffixStyle: themeStyle, suffixStyle: themeStyle,
suffixIconColor: themeColor,
counterStyle: themeStyle, counterStyle: themeStyle,
filled: true, filled: true,
fillColor: Colors.red, fillColor: themeColor,
focusColor: Colors.blue, focusColor: themeColor,
hoverColor: themeColor,
errorBorder: themeInputBorder,
focusedBorder: themeInputBorder,
focusedErrorBorder: themeInputBorder,
disabledBorder: themeInputBorder,
enabledBorder: themeInputBorder,
border: InputBorder.none, border: InputBorder.none,
alignLabelWithHint: true, alignLabelWithHint: true,
constraints: BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40), constraints: BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40),
@ -4214,16 +4240,31 @@ void main() {
); );
expect(decoration.labelStyle, themeStyle); expect(decoration.labelStyle, themeStyle);
expect(decoration.floatingLabelStyle, themeStyle);
expect(decoration.helperStyle, themeStyle); expect(decoration.helperStyle, themeStyle);
expect(decoration.helperMaxLines, 2);
expect(decoration.hintStyle, themeStyle); expect(decoration.hintStyle, themeStyle);
expect(decoration.errorStyle, themeStyle); expect(decoration.errorStyle, themeStyle);
expect(decoration.errorMaxLines, 2);
expect(decoration.floatingLabelBehavior, FloatingLabelBehavior.never);
expect(decoration.floatingLabelAlignment, FloatingLabelAlignment.center);
expect(decoration.isDense, true); expect(decoration.isDense, true);
expect(decoration.contentPadding, const EdgeInsets.all(1.0)); expect(decoration.contentPadding, const EdgeInsets.all(1.0));
expect(decoration.iconColor, themeColor);
expect(decoration.prefixStyle, themeStyle); expect(decoration.prefixStyle, themeStyle);
expect(decoration.prefixIconColor, themeColor);
expect(decoration.suffixStyle, themeStyle); expect(decoration.suffixStyle, themeStyle);
expect(decoration.suffixIconColor, themeColor);
expect(decoration.counterStyle, themeStyle); expect(decoration.counterStyle, themeStyle);
expect(decoration.filled, true); expect(decoration.filled, true);
expect(decoration.fillColor, Colors.red); expect(decoration.fillColor, themeColor);
expect(decoration.focusColor, themeColor);
expect(decoration.hoverColor, themeColor);
expect(decoration.errorBorder, themeInputBorder);
expect(decoration.focusedBorder, themeInputBorder);
expect(decoration.focusedErrorBorder, themeInputBorder);
expect(decoration.disabledBorder, themeInputBorder);
expect(decoration.enabledBorder, themeInputBorder);
expect(decoration.border, InputBorder.none); expect(decoration.border, InputBorder.none);
expect(decoration.alignLabelWithHint, true); expect(decoration.alignLabelWithHint, true);
expect(decoration.constraints, const BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40)); expect(decoration.constraints, const BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40));
@ -4231,57 +4272,97 @@ void main() {
// InputDecoration (baseDecoration) defines InputDecoration properties // InputDecoration (baseDecoration) defines InputDecoration properties
decoration = const InputDecoration( decoration = const InputDecoration(
labelStyle: decorationStyle, labelStyle: decorationStyle,
floatingLabelStyle: decorationStyle,
helperStyle: decorationStyle, helperStyle: decorationStyle,
helperMaxLines: 3,
hintStyle: decorationStyle, hintStyle: decorationStyle,
errorStyle: decorationStyle, errorStyle: decorationStyle,
errorMaxLines: 3,
floatingLabelBehavior: FloatingLabelBehavior.always,
floatingLabelAlignment: FloatingLabelAlignment.start,
isDense: false, isDense: false,
contentPadding: EdgeInsets.all(4.0), contentPadding: EdgeInsets.all(4.0),
iconColor: decorationColor,
prefixStyle: decorationStyle, prefixStyle: decorationStyle,
prefixIconColor: decorationColor,
suffixStyle: decorationStyle, suffixStyle: decorationStyle,
suffixIconColor: decorationColor,
counterStyle: decorationStyle, counterStyle: decorationStyle,
filled: false, filled: false,
fillColor: Colors.blue, fillColor: decorationColor,
focusColor: decorationColor,
hoverColor: decorationColor,
errorBorder: decorationInputBorder,
focusedBorder: decorationInputBorder,
focusedErrorBorder: decorationInputBorder,
disabledBorder: decorationInputBorder,
enabledBorder: decorationInputBorder,
border: OutlineInputBorder(), border: OutlineInputBorder(),
alignLabelWithHint: false, alignLabelWithHint: false,
constraints: BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40), constraints: BoxConstraints(minWidth: 40, maxWidth: 50, minHeight: 60, maxHeight: 70),
).applyDefaults( ).applyDefaults(
const InputDecorationTheme( const InputDecorationTheme(
labelStyle: themeStyle, labelStyle: themeStyle,
floatingLabelStyle: themeStyle,
helperStyle: themeStyle, helperStyle: themeStyle,
helperMaxLines: 5, helperMaxLines: 2,
hintStyle: themeStyle, hintStyle: themeStyle,
errorStyle: themeStyle, errorStyle: themeStyle,
errorMaxLines: 4, errorMaxLines: 2,
floatingLabelBehavior: FloatingLabelBehavior.never,
floatingLabelAlignment: FloatingLabelAlignment.center,
isDense: true, isDense: true,
contentPadding: EdgeInsets.all(1.0), contentPadding: EdgeInsets.all(1.0),
iconColor: themeColor,
prefixStyle: themeStyle, prefixStyle: themeStyle,
prefixIconColor: themeColor,
suffixStyle: themeStyle, suffixStyle: themeStyle,
suffixIconColor: themeColor,
counterStyle: themeStyle, counterStyle: themeStyle,
filled: true, filled: true,
fillColor: Colors.red, fillColor: themeColor,
focusColor: Colors.blue, focusColor: themeColor,
hoverColor: themeColor,
errorBorder: themeInputBorder,
focusedBorder: themeInputBorder,
focusedErrorBorder: themeInputBorder,
disabledBorder: themeInputBorder,
enabledBorder: themeInputBorder,
border: InputBorder.none, border: InputBorder.none,
alignLabelWithHint: true, alignLabelWithHint: true,
constraints: BoxConstraints(minWidth: 40, maxWidth: 30, minHeight: 20, maxHeight: 10), constraints: BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40),
), ),
); );
expect(decoration.labelStyle, decorationStyle); expect(decoration.labelStyle, decorationStyle);
expect(decoration.floatingLabelStyle, decorationStyle);
expect(decoration.helperStyle, decorationStyle); expect(decoration.helperStyle, decorationStyle);
expect(decoration.helperMaxLines, 5); expect(decoration.helperMaxLines, 3);
expect(decoration.hintStyle, decorationStyle); expect(decoration.hintStyle, decorationStyle);
expect(decoration.errorStyle, decorationStyle); expect(decoration.errorStyle, decorationStyle);
expect(decoration.errorMaxLines, 4); expect(decoration.errorMaxLines, 3);
expect(decoration.floatingLabelBehavior, FloatingLabelBehavior.always);
expect(decoration.floatingLabelAlignment, FloatingLabelAlignment.start);
expect(decoration.isDense, false); expect(decoration.isDense, false);
expect(decoration.contentPadding, const EdgeInsets.all(4.0)); expect(decoration.contentPadding, const EdgeInsets.all(4.0));
expect(decoration.iconColor, decorationColor);
expect(decoration.prefixStyle, decorationStyle); expect(decoration.prefixStyle, decorationStyle);
expect(decoration.prefixIconColor, decorationColor);
expect(decoration.suffixStyle, decorationStyle); expect(decoration.suffixStyle, decorationStyle);
expect(decoration.suffixIconColor, decorationColor);
expect(decoration.counterStyle, decorationStyle); expect(decoration.counterStyle, decorationStyle);
expect(decoration.filled, false); expect(decoration.filled, false);
expect(decoration.fillColor, Colors.blue); expect(decoration.fillColor, decorationColor);
expect(decoration.focusColor, decorationColor);
expect(decoration.hoverColor, decorationColor);
expect(decoration.errorBorder, decorationInputBorder);
expect(decoration.focusedBorder, decorationInputBorder);
expect(decoration.focusedErrorBorder, decorationInputBorder);
expect(decoration.disabledBorder, decorationInputBorder);
expect(decoration.enabledBorder, decorationInputBorder);
expect(decoration.border, const OutlineInputBorder()); expect(decoration.border, const OutlineInputBorder());
expect(decoration.alignLabelWithHint, false); expect(decoration.alignLabelWithHint, false);
expect(decoration.constraints, const BoxConstraints(minWidth: 10, maxWidth: 20, minHeight: 30, maxHeight: 40)); expect(decoration.constraints, const BoxConstraints(minWidth: 40, maxWidth: 50, minHeight: 60, maxHeight: 70));
}); });
testWidgets('InputDecorationTheme.inputDecoration with MaterialState', (WidgetTester tester) async { testWidgets('InputDecorationTheme.inputDecoration with MaterialState', (WidgetTester tester) async {