Skip to content

BottomNavigationBar uses the wrong theme #19653

Description

@nathansamson

I am trying to build an aplication that changes its theme on runtime. When changing the theme, new BottomNavigationBars follow the wrong color until next state change.

Steps to Reproduce

https://gist.github.com/nathansamson/ac5a11e8a4d8e6d4d8b19610a267c931

  1. Run the application
  2. Click on the button
    The button changes themes, but also changes pages
  3. The bottom bar should be yellow (the new theme) but is blue (the old theme)

In code speak

  1. Change the theme of the MaterialApp
  2. Create and Navigate to a new page with a BottomNavigationBar
  3. Observe that the BottomNavigationBar is still the old theme color
  4. The BottomNavigationBar changes color when updating its state

Also observe the build() of the encolosing widget that builds the BottomNavigationBar is being run multple times (see logs).

Note: when I switch to fixed layout (instead of shifting) the correct background colors are observed immediately.

Logs

(Full logs in the gist)

[ +154 ms] I/flutter ( 2836): THEME CHANGE
[   +1 ms] I/flutter ( 2836): CHANGED THEME
[  +26 ms] I/flutter ( 2836): NEW APP
[  +58 ms] I/flutter ( 2836): MaterialColor(primary value: Color(0xff2196f3))
[ +114 ms] I/flutter ( 2836): Color(0xfffeea3b)
[  +56 ms] I/flutter ( 2836): MaterialColor(primary value: Color(0xffffeb3b))
[PERSONAL ANNOTATION]: after this log the BottomNavigationBar should clearly be yellow (see last color), but isn't
[+5835 ms] I/flutter ( 2836): MaterialColor(primary value: Color(0xffffeb3b))
[nathan@linux bottom_navigation_bar_theme]$ flutter analyze
Analyzing bottom_navigation_bar_theme...                         
No issues found! (ran in 1.0s)
[nathan@linux bottom_navigation_bar_theme]$ 

[✓] Flutter (Channel beta, v0.5.1, on Linux, locale en_US.UTF-8)
    • Flutter version 0.5.1 at /home/nathan/flutter
    • Framework revision c7ea3ca377 (8 weeks ago), 2018-05-29 21:07:33 +0200
    • Engine revision 1ed25ca7b7
    • Dart version 2.0.0-dev.58.0.flutter-f981f09760

[✓] Android toolchain - develop for Android devices (Android SDK 27.0.3)
    • Android SDK at /home/nathan/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-27, build-tools 27.0.3
    • Java binary at: /home/nathan/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)
    • All Android licenses accepted.

[✓] Android Studio (version 3.1)
    • Android Studio at /home/nathan/android-studio
    • Flutter plugin version 25.0.1
    • Dart plugin version 173.4700
    • Java version OpenJDK Runtime Environment (build 1.8.0_152-release-1024-b01)

[✓] Connected devices (1 available)
    • Google • 192.168.56.101:5555 • android-x86 • Android 8.0.0 (API 26)

• No issues found!

Activity

  1. zoechi commented on Jul 22, 2018

    @zoechi
    Contributor

    BottomNavigationBar is not added as child, but as sibling of MaterialApp

    You can use the builder parameter to get all dialogs, pages, BottomNavigationBar, ... wrapped in the theme you want to use application-wide.

        return new MaterialApp(
          builder: (context, widget) => Theme(data: currentTheme, child: widget),
          title: 'Flutter Demo',
          theme: currentTheme,
          home: new MyHomePage(title: 'Flutter Demo Home Page'),
        );

    https://docs.flutter.io/flutter/material/MaterialApp/builder.html

    @HansMuller is this the correct approach?
    Does it make sense to pass a builder: this way and the theme parameter?

  2. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    d: api docsIssues with https://api.flutter.dev/
    and removed on Jul 22, 2018
  3. nathansamson commented on Jul 22, 2018

    @nathansamson
    Author

    @zoechi While my app might be using themes incorrectly, the actual issue is not theme related.

    The issue is that changing the backgroundColor of an BottomNavigationBarItem in a BottomNavigationBar does not actually update its color.

    While I admit that might be an edge case, I still believe it should do so.
    Note that I made a Pull request already, which is not related to Themes (in fact BottomNavigationBar does not really seem to use Theme.of that often.

  4. zoechi commented on Jul 22, 2018

    @zoechi
    Contributor

    I didn't investigate in detail but after the change I suggested the BottomNavigationBar was yellow instead of the blue it was before.

  5. nathansamson commented on Jul 22, 2018

    @nathansamson
    Author

    @zoechi intresting. I suspect that that change prevents us from building my DashboardPage with wrong color in the first place (if you look at the logs, I print out the theme color in the build() method, it prints it once with the old color)

    But, while I certainly am willing to change the way my custom theming works, this is still an issue without Themes. Eg: The reason I mentioned Themes in my bug report (and in hindsight that was wrong), was because I am using Theme.of(context) to choose a background color. I could very well have chosen another arbitrary 2 colors and switched between those two without ever looking at the theme.

    eg lets imagine a BottomNavigationBarItem that changes color every second (not sure why anyone would want that, but there is no reason why it should not be possible). It will still expose the bug and keep the incorrect color. Activating another item and then back to the same item will take the correct color for 1 second, but then will be stale again.

    See my Pull request (#19661) which fixes the issue by keeping the _backgroundColor in sync with the selected item (did not test with the continues updates, but should work)

    If you prefer, I can make another small test case which does not uses themes, but some sort of other (local) trigger to change the color

  6. HansMuller commented on Aug 21, 2018

    @HansMuller
    Contributor

    Here's a simplified version of the original test case that demos the problem and validates the fix in #19661: https://gist.github.com/HansMuller/968aeba7981a5e0b6996f5600ab2d1c1

  7. github-actions commented on Sep 2, 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.

  8. locked as resolved and limited conversation to collaborators on Sep 2, 2021
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

    d: api docsIssues with https://api.flutter.dev/frameworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions