feat(flutter): 增加关键操作震动反馈

This commit is contained in:
Zhou Weidong committed 2026-10-17 16:15:46 +08:00
1 parent e609e3ad8d
commit 8a481d5c62
20 files changed
+308 -72

No files matched your search

+4
View File
@@ -33,6 +33,7 @@ import 'core/data/services/favorite_controller.dart';
import 'core/data/services/interaction_controller.dart';
import 'core/data/services/playback_session.dart';
import 'core/data/services/video_player_port.dart';
import 'core/platform/haptics.dart';
import 'core/theme/app_theme.dart';
import 'l10n/app_localizations.dart';
import 'pages/home/home_view.dart';
@@ -49,6 +50,7 @@ class LumaDramaApp extends StatefulWidget {
this.playbackRepository,
this.commentRepository,
this.interactionRepository,
this.haptics = const FlutterHaptics(),
});
final PlayerFactory playerFactory;
@@ -62,6 +64,7 @@ class LumaDramaApp extends StatefulWidget {
final PlaybackRepository? playbackRepository;
final CommentRepository? commentRepository;
final InteractionRepository? interactionRepository;
final Haptics haptics;
@override
State<LumaDramaApp> createState() => _LumaDramaAppState();
@@ -176,6 +179,7 @@ class _LumaDramaAppState extends State<LumaDramaApp> {
Provider<FavoriteRepository>.value(value: _favorites),
Provider<CommentRepository>.value(value: _commentRepository),
Provider<InteractionRepository>.value(value: _interactionRepository),
Provider<Haptics>.value(value: widget.haptics),
ChangeNotifierProvider(
create: (_) => CatalogController(_catalogRepository)..load(),
),
+39
View File
@@ -0,0 +1,39 @@
// Author: fruit.com.cn
import 'package:flutter/services.dart';
/// User-action haptic feedback boundary.
abstract interface class Haptics {
/// Feedback for a selection change.
Future<void> selectionClick();
/// Feedback for a primary action.
Future<void> lightImpact();
/// Feedback for a destructive action.
Future<void> mediumImpact();
}
/// Default implementation backed by Flutter's platform haptics.
class FlutterHaptics implements Haptics {
const FlutterHaptics();
@override
Future<void> selectionClick() =>
_ignoreUnavailable(HapticFeedback.selectionClick);
@override
Future<void> lightImpact() => _ignoreUnavailable(HapticFeedback.lightImpact);
@override
Future<void> mediumImpact() =>
_ignoreUnavailable(HapticFeedback.mediumImpact);
Future<void> _ignoreUnavailable(Future<void> Function() feedback) async {
try {
await feedback();
} catch (_) {
// Haptics are optional and may be unavailable on the target device.
}
}
}
+4
View File
@@ -1,10 +1,13 @@
// Author: fruit.com.cn
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../l10n/app_localizations.dart';
import '../data/services/download_controller.dart';
import '../platform/haptics.dart';
import 'auth_gate.dart';
/// 详情页与播放页共用的下载授权入口。
@@ -16,6 +19,7 @@ Future<void> requestEpisodeDownload(
) async {
if (!await ensureSignedIn(context)) return;
if (!context.mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await context.read<DownloadController>().grant(episodeId);
if (!context.mounted) return;
final l10n = AppLocalizations.of(context)!;
+4
View File
@@ -1,10 +1,13 @@
// Author: fruit.com.cn
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../../core/data/remote/api_client.dart';
import '../../core/data/services/auth_controller.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../l10n/app_localizations.dart';
@@ -71,6 +74,7 @@ class _AuthPageState extends State<AuthPage> {
setState(() => _logic.errorMessage = _errorText(l10n, invalid));
return;
}
unawaited(context.read<Haptics>().lightImpact());
FocusScope.of(context).unfocus();
setState(() {
_logic.busy = true;
+5
View File
@@ -11,6 +11,7 @@ import '../../core/data/services/auth_controller.dart';
import '../../core/data/services/download_controller.dart';
import '../../core/formatting/error_text.dart';
import '../../core/formatting/localized_date_time.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/auth_gate.dart';
@@ -166,6 +167,7 @@ class _DownloadsBody extends StatelessWidget {
}
Future<void> _remove(BuildContext context, String episodeId) async {
unawaited(context.read<Haptics>().mediumImpact());
final ok = await downloads.remove(episodeId);
if (ok || !context.mounted) return;
_showMessage(context, AppLocalizations.of(context)!.downloadDeleteFailed);
@@ -191,6 +193,9 @@ class _DownloadsBody extends StatelessWidget {
),
);
if (confirmed != true) return;
if (context.mounted) {
unawaited(context.read<Haptics>().mediumImpact());
}
final ok = await downloads.clear();
if (!ok && context.mounted) {
_showMessage(context, l10n.downloadClearFailed);
@@ -11,6 +11,7 @@ import '../../core/data/services/favorite_controller.dart';
import '../../core/data/services/interaction_controller.dart';
import '../../core/formatting/error_text.dart';
import '../../core/formatting/localized_content.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/remote_image.dart';
@@ -315,6 +316,7 @@ class _DramaDetailPageState extends State<DramaDetailPage> {
if (!await ensureSignedIn(context) || !mounted) return;
final draft = await showReportDialog(context, title: l10n.reportDrama);
if (draft == null || !mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await context.read<InteractionController>().reportDrama(
logic.drama.id,
reason: draft.reason,
@@ -341,6 +343,8 @@ class _DramaDetailPageState extends State<DramaDetailPage> {
icon: Icon(saved ? Icons.bookmark : Icons.bookmark_border),
onPressed: () async {
if (!await ensureSignedIn(context)) return;
if (!context.mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final ok = await logic.toggleFavorite();
if (ok || !context.mounted) return;
// 成功靠图标变化反馈;失败必须说清楚(乐观更新已回滚)
+2
View File
@@ -9,6 +9,7 @@ import '../../core/data/models/drama.dart';
import '../../core/data/services/auth_controller.dart';
import '../../core/data/services/favorite_controller.dart';
import '../../core/formatting/error_text.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/auth_gate.dart';
import '../../core/widgets/library_drama_tile.dart';
@@ -142,6 +143,7 @@ class _FavoritesBody extends StatelessWidget {
}
Future<void> _remove(BuildContext context, String dramaId) async {
unawaited(context.read<Haptics>().lightImpact());
final ok = await favorites.remove(dramaId);
if (ok || !context.mounted) return;
final l10n = AppLocalizations.of(context)!;
+21 -4
View File
@@ -1,5 +1,7 @@
// Author: fruit.com.cn
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
@@ -8,6 +10,7 @@ import '../../core/config/app_config.dart';
import '../../core/data/models/drama.dart';
import '../../core/data/services/auth_controller.dart';
import '../../core/data/services/catalog_controller.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/auth_gate.dart';
@@ -203,7 +206,7 @@ class _HomeTabsState extends State<_HomeTabs> {
selectedIcon: Icons.home_rounded,
label: l10n.homeTab,
selected: logic.tab == 1,
onTap: () => logic.select(1),
onTap: () => _selectTab(context, 1),
),
_TabButton(
key: const Key('tab-home'),
@@ -211,7 +214,7 @@ class _HomeTabsState extends State<_HomeTabs> {
selectedIcon: Icons.movie_rounded,
label: l10n.shortsTab,
selected: logic.tab == 0,
onTap: () => logic.select(0),
onTap: () => _selectTab(context, 0),
),
_TabButton(
key: const Key('tab-my-list'),
@@ -219,7 +222,7 @@ class _HomeTabsState extends State<_HomeTabs> {
selectedIcon: Icons.bookmark_rounded,
label: l10n.myListTab,
selected: logic.tab == 2 && !logic.profile,
onTap: logic.openLibrary,
onTap: () => _selectTab(context, 2),
),
_TabButton(
key: const Key('tab-my'),
@@ -227,7 +230,7 @@ class _HomeTabsState extends State<_HomeTabs> {
selectedIcon: Icons.person_rounded,
label: l10n.myTab,
selected: logic.tab == 2 && logic.profile,
onTap: logic.openProfile,
onTap: () => _selectTab(context, 2, myProfile: true),
),
],
),
@@ -236,6 +239,20 @@ class _HomeTabsState extends State<_HomeTabs> {
),
);
}
void _selectTab(BuildContext context, int tab, {bool myProfile = false}) {
final same = tab == 2
? logic.tab == 2 && logic.profile == myProfile
: logic.tab == tab;
if (!same) {
unawaited(context.read<Haptics>().selectionClick());
}
if (tab == 2) {
myProfile ? logic.openProfile() : logic.openLibrary();
} else {
logic.select(tab);
}
}
}
class _TabButton extends StatelessWidget {
+5
View File
@@ -15,6 +15,7 @@ import '../../core/data/services/playback_session.dart';
import '../../core/data/services/video_player_port.dart';
import '../../core/formatting/error_text.dart';
import '../../core/formatting/localized_content.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/auth_gate.dart';
@@ -350,6 +351,8 @@ class _PlayerPageState extends State<PlayerPage> with WidgetsBindingObserver {
// 收藏/点赞要对到具体账号:未登录先引导登录(首页/详情仍可匿名浏览)
onLike: () async {
if (await ensureSignedIn(context)) {
if (!context.mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await interactions.toggleDramaLike(drama.id);
if (!result.isSuccess && context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
@@ -366,6 +369,8 @@ class _PlayerPageState extends State<PlayerPage> with WidgetsBindingObserver {
onSave: () async {
final favorites = context.read<FavoriteController>();
if (await ensureSignedIn(context)) {
if (!context.mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final ok = await favorites.toggle(drama.id, card: drama);
if (ok || !context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
@@ -11,6 +11,7 @@ import '../../../core/data/repositories/comment_repository.dart';
import '../../../core/data/repositories/interaction_repository.dart';
import '../../../core/data/services/comment_controller.dart';
import '../../../core/formatting/error_text.dart';
import '../../../core/platform/haptics.dart';
import '../../../core/theme/app_colors.dart';
import '../../../core/widgets/auth_gate.dart';
import '../../../l10n/app_localizations.dart';
@@ -60,6 +61,7 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
if (_controller.submitting) return;
if (_input.text.trim().isEmpty) return;
if (!await ensureSignedIn(context) || !mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await _controller.publish(_input.text, replyTo: _replyTo);
if (!mounted) return;
if (result.isSuccess) {
@@ -84,6 +86,7 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
Future<void> _like(DramaComment comment) async {
if (!await ensureSignedIn(context) || !mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await _controller.toggleLike(comment);
if (!result.isSuccess && mounted) {
_showMessage(
@@ -116,7 +119,8 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
final result = await _controller.delete(comment);
if (!result.isSuccess && mounted) {
_showMessage(
result.errorMessage ?? AppLocalizations.of(context)!.commentDeleteFailed,
result.errorMessage ??
AppLocalizations.of(context)!.commentDeleteFailed,
);
}
}
@@ -126,6 +130,7 @@ class _DramaCommentsSheetState extends State<DramaCommentsSheet> {
final l10n = AppLocalizations.of(context)!;
final draft = await showReportDialog(context, title: l10n.reportComment);
if (draft == null || !mounted) return;
unawaited(context.read<Haptics>().lightImpact());
final result = await _controller.report(
comment,
reason: draft.reason,
@@ -13,6 +13,7 @@ import '../../core/data/services/auth_controller.dart';
import '../../core/data/services/watch_records_controller.dart';
import '../../core/formatting/error_text.dart';
import '../../core/formatting/localized_date_time.dart';
import '../../core/platform/haptics.dart';
import '../../core/theme/app_colors.dart';
import '../../core/theme/app_theme.dart';
import '../../core/widgets/auth_gate.dart';
@@ -141,6 +142,7 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
}
Future<void> _remove(String dramaId) async {
unawaited(context.read<Haptics>().mediumImpact());
final ok = await controller.remove(dramaId);
if (!ok && mounted) _showFailure('recordDeleteFailed');
}
@@ -164,7 +166,8 @@ class _WatchRecordsBodyState extends State<_WatchRecordsBody> {
],
),
);
if (confirmed != true) return;
if (confirmed != true || !mounted) return;
unawaited(context.read<Haptics>().mediumImpact());
final ok = await controller.clear();
if (!ok && mounted) _showFailure('recordClearFailed');
}