Skip to content

TextButton not working inside AppBar actions[ ] #84642

Description

@ayaankhan98

Steps to Reproduce

  1. Run flutter create bug.
  2. Update the lib/main.dart as follows
import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Test App"),
        actions: [
          TextButton(
            onPressed: () {},
            child: const Text(
              "Button 1",
            ),
          ),
          TextButton(
            onPressed: () {},
            child: const Text(
              "Button 2",
            ),
          )
        ],
      ),
    );
  }
}

Expected results:
TextButton should be visible in AppBar

Actual results:
TextButton is not visible. But if we replace TextButton with ElevatedButton then it will work fine. I think TextButton should also give the same behaviour as ElevatedButton meaning TextButton should also be visible.

Logs
 flutter run --verbose -d chrome
[  +96 ms] executing: uname -m
[  +43 ms] Exit code 0 from: uname -m
[        ] x86_64
[   +8 ms] executing: [/home/ayaan/workspace/repos/flutter/] git -c log.showSignature=false log -n 1 --pretty=format:%H
[   +5 ms] Exit code 0 from: git -c log.showSignature=false log -n 1 --pretty=format:%H
[        ] 0d1358d7ee4c08d8e99a9f1b0cf6d2031a2e1fcb
[        ] executing: [/home/ayaan/workspace/repos/flutter/] git tag --points-at 0d1358d7ee4c08d8e99a9f1b0cf6d2031a2e1fcb
[  +15 ms] Exit code 0 from: git tag --points-at 0d1358d7ee4c08d8e99a9f1b0cf6d2031a2e1fcb
[   +1 ms] executing: [/home/ayaan/workspace/repos/flutter/] git describe --match *.*.* --long --tags 0d1358d7ee4c08d8e99a9f1b0cf6d2031a2e1fcb
[  +43 ms] Exit code 0 from: git describe --match *.*.* --long --tags 0d1358d7ee4c08d8e99a9f1b0cf6d2031a2e1fcb
[        ] 2.3.0-16.0.pre-278-g0d1358d7ee
[   +7 ms] executing: [/home/ayaan/workspace/repos/flutter/] git rev-parse --abbrev-ref --symbolic @{u}
[   +5 ms] Exit code 0 from: git rev-parse --abbrev-ref --symbolic @{u}
[        ] origin/master
[        ] executing: [/home/ayaan/workspace/repos/flutter/] git ls-remote --get-url origin
[   +4 ms] Exit code 0 from: git ls-remote --get-url origin
[        ] https://github.com/ayaankhan98/flutter
[  +99 ms] Unable to locate an Android SDK.
[   +4 ms] executing: [/home/ayaan/workspace/repos/flutter/] git rev-parse --abbrev-ref HEAD
[   +5 ms] Exit code 0 from: git rev-parse --abbrev-ref HEAD
[        ] master
[  +75 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterWebSdk' is not required, skipping update.
[   +3 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'WindowsUwpEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +92 ms] Artifact Instance of 'AndroidGenSnapshotArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'AndroidInternalBuildArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'IOSEngineArtifacts' is not required, skipping update.
[   +2 ms] Artifact Instance of 'WindowsEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'WindowsUwpEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxEngineArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'LinuxFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'MacOSFuchsiaSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerSDKArtifacts' is not required, skipping update.
[        ] Artifact Instance of 'FlutterRunnerDebugSymbols' is not required, skipping update.
[  +92 ms] Skipping pub get: version match.
[ +151 ms] Generating /home/ayaan/workspace/testapp/android/app/src/main/java/io/flutter/plugins/GeneratedPluginRegistrant.java
[ +524 ms] Launching lib/main.dart on Chrome in debug mode...
[ +140 ms] Updating assets
[ +102 ms] Waiting for connection from debug service on Chrome...
[  +82 ms] <- reset
[  +10 ms] /home/ayaan/workspace/repos/flutter/bin/cache/dart-sdk/bin/dart --disable-dart-dev
/home/ayaan/workspace/repos/flutter/bin/cache/artifacts/engine/linux-x64/frontend_server.dart.snapshot --sdk-root
/home/ayaan/workspace/repos/flutter/bin/cache/flutter_web_sdk/ --incremental --target=dartdevc --debugger-module-names --experimental-emit-debug-metadata
-DFLUTTER_WEB_AUTO_DETECT=true --output-dill /tmp/flutter_tools.AWAJDR/flutter_tool.WLMDTU/app.dill --libraries-spec
file:///home/ayaan/workspace/repos/flutter/bin/cache/flutter_web_sdk/libraries.json --packages /home/ayaan/workspace/testapp/.dart_tool/package_config.json
-Ddart.vm.profile=false -Ddart.vm.product=false --enable-asserts --track-widget-creation --filesystem-root /tmp/flutter_tools.AWAJDR/flutter_tools.DLXQDR
--filesystem-scheme org-dartlang-app --initialize-from-dill build/b1b715402d823b7fd5c2b68d2edcb2ce.cache.dill.track.dill --platform
file:///home/ayaan/workspace/repos/flutter/bin/cache/flutter_web_sdk/kernel/flutter_ddc_sdk_sound.dill --sound-null-safety
[  +16 ms] <- compile org-dartlang-app:/web_entrypoint.dart
[+23608 ms] Waiting for connection from debug service on Chrome... (completed in 23.7s)
[        ] Synced 28.6MB.
[        ] <- accept
[        ] Caching compiled dill
[ +391 ms] Using Google Chrome 91.0.4472.77 unknown

[ +100 ms] executing: uname -m
[  +14 ms] Exit code 0 from: uname -m
[        ] x86_64
[ +431 ms] [CHROME]:Gtk-Message: 22:39:08.532: Failed to load module "appmenu-gtk-module"
[ +145 ms] [CHROME]:
[        ] [CHROME]:DevTools listening on ws://127.0.0.1:33447/devtools/browser/3ca3f787-eda3-4e88-a5ca-1b84d36aeb55
[ +572 ms] DwdsInjector: Received request for entrypoint at http://localhost:42399/main_module.bootstrap.js
[   +9 ms] MetadataProvider: Loading debug metadata...
[  +27 ms] MetadataProvider: Loaded debug metadata (sound null safety)
[  +14 ms] DwdsInjector: Injected debugging metadata for entrypoint at http://localhost:42399/main_module.bootstrap.js
[+2287 ms] ChromeProxyService: Initializing expression compiler for main_module.bootstrap.js with sound null safety: true
[  +99 ms] DevHandler: Debug service listening on ws://127.0.0.1:41415/By2zPxj_N-c=/ws

[  +33 ms] This app is linked to the debug service: ws://127.0.0.1:41415/By2zPxj_N-c=/ws
[  +11 ms] Debug service listening on ws://127.0.0.1:41415/By2zPxj_N-c=/ws
[        ] 💪 Running with sound null safety 💪
[   +5 ms] 🔥  To hot restart changes while running, press "r" or "R".
[        ] For a more detailed help message, press "h". To quit, press "q".
[   +1 ms] An Observatory debugger and profiler on Chrome is available at: http://127.0.0.1:41415/By2zPxj_N-c=
[+2891 ms] DevTools activation throttled until 2021-06-16 04:08:36.603489.
[+101179 ms] The Flutter DevTools debugger and profiler on Chrome is available at: http://127.0.0.1:9102?uri=http%3A%2F%2F127.0.0.1%3A41415%2FBy2zPxj_N-c%3D
[+52979 ms] DevHandler: Stopped debug service on ws://127.0.0.1:37035

[ +179 ms] Application finished.
[  +11 ms] "flutter run" took 1,86,251ms.
[ +264 ms] ensureAnalyticsSent: 252ms
[   +1 ms] Running shutdown hooks
[        ] Shutdown hooks complete
[        ] exiting with code 0
flutter analyze
Analyzing testapp...                                                    
No issues found! (ran in 3.2s)
 flutter doctor -v
[✓] Flutter (Channel master, 2.3.0-17.0.pre.278, on Manjaro Linux 5.4.123-1-MANJARO, locale en_IN)
    • Flutter version 2.3.0-17.0.pre.278 at /home/ayaan/workspace/repos/flutter
    • Upstream repository https://github.com/ayaankhan98/flutter
    • Framework revision 0d1358d7ee (6 days ago), 2021-06-09 01:59:04 -0700
    • Engine revision 299c801978
    • Dart version 2.14.0 (build 2.14.0-190.0.dev)

[✗] Android toolchain - develop for Android devices
    ✗ Unable to locate Android SDK.
      Install Android Studio from: https://developer.android.com/studio/index.html
      On first launch it will assist you in installing the Android SDK components.
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).
      If the Android SDK has been installed to a custom location, please use
      `flutter config --android-sdk` to update to that location.


[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /opt/google/chrome/chrome

[✓] Linux toolchain - develop for Linux desktop
    • clang version 11.1.0
    • cmake version 3.20.3
    • ninja version 1.10.2
    • pkg-config version 1.7.3

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] VS Code
    • VS Code at /snap/code/current
    • Flutter extension version 3.23.0

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Manjaro Linux 5.4.123-1-MANJARO
    • Chrome (web)    • chrome • web-javascript • Google Chrome 91.0.4472.77 unknown

! Doctor found issues in 2 categories.

Activity

  1. rohanjsh commented on Jun 15, 2021

    @rohanjsh

    Edit the style property and make the text font color to white

  2. ayaankhan98 commented on Jun 15, 2021

    @ayaankhan98
    ContributorAuthor

    Edit the style property and make the text font color to white

    It's visible. I thought default color is white that's why i didn't thought about changing text color

  3. rohanjsh commented on Jun 15, 2021

    @rohanjsh

    Edit the style property and make the text font color to white

    It's visible. I thought default color is white that's why i didn't thought about changing text color

    Solved then??

  4. ayaankhan98 commented on Jun 15, 2021

    @ayaankhan98
    ContributorAuthor

    Edit the style property and make the text font color to white

    It's visible. I thought default color is white that's why i didn't thought about changing text color

    Solved then??

    yes button is visible. But shouldn't the default text color supposed to be white or something that is visible with respect to theme color?

  5. rohanjsh commented on Jun 15, 2021

    @rohanjsh

    That is because of the primary swatch defined in Theme Data, where there is a map of colors by default for the entire app. Also, I guess the text button copies the same ThemeData and hence the primary swatch.

  6. ayaankhan98 commented on Jun 15, 2021

    @ayaankhan98
    ContributorAuthor

    That is because of the primary swatch defined in Theme Data, where there is a map of colors by default for the entire app. Also, I guess the text button copies the same ThemeData and hence the primary swatch.

    I see, Basically what is happening is if primarySwatch is green then TextButton text color becomes green, if primarySwatch is blue then TextButton text color becomes blue or anything thing else, In all these cases TextButton will not be visible because TextButton text color matches with background color of appbar. So we explicitly set text color for TextButton to make it visible.
    But the case with Elevated button is not same, It's not required to set style property of Elevated button to make it visible. It automatically takes different color as compared to background of AppBar to make itself visible.

    I think TextButton should also exhibit the same behaviour isn't?

  7. ayaankhan98 commented on Jun 15, 2021

    @ayaankhan98
    ContributorAuthor

    Also in theme_data.dart it's mentioned

    207 |   /// The [textTheme] [TextStyle] colors are black if the color scheme's
    208 |  /// brightness is [Brightness.light], and white for [Brightness.dark].
    

    textTheme and TextStyle should be by default such that it is visible with any primarySwatch color. This is not working as expected in case of TextButton. Isn't?

  8. rohanjsh commented on Jun 15, 2021

    @rohanjsh

    Check out the TextButton properties, the textstyle references the foreground color. I am not sure if that is the case, but you can check this

    textStyle: resolvedTextStyle?.copyWith(color: resolvedForegroundColor),
    
  9. darshankawar commented on Jun 16, 2021

    @darshankawar
    Member

    @ayaankhan98
    You may override the primarySwatch property using colorScheme property, as below:

    theme: ThemeData(
            primarySwatch: Colors.blue,
            colorScheme: ColorScheme.fromSwatch(
              primarySwatch: Colors.green
            ).copyWith()
          ),
    

    which shows the TextButtons in green color and are visible in appbar, as below:

    Screenshot 2021-06-16 at 5 41 06 PM

  10. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 16, 2021
  11. added
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jun 16, 2021
  12. self-assigned this
    on Jun 16, 2021
  13. ayaankhan98 commented on Jun 17, 2021

    @ayaankhan98
    ContributorAuthor

    You may override the primarySwatch property using colorScheme property, as below:

    Yes we can override, But my point is by default it should be such that it is visible by itslef.

  14. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Jun 17, 2021
  15. darshankawar commented on Jun 17, 2021

    @darshankawar
    Member
    flutter doctor -v
    /Users/dhs/Documents/Fluttersdk/flutter/bin/flutter doctor --verbose
    [✓] Flutter (Channel stable, 2.2.2, on Mac OS X 10.15.4 19E2269 darwin-x64, locale en-GB)
        • Flutter version 2.2.2 at /Users/dhs/Documents/Fluttersdk/flutter
        • Framework revision d79295af24 (6 days ago), 2021-06-11 08:56:01 -0700
        • Engine revision 91c9fc8fe0
        • Dart version 2.13.3
    
    [✓] Xcode - develop for iOS and macOS
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 12.3, Build version 12C33
        • CocoaPods version 1.10.1
    
    [✓] Chrome - develop for the web
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] VS Code (version 1.56.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.21.0
    
    [✓] Connected device (2 available)
        • macOS (desktop) • macos  • darwin-x64     • Mac OS X 10.15.4 19E2269 darwin-x64
        • Chrome (web)    • chrome • web-javascript • Google Chrome 91.0.4472.106
    
    • No issues found!
    
  16. added
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    a: qualityA truly polished experience
    and removed
    in triagePresently being triaged by the triage team
    on Jun 17, 2021
  17. HansMuller commented on Jun 17, 2021

    @HansMuller
    Contributor

    @ayaankhan98 - you're right, it really shouldn't be necessary to do anything in this case. The problem is that the AppBar's background defaults to the color scheme's primary color, as do the text buttons' text color.

    The sample below shows how to workaround the issue.

    We could mitigate the problem by wrapping the actions in a TextButtonTheme with a style like the one below. That doesn't cover all cases, since one can specify the AppBar's background color - #71184 - and the color might not contrast very well with ColorScheme.onPrimary. However the approach shown below could be used in that case.

    class MyHomePage extends StatelessWidget {
      const MyHomePage({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        final ButtonStyle style = TextButton.styleFrom(
          primary: Theme.of(context).colorScheme.onPrimary
        );
        return Scaffold(
          appBar: AppBar(
            title: const Text("Test App"),
            actions: [
              TextButton(
                style: style,
                onPressed: () {},
                child: const Text(
                  "Button 1",
                ),
              ),
              TextButton(
                style: style,
                onPressed: () {},
                child: const Text(
                  "Button 2",
                ),
              )
            ],
          ),
        );
      }
    }
  18. added
    r: fixedIssue is closed as already fixed in a newer version
    on Jul 1, 2021
  19. HansMuller commented on Jul 9, 2021

    @HansMuller
    Contributor

    Unfortunately the "fix", #85585, had to be reverted because the many application tests failed because we were overriding the default foreground color for TextButtons. It's not really practical to try and detect the situation where the contrast between the default text button theme's foreground color and the AppBar's background color is so low that we should override the text button theme's foreground color. It's possible but not practical because it would introduce a complex dependency that would difficult to document and likely to confuse when it wasn't expected. For example, an app that tried to animate the foreground color of TextButton AppBar actions might be surprised when - mid animation - the expected color was automatically changed.

    So, instead we'll "fix" this problem by documenting the remedy noted in #84642 (comment)

  20. github-actions commented on Jul 30, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  21. locked as resolved and limited conversation to collaborators on Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

a: qualityA truly polished experiencefound in release: 2.2Found to occur in 2.2frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesr: fixedIssue is closed as already fixed in a newer version

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions