Flutter修改FloatingActionButton的位置以及动画

本来想将悬浮组件放在顶部,发现没有修改之前使用 FloatingActionButtonLocation的top几个属性都是左边有一点边距,不符合我的要求,所以下面通过改源码去掉即可。

 //audio floating
  static const FloatingActionButtonLocation topFloat = TopFloatingActionButtonLocation();

child: Scaffold(
           backgroundColor: Colors.white,
          floatingActionButtonAnimator: FabMotionAnimator(),
          floatingActionButtonLocation: TopFloatingActionButtonLocation(),
          floatingActionButton: _buildPlayControl(),

将需要的源代码从FloatingActionButtonLocation复制出来修改即可



import 'package:flutter/material.dart';



double _leftOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
  return kFloatingActionButtonMargin
      + scaffoldGeometry.minInsets.left
      - offset;
}

double _rightOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
  return scaffoldGeometry.scaffoldSize.width
      - kFloatingActionButtonMargin
      - scaffoldGeometry.minInsets.right
      - scaffoldGeometry.floatingActionButtonSize.width
      + offset;
}

double _endOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
  assert(scaffoldGeometry.textDirection != null);
  switch (scaffoldGeometry.textDirection) {
    case TextDirection.rtl:
      return _leftOffset(scaffoldGeometry, offset: offset);
    case TextDirection.ltr:
      return _rightOffset(scaffoldGeometry, offset: offset);
  }
  return null;
}

double _startOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
  assert(scaffoldGeometry.textDirection != null);
  switch (scaffoldGeometry.textDirection) {
    case TextDirection.rtl:
      return _rightOffset(scaffoldGeometry, offset: offset);
    case TextDirection.ltr:
      return _leftOffset(scaffoldGeometry, offset: offset);
  }
  return null;
}

class TopFloatingActionButtonLocation extends FloatingActionButtonLocation {
  const TopFloatingActionButtonLocation();

  double _rightOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
    return scaffoldGeometry.scaffoldSize.width
        - kFloatingActionButtonMargin
        - scaffoldGeometry.minInsets.right
        - scaffoldGeometry.floatingActionButtonSize.width
        + offset;
  }

  double _startOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
    assert(scaffoldGeometry.textDirection != null);
    switch (scaffoldGeometry.textDirection) {
      case TextDirection.rtl:
        return _rightOffset(scaffoldGeometry, offset: offset);
      case TextDirection.ltr:
        return _leftOffset(scaffoldGeometry, offset: offset);
    }
    return null;
  }

  double _leftOffset(ScaffoldPrelayoutGeometry scaffoldGeometry, { double offset = 0.0 }) {
    return 0.0
        + scaffoldGeometry.minInsets.left
        - offset;
  }

//  double _straddleAppBar(ScaffoldPrelayoutGeometry scaffoldGeometry) {
//    final double fabHalfHeight = scaffoldGeometry.floatingActionButtonSize.height / 2.0;
//    return scaffoldGeometry.contentTop;
//  }

  @override
  Offset getOffset(ScaffoldPrelayoutGeometry scaffoldGeometry) {
    return Offset(0.0, scaffoldGeometry.contentTop);
  }

  @override
  String toString() => 'FloatingActionButtonLocation.startTop';
}

class FabMotionAnimator extends FloatingActionButtonAnimator {
  const FabMotionAnimator();

  @override
  Offset getOffset({ Offset begin, Offset end, double progress }) {
    if (progress < 0.5) {
      return begin;
    } else {
      return end;
    }
  }

  @override
  Animation<double> getScaleAnimation({ Animation<double> parent }) {
    const Curve curve = Interval(1.0, 1.0, curve: Curves.ease);
    return _AnimationSwap<double>(
      ReverseAnimation(parent.drive(CurveTween(curve: curve.flipped))),
      parent.drive(CurveTween(curve: curve)),
      parent,
      0.5,
    );
  }

  // Because we only see the last half of the rotation tween,
  // it needs to go twice as far.
  static final Animatable<double> _rotationTween = Tween<double>(
    begin: 1.0 - kFloatingActionButtonTurnInterval * 2.0,
    end: 1.0,
  );

  @override
  Animation<double> getRotationAnimation({Animation<double> parent}) {
    // TODO: implement getRotationAnimation
    return null;
  }
}

class _AnimationSwap<T> extends CompoundAnimation<T> {
  /// Creates an [_AnimationSwap].
  ///
  /// Both arguments must be non-null. Either can be an [_AnimationSwap] itself
  /// to combine multiple animations.
  _AnimationSwap(Animation<T> first, Animation<T> next, this.parent, this.swapThreshold) : super(first: first, next: next);

  final Animation<double> parent;
  final double swapThreshold;

  @override
  T get value => parent.value < swapThreshold ? first.value : next.value;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值