import 'dart:math' as math; import 'dart:ui' as ui; import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; import 'glass_refraction.dart'; class GlassDock extends StatefulWidget { const GlassDock({ super.key, required this.selectedIndex, required this.onSelected, }); final int selectedIndex; final ValueChanged onSelected; @override State createState() => _GlassDockState(); } class _GlassDockState extends State with TickerProviderStateMixin { static const _height = 58.0, _inset = 3.0; double _width = 264; double get _travel => (_width - _inset * 2) / 3; static const _blue = Color(0xFF007AFF); late final AnimationController _motion = AnimationController.unbounded( vsync: this, value: widget.selectedIndex.toDouble(), ); late final AnimationController _press = AnimationController.unbounded( vsync: this, value: 0, ); late final AnimationController _tap = AnimationController( vsync: this, duration: const Duration(milliseconds: 460), ); late final AnimationController _sway = AnimationController.unbounded( vsync: this, ); void _swayTo(double target) { if (_reduceMotion) { _sway.value = 0; return; } _sway.animateWith( SpringSimulation( const SpringDescription(mass: 1, stiffness: 220, damping: 18), _sway.value, target, _sway.velocity.clamp(-12, 12), ), ); } bool _reduceMotion = false; late int _targetIndex; @override void initState() { super.initState(); _targetIndex = widget.selectedIndex; } double get _pressure => _press.value.clamp(0.0, 1.18); void _pressTo(double target) { if (_reduceMotion) { _press.value = target; return; } _press.animateWith( SpringSimulation( const SpringDescription(mass: 1, stiffness: 440, damping: 17), _press.value, target, _press.velocity, ), ); } @override void didChangeDependencies() { super.didChangeDependencies(); _reduceMotion = MediaQuery.disableAnimationsOf(context); if (_reduceMotion) { _motion.value = widget.selectedIndex.toDouble(); _press.value = 0; _tap.value = 0; _sway.value = 0; } } double? _drag; double _dragSpeed = 0; double get _position => _drag ?? _motion.value.clamp(-0.12, 2.12); void _moveTo(int index, {double velocity = 0}) { _targetIndex = index; if (_reduceMotion) { _motion.value = index.toDouble(); return; } _motion.animateWith( SpringSimulation( const SpringDescription(mass: 1, stiffness: 360, damping: 26), _position, index.toDouble(), velocity, ), ); } void _select(int index, {bool elastic = false, double velocity = 0}) { final position = _position; final releaseVelocity = elastic ? velocity.clamp(-8.0, 8.0) : _motion.velocity; setState(() => _drag = null); _motion.value = position; _moveTo(index, velocity: releaseVelocity); // Retarget the position spring without resetting an in-flight size pulse. // Restarting that pulse at its peak made the glass edges jump on quick taps. if (!elastic && !_reduceMotion && !_tap.isAnimating) { _tap.forward(from: 0); } _pressTo(0); _swayTo(0); widget.onSelected(index); } @override void didUpdateWidget(GlassDock oldWidget) { super.didUpdateWidget(oldWidget); // The parent acknowledges onSelected with a rebuild. Restarting here used // to erase the release velocity and start a second spring for the same tab. if (widget.selectedIndex != _targetIndex && _drag == null) { _moveTo(widget.selectedIndex, velocity: _motion.velocity); } } @override void dispose() { _motion.dispose(); _press.dispose(); _tap.dispose(); _sway.dispose(); super.dispose(); } Widget _glass({required Widget child, bool lens = false}) { final pressure = _pressure.clamp(0.0, 1.0); final content = CustomPaint( foregroundPainter: _GlassRimPainter(lens: lens, pressure: pressure), child: DecoratedBox( decoration: BoxDecoration( gradient: LinearGradient( begin: Alignment.topLeft, end: Alignment.bottomRight, colors: lens ? [ Color.lerp( const Color(0xFFA1A4AA).withValues(alpha: 0.34), const Color(0xFFD8DBE0).withValues(alpha: 0.0252), pressure, )!, Color.lerp( const Color(0xFF8E939B).withValues(alpha: 0.26), const Color(0xFFC3C7CE).withValues(alpha: 0.0144), pressure, )!, Color.lerp( const Color(0xFFB4B7BC).withValues(alpha: 0.32), const Color(0xFFE5E7EB).withValues(alpha: 0.0216), pressure, )!, ] : [ Colors.white.withValues(alpha: 0.24), const Color(0xFFF9FBFF).withValues(alpha: 0.12), Colors.white.withValues(alpha: 0.18), ], ), ), child: child, ), ); return ClipRRect( borderRadius: BorderRadius.circular(40), child: GlassRefraction( strength: lens ? 0.78 + pressure * 0.70 : 1.05, blurSigma: lens ? 1.6 - pressure * 0.6 : 1.5, child: content, ), ); } @override Widget build(BuildContext context) => LayoutBuilder( builder: (context, constraints) { _width = math.min( constraints.maxWidth, MediaQuery.sizeOf(context).width >= 600 ? 300.0 : 264.0, ); return AnimatedBuilder( animation: Listenable.merge([_motion, _press, _tap, _sway]), builder: (context, _) { final press = _pressure; final pulse = _reduceMotion ? 0.0 : math.sin(math.pi * _tap.value); final speed = _reduceMotion ? 0.0 : (_drag != null ? _dragSpeed : _motion.velocity).abs(); final stretch = (speed / 80).clamp(0.0, 0.08) + pulse * 0.10 + press * 0.30; final lensWidth = _travel * (1 + stretch); final lensHeight = (_height - _inset * 2) * (1 + press * 0.31 + pulse * 0.08 - (speed / 180).clamp(0.0, 0.035)); // Resize real glass bounds: sampling stays aligned without scaling text. final sway = _reduceMotion ? 0.0 : _sway.value.clamp(-1.2, 1.2); final bodyGrowth = press * 12 + pulse * 3 + sway.abs() * 4; final bodyHeight = _height + press * 5 + pulse * 1.5 - sway.abs() * 0.8; final bodyShift = (_position - 1).clamp(-1.0, 1.0) * press * 2 + sway * 2.5; return SizedBox( width: _width, height: _height, child: DecoratedBox( decoration: BoxDecoration( borderRadius: BorderRadius.circular(40), boxShadow: [ BoxShadow( color: const Color(0xFF4C6078).withValues(alpha: 0.075), blurRadius: 18, offset: const Offset(0, 6), ), ], ), child: Stack( clipBehavior: Clip.none, children: [ Positioned( left: -bodyGrowth / 2 + bodyShift, top: (_height - bodyHeight) / 2, width: _width + bodyGrowth, height: bodyHeight, child: _glass(child: const SizedBox.expand()), ), Positioned( left: (_inset + _position * _travel - (lensWidth - _travel) / 2) .clamp(1.0, _width - lensWidth - 1), top: (_height - lensHeight) / 2, width: lensWidth, height: lensHeight, child: IgnorePointer( child: _glass(lens: true, child: const SizedBox.expand()), ), ), Positioned( left: _inset, right: _inset, top: _inset, bottom: _inset, child: Row( children: [ _item(0, Icons.calendar_month_rounded, '课表'), _item(1, Icons.assignment_rounded, '考试'), _item(2, Icons.person_rounded, '个人设置', caption: '我的'), ], ), ), ], ), ), ); }, ); }, ); Widget _item(int index, IconData icon, String label, {String? caption}) => Expanded( child: Semantics( label: label, button: true, selected: index == widget.selectedIndex, excludeSemantics: true, onTap: () => _select(index), child: GestureDetector( key: index == widget.selectedIndex ? const Key('dock-cursor') : null, behavior: HitTestBehavior.opaque, onTapDown: index == widget.selectedIndex ? (_) => _pressTo(_reduceMotion ? 0 : 1) : null, onTapCancel: () { if (_drag == null) _pressTo(0); }, onTap: () => _select(index), onHorizontalDragStart: index == widget.selectedIndex ? (_) { final start = _position.clamp(0.0, 2.0); _motion.stop(); _pressTo(_reduceMotion ? 0 : 1); setState(() { _drag = start; _dragSpeed = 0; }); } : null, onHorizontalDragUpdate: index == widget.selectedIndex ? (details) => setState(() { _dragSpeed = details.delta.dx / _travel * 60; _swayTo((_dragSpeed / 8).clamp(-1.0, 1.0)); _drag = (_position + details.delta.dx / _travel).clamp( 0.0, 2.0, ); }) : null, onHorizontalDragEnd: index == widget.selectedIndex ? (details) => _select( _position.round().clamp(0, 2), elastic: true, velocity: details.velocity.pixelsPerSecond.dx / _travel, ) : null, onHorizontalDragCancel: index == widget.selectedIndex ? () { if (!mounted) return; final position = _position; setState(() => _drag = null); _motion.value = position; _moveTo(widget.selectedIndex); _pressTo(0); _swayTo(0); } : null, child: Center( child: Column( mainAxisSize: MainAxisSize.min, children: [ AnimatedScale( scale: index == _position.round().clamp(0, 2) ? 1 + _pressure * 0.08 : 1, duration: const Duration(milliseconds: 130), child: Icon( icon, size: 25, color: index == _position.round().clamp(0, 2) ? _blue : _blue.withValues(alpha: 0.72), shadows: [ Shadow( color: Colors.white.withValues( alpha: index == _position.round().clamp(0, 2) ? 0.60 : 0.38, ), blurRadius: 8, ), Shadow( color: Colors.white.withValues( alpha: index == _position.round().clamp(0, 2) ? 0.95 : 0.85, ), blurRadius: 2.8, ), Shadow( color: Colors.white.withValues( alpha: index == _position.round().clamp(0, 2) ? 0.34 : 0.22, ), blurRadius: 1.4, ), ], ), ), const SizedBox(height: 2), Flexible( child: FittedBox( fit: BoxFit.scaleDown, child: Text( caption ?? label, maxLines: 1, style: TextStyle( fontSize: 11, height: 1.1, fontWeight: FontWeight.w600, color: index == _position.round().clamp(0, 2) ? _blue : _blue.withValues(alpha: 0.62), ), ), ), ), ], ), ), ), ), ); } /// A white, rounded bevel catches light around a translucent milky body. /// Highlights stay at the edge; the backdrop remains visible beneath the glass. class _GlassRimPainter extends CustomPainter { const _GlassRimPainter({required this.lens, required this.pressure}); final bool lens; final double pressure; @override void paint(Canvas canvas, Size size) { final edge = lens ? const Color(0xFF7B838F) : const Color(0xFF9CAABD); // A quiet grey selection at rest; the glossy lens rim returns on touch. final opacity = lens ? 0.25 + pressure * 0.75 : 1.0; final bounds = Offset.zero & size; final outer = RRect.fromRectAndRadius( bounds.deflate(0.7), Radius.circular(size.height / 2), ); final inner = RRect.fromRectAndRadius( bounds.deflate(2.2), Radius.circular(size.height / 2 - 1.5), ); canvas.drawRRect( outer, Paint() ..style = PaintingStyle.stroke ..strokeWidth = lens ? 1.8 : 2.8 ..maskFilter = MaskFilter.blur(BlurStyle.normal, lens ? 0.9 : 0.65) ..shader = ui.Gradient.linear( bounds.topLeft, bounds.bottomRight, [ Colors.white.withValues(alpha: (lens ? 0.24 : 0.42) * opacity), edge.withValues(alpha: 0.04 * opacity), Colors.white.withValues(alpha: (lens ? 0.14 : 0.30) * opacity), ], [0.0, 0.55, 1.0], ), ); canvas.drawRRect( outer, Paint() ..style = PaintingStyle.stroke ..strokeWidth = lens ? 1.25 : 1.1 ..shader = ui.Gradient.sweep( bounds.center, [ Colors.white.withValues(alpha: 0.96 * opacity), edge.withValues(alpha: (lens ? 0.30 : 0.15) * opacity), Colors.white.withValues(alpha: 0.10 * opacity), Colors.white.withValues(alpha: 0.94 * opacity), edge.withValues(alpha: 0.12 * opacity), Colors.white.withValues(alpha: 0.96 * opacity), ], [0.0, 0.18, 0.38, 0.60, 0.80, 1.0], TileMode.clamp, -math.pi / 2, math.pi * 1.5, ), ); canvas.drawRRect( inner, Paint() ..style = PaintingStyle.stroke ..strokeWidth = 0.7 ..shader = ui.Gradient.linear( bounds.topLeft, bounds.bottomRight, [ Colors.white.withValues(alpha: (lens ? 0.20 : 0.34) * opacity), edge.withValues(alpha: (lens ? 0.13 : 0.07) * opacity), Colors.white.withValues(alpha: (lens ? 0.28 : 0.40) * opacity), ], [0.0, 0.5, 1.0], ), ); final shine = Path() ..moveTo(size.height * 0.32, 4) ..quadraticBezierTo( size.width * 0.5, 1.8, size.width - size.height * 0.35, 4, ); canvas.drawPath( shine, Paint() ..style = PaintingStyle.stroke ..strokeCap = StrokeCap.round ..strokeWidth = 0.8 ..shader = ui.Gradient.linear( Offset.zero, Offset(size.width, 0), [ Colors.white.withValues(alpha: 0), Colors.white.withValues(alpha: 0.50 * opacity), Colors.white.withValues(alpha: 0), ], [0.0, 0.5, 1.0], ), ); } @override bool shouldRepaint(_GlassRimPainter oldDelegate) => oldDelegate.lens != lens || oldDelegate.pressure != pressure; }