Skip to content

Commit 499f859

Browse files
[material_ui] Repaint NavigationIndicator when the selection changes without pointer input (flutter#13010)
Port of flutter#187802 to `material_ui`, following the process in flutter#188444. The original PR was approved by dkwingsmt and QuncCccccc but couldn't land because of the Material/Cupertino code freeze. `NavigationIndicator` draws its indicator with an `Ink`, which paints on the nearest ancestor `Material` rather than locally. When the indicator animates without an accompanying pointer interaction — for example when `selectedIndex` is changed programmatically — nothing tells that `Material` to repaint, so it keeps showing the indicator's previous frame. The fix calls `Material.of(context).markNeedsPaint()` on each animation tick. The regression test pumps a `NavigationDrawer`, changes `selectedIndex` from outside without any pointer events, and verifies the indicator repaints. I checked it fails with the one-line fix removed and passes with it, in its new location under `packages/material_ui/test/`. The two original commits were cherry-picked without merge commits, as the porting guide describes; rename detection placed them at `lib/src/navigation_bar.dart` and `test/navigation_drawer_test.dart` with no conflicts. The only new commit adds the pending changelog entry (`version: patch`). Fixes flutter#180359 ## Pre-Review Checklist I used AI tooling while working on this, and reviewed and tested all of it myself. [^1]: Regular contributors who have demonstrated familiarity with the repository guidelines only need to comment if the PR is not auto-exempted by repo tooling.
1 parent 83da6fb commit 499f859

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

‎packages/material_ui/lib/src/navigation_bar.dart‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,14 @@ class NavigationIndicator extends StatelessWidget {
867867
CurveTween(curve: Curves.easeInOutCubicEmphasized).transform(animation.value),
868868
);
869869

870+
// The indicator is drawn by the Ink widget below, which paints on the
871+
// nearest ancestor Material rather than locally. When the animation
872+
// ticks without an accompanying pointer interaction (for example, the
873+
// selected index was changed programmatically), that Material is not
874+
// otherwise notified and would keep showing the indicator's previous
875+
// frame. See https://github.com/flutter/flutter/issues/180359.
876+
Material.of(context).markNeedsPaint();
877+
870878
return Transform(
871879
alignment: Alignment.center,
872880
// Scale in the X direction only.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
changelog: |
2+
- Fixes `NavigationIndicator` not repainting when the selected destination changes without pointer interaction.
3+
version: patch

‎packages/material_ui/test/navigation_drawer_test.dart‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,59 @@ void main() {
591591
expect(inkMaterialRect.bottom, equals(footerRect.top));
592592
},
593593
);
594+
595+
// Regression test for https://github.com/flutter/flutter/issues/180359.
596+
testWidgets('Indicator repaints when selectedIndex changes without pointer interaction', (
597+
WidgetTester tester,
598+
) async {
599+
final scaffoldKey = GlobalKey<ScaffoldState>();
600+
var selectedIndex = 0;
601+
late StateSetter setSelectedIndex;
602+
603+
await tester.pumpWidget(
604+
MaterialApp(
605+
home: StatefulBuilder(
606+
builder: (BuildContext context, StateSetter setState) {
607+
setSelectedIndex = setState;
608+
return Scaffold(
609+
key: scaffoldKey,
610+
drawer: NavigationDrawer(
611+
selectedIndex: selectedIndex,
612+
children: const <Widget>[
613+
NavigationDrawerDestination(icon: Icon(Icons.ac_unit), label: Text('AC')),
614+
NavigationDrawerDestination(icon: Icon(Icons.access_alarm), label: Text('Alarm')),
615+
],
616+
),
617+
body: Container(),
618+
);
619+
},
620+
),
621+
),
622+
);
623+
624+
scaffoldKey.currentState!.openDrawer();
625+
await tester.pumpAndSettle();
626+
627+
// The indicator is painted by an Ink widget, which paints on the nearest
628+
// ancestor Material rather than on its own render object.
629+
final Finder indicatorInk = find
630+
.descendant(of: find.byType(NavigationIndicator).first, matching: find.byType(Ink))
631+
.first;
632+
final inkController = Material.of(tester.element(indicatorInk)) as RenderBox;
633+
634+
// Change the selected index without any pointer interaction and advance
635+
// one frame, stopping before the paint phase: the Material that hosts the
636+
// indicator's ink must have been marked as needing paint, otherwise it
637+
// keeps showing the indicator's previous frame.
638+
setSelectedIndex(() {
639+
selectedIndex = 1;
640+
});
641+
await tester.pump(null, EnginePhase.layout);
642+
expect(inkController.debugNeedsPaint, isTrue);
643+
644+
// Let the indicator animation run to completion normally.
645+
await tester.pumpAndSettle();
646+
});
594647
}
595648

596649
Widget _buildWidget(GlobalKey<ScaffoldState> scaffoldKey, Widget child, {bool? useMaterial3}) {

0 commit comments

Comments
 (0)