111 lines
2.7 KiB
Dart
111 lines
2.7 KiB
Dart
/// ============================================================
|
||
/// 闲言APP — Rive Tab 动画组件
|
||
/// 创建时间: 2026-05-29
|
||
/// 更新时间: 2026-05-29
|
||
/// 作用: 使用Rive动画替代TabIconSprite,实现更丰富的角色动画
|
||
/// 上次更新: 迁移至 Rive 0.14.x API
|
||
/// ============================================================
|
||
|
||
import 'package:flutter/cupertino.dart';
|
||
import 'package:rive/rive.dart';
|
||
|
||
/// Rive Tab 动画图标组件
|
||
///
|
||
/// 通过 Rive StateMachine 控制选中/未选中状态切换,
|
||
/// 作为 TabIconSprite 的可选替代方案。
|
||
/// 需要提供 .riv 资源文件,StateMachine 中需包含
|
||
/// 名为 `isSelected` 的布尔输入。
|
||
class RiveTabIcon extends StatefulWidget {
|
||
const RiveTabIcon({
|
||
required this.isSelected,
|
||
required this.assetPath,
|
||
this.stateMachineName = 'State',
|
||
this.size = 32,
|
||
super.key,
|
||
});
|
||
|
||
/// 是否选中
|
||
final bool isSelected;
|
||
|
||
/// Rive 资源路径 (如 'assets/animations/tab_home.riv')
|
||
final String assetPath;
|
||
|
||
/// StateMachine 名称
|
||
final String stateMachineName;
|
||
|
||
/// 图标尺寸
|
||
final double size;
|
||
|
||
@override
|
||
State<RiveTabIcon> createState() => _RiveTabIconState();
|
||
}
|
||
|
||
class _RiveTabIconState extends State<RiveTabIcon> {
|
||
RiveWidgetController? _controller;
|
||
File? _file;
|
||
bool _isLoading = true;
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_loadRive();
|
||
}
|
||
|
||
@override
|
||
void dispose() {
|
||
_controller?.dispose();
|
||
_file?.dispose();
|
||
super.dispose();
|
||
}
|
||
|
||
Future<void> _loadRive() async {
|
||
try {
|
||
final file = await File.asset(
|
||
widget.assetPath,
|
||
riveFactory: Factory.rive,
|
||
);
|
||
if (!mounted) return;
|
||
_file = file;
|
||
_controller = RiveWidgetController(
|
||
file!,
|
||
stateMachineSelector: StateMachineSelector.byName(
|
||
widget.stateMachineName,
|
||
),
|
||
);
|
||
_syncSelection();
|
||
setState(() => _isLoading = false);
|
||
} catch (e) {
|
||
if (mounted) setState(() => _isLoading = false);
|
||
}
|
||
}
|
||
|
||
void _syncSelection() {
|
||
// ignore: deprecated_member_use
|
||
_controller?.stateMachine.boolean('isSelected')?.value = widget.isSelected;
|
||
}
|
||
|
||
@override
|
||
void didUpdateWidget(RiveTabIcon oldWidget) {
|
||
super.didUpdateWidget(oldWidget);
|
||
if (oldWidget.isSelected != widget.isSelected) {
|
||
_syncSelection();
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
if (_isLoading || _controller == null) {
|
||
return SizedBox(
|
||
width: widget.size,
|
||
height: widget.size,
|
||
child: const CupertinoActivityIndicator(radius: 8),
|
||
);
|
||
}
|
||
return SizedBox(
|
||
width: widget.size,
|
||
height: widget.size,
|
||
child: RiveWidget(controller: _controller!),
|
||
);
|
||
}
|
||
}
|