Создаем портфолио за 1 месяц
К концу месяца: 4 классных проекта в вашем портфолио! Стоимость: 100$. Подробности...
755 of 1024 menu
◀ ▶

Метод popRoute

Метод popRoute класса RouterDelegate возвращает Future: true, если pop обработан delegate. Вызывают при кнопке назад Android и жестах web.

Синтаксис

@override Future<bool> popRoute() async { return true; }

Параметры

Параметр Описание

У метода popRoute нет параметров.

Пример

Давайте снимем верхнюю page в popRoute:

import 'package:flutter/material.dart'; class PopCfg {} class PopDelegate extends RouterDelegate<PopCfg> with ChangeNotifier { var pages = <Page<void>>[ const MaterialPage<void>( child: Scaffold( body: Center(child: Text('abcde')), ), ), const MaterialPage<void>( child: Scaffold( body: Center(child: Text('12345')), ), ), ]; @override Widget build(BuildContext context) { return Navigator( pages: pages, onPopPage: (route, result) { if (!route.didPop(result)) { return false; } return true; }, ); } @override Future<bool> popRoute() async { if (pages.length <= 1) { return true; } pages = pages.sublist(0, pages.length - 1); notifyListeners(); return true; } @override Future<void> setNewRoutePath(PopCfg configuration) async {} @override PopCfg get currentConfiguration => PopCfg(); } class PopParser extends RouteInformationParser<PopCfg> { @override Future<PopCfg> parseRouteInformation( RouteInformation routeInformation, ) async { return PopCfg(); } @override RouteInformation restoreRouteInformation(PopCfg configuration) { return RouteInformation(uri: Uri.parse('/')); } } void main() { runApp( MaterialApp.router( routerDelegate: PopDelegate(), routeInformationParser: PopParser(), ), ); }

На экране виден верхний '12345'; popRoute уберёт его и покажет 'abcde'.

Пример

Давайте вернём true, когда pop не нужен:

import 'package:flutter/material.dart'; class IdleDelegate extends RouterDelegate<PopCfg> with ChangeNotifier { @override Widget build(BuildContext context) { return Navigator( pages: const [ MaterialPage<void>( child: Scaffold( body: Center(child: Text('abcde')), ), ), ], onPopPage: (route, result) => route.didPop(result), ); } @override Future<bool> popRoute() async => true; @override Future<void> setNewRoutePath(PopCfg configuration) async {} @override PopCfg get currentConfiguration => PopCfg(); } void main() { runApp( MaterialApp.router( routerDelegate: IdleDelegate(), routeInformationParser: PopParser(), ), ); }

На экране один маршрут; popRoute вернёт true и приложение не упадёт на системном назад.

Смотрите также

  • метод pop,
    императивный pop в Navigator
  • виджет PopScope,
    контроль pop на уровне экрана
  • метод build,
    перестраивает pages после popRoute
  • класс RouterDelegate,
    владелец popRoute
← →
↑
Мы используем cookie для работы сайта, аналитики и персонализации. Обработка данных происходит согласно Политике конфиденциальности.
принять все настроить отклонить