Skip to content

TextField decoration documentation/assertion bug #133662

Open
flutter/packages
#13734

Description

@outring

Is there an existing issue for this?

Steps to reproduce

  1. Use Material TextField inside a custom theme, outside of MaterialApp and any other Flutter-provided wrappers

  2. Set TextField.decoration to null as per documentation only having it non-null requires you to have a Material ancestor:

    If decoration is non-null (which is the default), the text field requires one of its ancestors to be a Material widget.

    Which is understood as having decoration value to be null allows you not to have Material ancestors

Expected results

Everything works and no errors are happening

Actual results

The error:

No Material widget found.

TextField widgets require a Material widget ancestor within the closest LookupBoundary.

So either documentation has to be changed or the assertion has to take into account the decoration value

Code sample

Code sample
import 'package:flutter/cupertino.dart' show DefaultCupertinoLocalizations;
import 'package:flutter/material.dart' show DefaultMaterialLocalizations, TextField;
import 'package:flutter/widgets.dart';

void main() {
  runApp(TextFieldExample());
}

class TextFieldExample extends StatelessWidget {
  @override
  Widget build(final BuildContext context) {
    return Localizations(
      locale: basicLocaleListResolution(null, [Locale('en', 'US')]),
      delegates: [
        DefaultMaterialLocalizations.delegate,
        DefaultCupertinoLocalizations.delegate,
        DefaultWidgetsLocalizations.delegate,
      ],
      child: TextField(decoration: null),
    );
  }
}

Screenshots or Video

No response

Logs

No response

Flutter Doctor output

[✓] Flutter (Channel stable, 3.10.5, on macOS 12.6.3 21G419 darwin-x64, locale en-RU)
    • Flutter version 3.10.5 on channel stable at /usr/local/Caskroom/flutter/3.10.5/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (3 months ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/outring/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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-17.0.6b829.9-10027231)

[✓] Connected device (3 available)
    • iPhone 14 (mobile) • BB48C113-D88F-42CF-992A-A9ABDE3F5069 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 12.6.3 21G419 darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 116.0.5845.110

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

! Doctor found issues in 1 category.

Activity

  1. opxdelwin commented on Aug 30, 2023

    @opxdelwin
    Contributor

    @outring Thanks for filing this issue, please provide a minimal reproducible code so that we can assist you better.
    If possible, format flutter doctor output as per markdown syntaxes.

  2. dam-ease commented on Aug 30, 2023

    @dam-ease

    Hi @outring. Please can you provide a minimal complete reproducible code sample that doesn’t include 3rd party plugins or complex production code?
    Thank you

  3. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 30, 2023
  4. outring commented on Aug 30, 2023

    @outring
    Author

    Hi @dam-ease. I've updated the code sample. According to the documentation it should work without errors but in debug mode it causes an assertion failure here:

    assert(debugCheckHasMaterial(context));

    Either the assertion should take into account the value of decoration or the documentation should be updated to avoid confusion about the Material ancestors necessity. Thanks!

  5. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 30, 2023
  6. dam-ease commented on Aug 31, 2023

    @dam-ease

    Hi @outring. Thanks for your response. Kindly check out the documentation https://docs.flutter.dev/release/breaking-changes/text-field-material-localizations on why you are getting this issue.

  7. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2023
  8. outring commented on Aug 31, 2023

    @outring
    Author

    @dam-ease thanks. Please, check my code sample — it contains all the localizations (specifically it uses the method explained in the 3rd example of the page you have shared). The error I'm getting is:

    No Material widget found.
    
    TextField widgets require a Material widget ancestor within the closest LookupBoundary.
    

    As you can see it says nothing about localizations. If I add a Material ancestor I won't get any errors at all. So the localizations have nothing to do with this

    The problem is that per documentation you can use use TextField without Material ancestor if the decoration is null. Which is currently not true per implementation

  9. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2023
  10. dam-ease commented on Aug 31, 2023

    @dam-ease

    Hi @outring. I think there might be a misinterpretation in the line you're referencing from the docs. From my understanding and from the error displayed, TextField widget requires a Material ancestor as also shown https://docs.flutter.dev/release/breaking-changes/text-field-material-localizations.

  11. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2023
  12. outring commented on Aug 31, 2023

    @outring
    Author
    1. The documentation doesn't say that the TextField requires Material ancestor at all times. It only says that it's needed only when the decoration is not null:

      If decoration is non-null (which is the default), the text field requires one of its ancestors to be a Material widget.

      So if you believe the implementation is correct, then the documentation has to be fixed to avoid future confusion

    2. https://docs.flutter.dev/release/breaking-changes/text-field-material-localizations specifically doesn't say anything about Material ancestors, it only covers Localizations ancestor or a MaterialApp ancestor which has Localizations built-in. So I'm afraid it's not related to the issue I'm facing in any way

    I would like to have a confirmation on the point 1, whether the documentation is wrong or implementation so I can prepare a PR for this issue, please

  13. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Aug 31, 2023
  14. dam-ease commented on Aug 31, 2023

    @dam-ease

    Hi @outring. Thanks for your patience and detailed explanation. From what's here, it seems the textfield applies the effectiveTextDecoration only when the textDecoration is not null. But then, if you don't want to use the Material, you could look into using EditableText directly so that you can customize it.

  15. 5 remaining items

  16. justinmc commented on Aug 31, 2023

    @justinmc
    Contributor

    Here's where that assertion was added for the record: #16147

    I wonder if someone uses a TextField inside of CupertinoApp, does this assertion get triggered?

    If someone (CC @Renzo-Olivares ) can put together a simple app that doesn't have a Material where TextField seems to work with the assertion disabled, then let's remove it. Otherwise, if the TextField seems to break somehow, then we should remove the line in the docs.

  17. Renzo-Olivares commented on Sep 1, 2023

    @Renzo-Olivares
    Contributor

    I came up with a few samples to observe this behavior.

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    void main() {
      runApp(const WidgetsWithTextFieldApp());
    }
    
    /// Using [TextField] in [CupertinoApp].
    class CupertinoWithTextFieldApp extends StatelessWidget {
      const CupertinoWithTextFieldApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const CupertinoApp(
          localizationsDelegates: [
            DefaultMaterialLocalizations.delegate,
          ],
          home: CupertinoPageScaffold(
            child: Center(
              child: TextField(),
            ),
          ),
        );
      }
    }
    
    /// Using [CupertinoTextField] in [MaterialApp].
    class MaterialWithCupertinoTextFieldApp extends StatelessWidget {
      const MaterialWithCupertinoTextFieldApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return const MaterialApp(
          home: Scaffold(
            body: Center(
              child: CupertinoTextField(),
            ),
          ),
        );
      }
    }
    
    /// Using [TextField] in [WidgetsApp].
    class WidgetsWithTextFieldApp extends StatelessWidget {
      const WidgetsWithTextFieldApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return WidgetsApp(
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            DefaultMaterialLocalizations.delegate,
          ],
          color: Colors.white,
          pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) => PageRouteBuilder<T>(
            settings: settings,
            pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) =>
                builder(context),
          ),
          home: Container(
            color: Colors.white,
            child: const Center(
              child: TextField(),
            ),
          ),
        );
      }
    }
    
    /// Using [CupertinoTextField] in [WidgetsApp].
    class WidgetsWithCupertinoTextFieldApp extends StatelessWidget {
      const WidgetsWithCupertinoTextFieldApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return WidgetsApp(
          localizationsDelegates: const <LocalizationsDelegate<dynamic>>[
            DefaultCupertinoLocalizations.delegate,
          ],
          color: Colors.white,
          pageRouteBuilder: <T>(RouteSettings settings, WidgetBuilder builder) => PageRouteBuilder<T>(
            settings: settings,
            pageBuilder: (BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) =>
                builder(context),
          ),
          home: Container(
            color: Colors.white,
            child: const Center(
              child: CupertinoTextField(),
            ),
          ),
        );
      }
    }

    Some observations:

    • CupertinoTextField will load without any localizations provided to WidgetsApp, but will crash when doing something like opening the context menu because of those missing localizations. We should probably assert and document this in CupertinoTextField.
    • CupertinoTextField currently "works" on all App variants without any changes.
    • Removing the assert for material ancestors in TextField lets us use TextField in CupertinoApp and WidgetsApp.
    • It looks like passing a decoration to TextField when used under WidgetsApp does not require it to have a material ancestor.

    Looks like CupertinoTextField and TextField should be able to be used in any App context given that the proper localizations are defined. I'll work on verifying this further and making this change.

  18. added
    ⏳Bot is counting down the days until it unassigns the issue
    on Jan 19, 2024
  19. flutter-triage-bot commented on Jan 19, 2024

    @flutter-triage-bot

    This issue is assigned to @Renzo-Olivares but has had no recent status updates. Please consider unassigning this issue if it is not going to be addressed in the near future. This allows people to have a clearer picture of what work is actually planned. Thanks!

  20. flutter-triage-bot commented on Apr 7, 2024

    @flutter-triage-bot

    This issue was assigned to @Renzo-Olivares but has had no status updates in a long time. To remove any ambiguity about whether the issue is being worked on, the assignee was removed.

  21. removed
    ⏳Bot is counting down the days until it unassigns the issue
    on Apr 7, 2024
  22. added
    P3Issues that are less important to the Flutter project
    and removed
    team-designOwned by Design Languages team
    on Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: error messageError messages from the Flutter frameworka: text inputEntering text in a text field or keyboard related problemsfound in release: 3.13Found to occur in 3.13found in release: 3.14Found to occur in 3.14has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions