您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
120 行
4.5 KiB
120 行
4.5 KiB
using System;
|
|
using Unity.UIWidgets.animation;
|
|
using Unity.UIWidgets.async;
|
|
using Unity.UIWidgets.foundation;
|
|
using Unity.UIWidgets.ui;
|
|
using Unity.UIWidgets.widgets;
|
|
|
|
namespace Unity.UIWidgets.cupertino {
|
|
class CupertinoScrollbarUtils {
|
|
public static readonly Color _kScrollbarColor = new Color(0x99777777);
|
|
public const float _kScrollbarThickness = 2.5f;
|
|
public const float _kScrollbarMainAxisMargin = 4.0f;
|
|
public const float _kScrollbarCrossAxisMargin = 2.5f;
|
|
public const float _kScrollbarMinLength = 36.0f;
|
|
public const float _kScrollbarMinOverscrollLength = 8.0f;
|
|
public static readonly Radius _kScrollbarRadius = Radius.circular(1.25f);
|
|
public static readonly TimeSpan _kScrollbarTimeToFade = new TimeSpan(0, 0, 0, 0, 50);
|
|
public static readonly TimeSpan _kScrollbarFadeDuration = new TimeSpan(0, 0, 0, 0, 250);
|
|
}
|
|
|
|
public class CupertinoScrollbar : StatefulWidget {
|
|
public CupertinoScrollbar(
|
|
Widget child,
|
|
Key key = null
|
|
) : base(key: key) {
|
|
this.child = child;
|
|
}
|
|
|
|
public readonly Widget child;
|
|
|
|
public override State createState() {
|
|
return new _CupertinoScrollbarState();
|
|
}
|
|
}
|
|
|
|
class _CupertinoScrollbarState : TickerProviderStateMixin<CupertinoScrollbar> {
|
|
ScrollbarPainter _painter;
|
|
TextDirection _textDirection;
|
|
AnimationController _fadeoutAnimationController;
|
|
Animation<float> _fadeoutOpacityAnimation;
|
|
Timer _fadeoutTimer;
|
|
|
|
public override void initState() {
|
|
base.initState();
|
|
_fadeoutAnimationController = new AnimationController(
|
|
vsync: this,
|
|
duration: CupertinoScrollbarUtils._kScrollbarFadeDuration
|
|
);
|
|
_fadeoutOpacityAnimation = new CurvedAnimation(
|
|
parent: _fadeoutAnimationController,
|
|
curve: Curves.fastOutSlowIn
|
|
);
|
|
}
|
|
|
|
|
|
public override void didChangeDependencies() {
|
|
base.didChangeDependencies();
|
|
_textDirection = Directionality.of(context);
|
|
_painter = _buildCupertinoScrollbarPainter();
|
|
}
|
|
|
|
ScrollbarPainter _buildCupertinoScrollbarPainter() {
|
|
return new ScrollbarPainter(
|
|
color: CupertinoScrollbarUtils._kScrollbarColor,
|
|
textDirection: _textDirection,
|
|
thickness: CupertinoScrollbarUtils._kScrollbarThickness,
|
|
fadeoutOpacityAnimation: _fadeoutOpacityAnimation,
|
|
mainAxisMargin: CupertinoScrollbarUtils._kScrollbarMainAxisMargin,
|
|
crossAxisMargin: CupertinoScrollbarUtils._kScrollbarCrossAxisMargin,
|
|
radius: CupertinoScrollbarUtils._kScrollbarRadius,
|
|
minLength: CupertinoScrollbarUtils._kScrollbarMinLength,
|
|
minOverscrollLength: CupertinoScrollbarUtils._kScrollbarMinOverscrollLength
|
|
);
|
|
}
|
|
|
|
bool _handleScrollNotification(ScrollNotification notification) {
|
|
if (notification is ScrollUpdateNotification ||
|
|
notification is OverscrollNotification) {
|
|
if (_fadeoutAnimationController.status != AnimationStatus.forward) {
|
|
_fadeoutAnimationController.forward();
|
|
}
|
|
|
|
_fadeoutTimer?.cancel();
|
|
_painter.update(notification.metrics, notification.metrics.axisDirection);
|
|
}
|
|
else if (notification is ScrollEndNotification) {
|
|
_fadeoutTimer?.cancel();
|
|
_fadeoutTimer = Window.instance.run(CupertinoScrollbarUtils._kScrollbarTimeToFade, () => {
|
|
_fadeoutAnimationController.reverse();
|
|
_fadeoutTimer = null;
|
|
});
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
|
|
public override void dispose() {
|
|
_fadeoutAnimationController.dispose();
|
|
_fadeoutTimer?.cancel();
|
|
_painter.dispose();
|
|
base.dispose();
|
|
}
|
|
|
|
|
|
public override Widget build(BuildContext context) {
|
|
return new NotificationListener<ScrollNotification>(
|
|
onNotification: _handleScrollNotification,
|
|
child: new RepaintBoundary(
|
|
child: new CustomPaint(
|
|
foregroundPainter: _painter,
|
|
child: new RepaintBoundary(
|
|
child: widget.child
|
|
)
|
|
)
|
|
)
|
|
);
|
|
}
|
|
}
|
|
}
|