import 'package:flutter/cupertino.dart';

class YJPush {
  static Future presentWidget(BuildContext context, Widget widget) {
    return Navigator.of(context).push(PageRouteBuilder(
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
          return widget;
        },
        transitionsBuilder: (
          BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child,
        ) {
          // 添加一个平移动画
          return YJPush.createTransition(animation, child);
        },
        opaque: false,
        barrierDismissible: true));
  }

  static void presentWidgetNoAnimation(BuildContext context, Widget widget) {
    Navigator.of(context).push(PageRouteBuilder(
        pageBuilder: (BuildContext context, Animation<double> animation,
            Animation<double> secondaryAnimation) {
          return widget;
        },
        transitionsBuilder: (
          BuildContext context,
          Animation<double> animation,
          Animation<double> secondaryAnimation,
          Widget child,
        ) {
          // 添加一个平移动画
          return YJPush.createNoAnimationTransition(animation, child);
        },
        opaque: false,
        barrierDismissible: true));
  }

  /// 创建一个平移变换
  /// 跳转过去查看源代码，可以看到有各种各样定义好的变换
  static SlideTransition createTransition(
      Animation<double> animation, Widget child) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0.0, 1.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  }

  /// 创建一个平移变换 没有动画
  static SlideTransition createNoAnimationTransition(
      Animation<double> animation, Widget child) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(0.0, 0.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  }

  static Future pushWidget(BuildContext context, Widget widget) async {
    Route route = CupertinoPageRoute(builder: (context) => widget);
    return await Navigator.push(context, route);
  }

  static Future pushAndRemoveWidget(BuildContext context, Widget widget) async {
    Route route = CupertinoPageRoute(builder: (context) => widget);
    return await Navigator.pushAndRemoveUntil(context, route, (route) => false);
  }

  static SlideTransition createHTransition(
      Animation<double> animation, Widget child) {
    return SlideTransition(
      position: Tween<Offset>(
        begin: const Offset(1.0, 0.0),
        end: const Offset(0.0, 0.0),
      ).animate(animation),
      child: child, // child is the value returned by pageBuilder
    );
  }
}
