flutter页面添加透明遮罩

flutter页面添加透明遮罩_第1张图片
路由工具

import 'package:test/main.dart';
import 'package:flutter/material.dart';

import 'circle_page_route.dart';

class NavigatorUtil {
  static push(Widget page, {BuildContext context}) {
    return Navigator.push(context ?? navigatorKey.currentContext,
        MaterialPageRoute(builder: (context) => page));
  }

  /// 透明页面
  static pushTransparentRoute(Widget page, {BuildContext context}) {
    return Navigator.push(context ?? navigatorKey.currentContext,
        TransparentRoute(builder: (context) => page));
  }

  /// 透明关闭页面
  static pushTransparentReplacement(Widget page, {BuildContext context}) {
    return Navigator.pushReplacement(context ?? navigatorKey.currentContext,
        TransparentRoute(builder: (context) => page));
  }

  /// 关闭所有页面
  static pushReplacement(
    Widget page, {
    BuildContext context,
  }) {
    Navigator.pushReplacement(context ?? navigatorKey.currentContext,
        MaterialPageRoute(builder: (context) => page));
  }

  /// 跳转并且无法返回到上一页
  static pushAndNoBack(Widget page, {BuildContext context}) {
    return Navigator.pushAndRemoveUntil(
        context ?? navigatorKey.currentContext,
        MaterialPageRoute(builder: (context) => page),
        (route) => route == null);
  }

  /// 返回上一页
  static pop<T extends Object>({BuildContext context, T value}) {
    Navigator.pop(
        context == null ? navigatorKey.currentContext : context, value);
  }

  /// 删除指定路由
  static removeRoute(Widget page, {BuildContext context}) {
    return Navigator.removeRoute(context ?? navigatorKey.currentContext,
        MaterialPageRoute(builder: (context) => page));
  }
}

路由

//透明路由
class TransparentRoute extends PageRoute<void> {

  TransparentRoute({
     this.builder,
  });

  final WidgetBuilder builder;

  
  Duration get transitionDuration => Duration(milliseconds: 100);

  
  bool get opaque => false;

  
  bool get barrierDismissible => false;

  
  Color get barrierColor => Colors.white.withOpacity(0.1);

  
  String get barrierLabel => null;

  
  bool get maintainState => true;

  
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return Container(
      child: builder(context),
    );
  }
}
// 孔雀开屏动画效果路由
class CirclePageRoute extends PageRoute {
  CirclePageRoute({
     this.builder,
  });

  final WidgetBuilder builder;

  
  Duration get transitionDuration => Duration(milliseconds: 500);

  
  bool get opaque => false;

  
  bool get barrierDismissible => false;

  
  Color get barrierColor => Colors.white.withOpacity(0.1);

  
  String get barrierLabel => null;

  
  bool get maintainState => true;

  
  Widget buildPage(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation) {
    return Container(
      child: builder(context),
    );
  }

  
  Widget buildTransitions(BuildContext context, Animation<double> animation,
      Animation<double> secondaryAnimation, Widget child) {
    return AnimatedBuilder(
      animation: animation,
      builder: (context, child) {
        return ClipPath(
          clipper: CirclePath(animation.value),
          child: child,
        );
      },
      child: builder(context),
    );
  }
}

透明遮罩页面显示

//在需要遮罩的页面的initState()函数中使用

  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((_) {
     NavigatorUtil.pushTransparentRoute(PushArticleGuidePage(
          itemType: 1,
        ));
    });
  }

你可能感兴趣的:(flutter)