本来想将悬浮组件放在顶部,发现没有修改之前使用 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;
}