Skip to content

Implementing Navigator.popToFirst #15493

@amrnt

Description

@amrnt

I have a question about navigation. On instagram in iOS, however you go deep in a tab, when you click the tab you will have one pop action back to your main screen for that tab. I tried popUntil but it shows continues pops.

I implemented a function that does what I want. Before submitting it as a PR to flutter, I want to check if there is another solution for this. And here's the function I created:

  /// In flutter/lib/src/widgets/navigator.dart
  /// Calls [pop] to `position` after removing all routes between the position and the last.
  void popToFirst(int position) {
    assert(_history.isNotEmpty);

    if (_history.length > position) {
      final List<Route<dynamic>> routes =
          _history.sublist(position, _history.length - 1);
      routes.forEach(removeRoute);
      pop();
    }
  }

Metadata

Metadata

Assignees

No one assigned

    Labels

    f: routesNavigator, Router, and related APIs.frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions