Transparent PageRoute in Flutter for displaying a (semi-) transparent page

拥有回忆 提交于 2019-12-19 17:24:19

问题


Would it be possible to have a page route with a transparent background so I can show a (semi-)transparent page on top of an existing page?


回答1:


Yes, definitely! One solution would be to extend PageRoute and make the opaque getter return false. A possible solution could look like the following:

import 'package:flutter/widgets.dart';

class TransparentRoute extends PageRoute<void> {
  TransparentRoute({
    @required this.builder,
    RouteSettings settings,
  })  : assert(builder != null),
        super(settings: settings, fullscreenDialog: false);

  final WidgetBuilder builder;

  @override
  bool get opaque => false;

  @override
  Color get barrierColor => null;

  @override
  String get barrierLabel => null;

  @override
  bool get maintainState => true;

  @override
  Duration get transitionDuration => Duration(milliseconds: 350);

  @override
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    final result = builder(context);
    return FadeTransition(
      opacity: Tween<double>(begin: 0, end: 1).animate(animation),
      child: Semantics(
        scopesRoute: true,
        explicitChildNodes: true,
        child: result,
      ),
    );
  }
}

Keep in mind that this would also create a custom transition animation and behave differently than the more complex MaterialPageRoute (e.g. the swipe-back gesture would not work with the current implementation on iOS).

You could use it like this:

Navigator.of(context).push(
    TransparentRoute(builder: (BuildContext context) => YourSecondPage())
);

For more info on the PageRoute and the different implementers, head over to https://docs.flutter.io/flutter/widgets/PageRoute-class.html



来源:https://stackoverflow.com/questions/54572107/transparent-pageroute-in-flutter-for-displaying-a-semi-transparent-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!