generated from dellevin/template
结构重构
This commit is contained in:
841
lib/pages/movies/movie_detail_page.dart
Normal file
841
lib/pages/movies/movie_detail_page.dart
Normal file
@@ -0,0 +1,841 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as path;
|
||||
import 'package:share_plus/share_plus.dart';
|
||||
import 'package:cross_file/cross_file.dart';
|
||||
import 'package:permission_handler/permission_handler.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import 'movie_reviews_page.dart';
|
||||
import 'movie_posters_page.dart';
|
||||
|
||||
/// 影视详情页 - 极简主义设计
|
||||
class MovieDetailPage extends StatefulWidget {
|
||||
final Movie movie;
|
||||
|
||||
const MovieDetailPage({super.key, required this.movie});
|
||||
|
||||
@override
|
||||
State<MovieDetailPage> createState() => _MovieDetailPageState();
|
||||
}
|
||||
|
||||
class _MovieDetailPageState extends State<MovieDetailPage> {
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
// 页面获得焦点时刷新数据
|
||||
_refreshMovieData();
|
||||
}
|
||||
|
||||
void _refreshMovieData() {
|
||||
final provider = context.read<AppProvider>();
|
||||
// 强制刷新当前影视数据
|
||||
provider.loadMovies();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// 从 Provider 获取最新的 movie 数据,实现动态刷新
|
||||
final movie = context.watch<AppProvider>().movies
|
||||
.where((m) => m.id == widget.movie.id)
|
||||
.firstOrNull ?? widget.movie;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
body: CustomScrollView(
|
||||
slivers: [
|
||||
// 顶部海报区域
|
||||
_buildSliverAppBar(movie),
|
||||
|
||||
// 内容区域
|
||||
SliverToBoxAdapter(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 基本信息
|
||||
_buildBasicInfo(movie),
|
||||
|
||||
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
|
||||
|
||||
// 导演
|
||||
if (movie.directors.isNotEmpty)
|
||||
_buildDirectorsSection(movie),
|
||||
|
||||
// 编剧
|
||||
if (movie.writers.isNotEmpty)
|
||||
_buildWritersSection(movie),
|
||||
|
||||
// 主演
|
||||
if (movie.actors.isNotEmpty)
|
||||
_buildActorsSection(movie),
|
||||
|
||||
// 类型
|
||||
if (movie.genres.isNotEmpty)
|
||||
_buildGenresSection(movie),
|
||||
|
||||
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
|
||||
|
||||
// 简介
|
||||
if (movie.summary != null && movie.summary!.isNotEmpty)
|
||||
_buildSummarySection(movie),
|
||||
|
||||
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
|
||||
|
||||
// 影评和海报墙入口
|
||||
_buildExtraSections(movie),
|
||||
|
||||
const SizedBox(height: 48),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// 底部操作栏
|
||||
bottomNavigationBar: _buildBottomBar(),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建顶部 AppBar
|
||||
Widget _buildSliverAppBar(Movie movie) {
|
||||
final hasPoster = movie.posterPath != null && movie.posterPath!.isNotEmpty;
|
||||
|
||||
return SliverAppBar(
|
||||
expandedHeight: 320,
|
||||
pinned: true,
|
||||
backgroundColor: const Color(0xFFF5F5F5),
|
||||
flexibleSpace: FlexibleSpaceBar(
|
||||
background: _buildPosterSection(movie),
|
||||
),
|
||||
actions: [
|
||||
// 下载海报按钮(仅当有海报时显示)
|
||||
if (hasPoster)
|
||||
Container(
|
||||
margin: const EdgeInsets.all(8),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
),
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.download_outlined, color: Color(0xFF666666)),
|
||||
onPressed: () => _downloadPoster(movie),
|
||||
tooltip: '下载海报',
|
||||
),
|
||||
),
|
||||
// 清空海报按钮(仅当有海报时显示)
|
||||
if (hasPoster)
|
||||
Container(
|
||||
margin: const EdgeInsets.all(8),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
),
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.hide_image_outlined, color: Color(0xFF666666)),
|
||||
onPressed: () => _showClearPosterDialog(movie),
|
||||
tooltip: '清空海报',
|
||||
),
|
||||
),
|
||||
// 编辑按钮
|
||||
Container(
|
||||
margin: const EdgeInsets.all(8),
|
||||
decoration: const BoxDecoration(
|
||||
color: Colors.white,
|
||||
),
|
||||
child: IconButton(
|
||||
icon: const Icon(Icons.edit_outlined, color: Color(0xFF1A1A1A)),
|
||||
onPressed: () => _navigateToEdit(context),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建海报区域
|
||||
Widget _buildPosterSection(Movie movie) {
|
||||
return SizedBox.expand(
|
||||
child: movie.posterPath != null && movie.posterPath!.isNotEmpty
|
||||
? Image.file(
|
||||
File(movie.posterPath!),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _buildPosterPlaceholder(),
|
||||
)
|
||||
: _buildPosterPlaceholder(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPosterPlaceholder() {
|
||||
return const Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.movie_outlined,
|
||||
size: 64,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无海报',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 显示清空海报对话框
|
||||
void _showClearPosterDialog(Movie movie) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('清空海报'),
|
||||
content: const Text('确定要清空海报吗?清空后将使用默认占位图。'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
Navigator.pop(context);
|
||||
final updatedMovie = movie.copyWith(
|
||||
posterPath: null,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
await context.read<AppProvider>().updateMovie(updatedMovie);
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '海报已清空');
|
||||
}
|
||||
},
|
||||
child: const Text('清空', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建基本信息
|
||||
Widget _buildBasicInfo(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 影视名称
|
||||
Text(
|
||||
movie.title,
|
||||
style: const TextStyle(
|
||||
fontSize: 24,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1A1A1A),
|
||||
height: 1.3,
|
||||
),
|
||||
),
|
||||
|
||||
// 别名(显示在主名称下面,用 / 分隔)
|
||||
if (movie.alternateTitles.isNotEmpty) ...[
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
movie.alternateTitles.join(' / '),
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF999999),
|
||||
height: 1.4,
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// 评分和状态
|
||||
Row(
|
||||
children: [
|
||||
if (movie.rating != null) ...[
|
||||
const Icon(
|
||||
Icons.star,
|
||||
size: 20,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
Text(
|
||||
movie.rating!.toStringAsFixed(1),
|
||||
style: const TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
],
|
||||
_buildStatusTag(movie),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// 上映日期
|
||||
if (movie.releaseDate != null)
|
||||
Text(
|
||||
'${movie.releaseDate!.year}年上映',
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 8),
|
||||
|
||||
// 时间信息
|
||||
Text(
|
||||
'添加于 ${_formatDate(movie.createdAt)}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建状态标签
|
||||
Widget _buildStatusTag(Movie movie) {
|
||||
String label;
|
||||
Color color;
|
||||
switch (movie.status) {
|
||||
case 'watched':
|
||||
label = '已看';
|
||||
color = const Color(0xFF1A1A1A);
|
||||
break;
|
||||
case 'watching':
|
||||
label = '在看';
|
||||
color = const Color(0xFF666666);
|
||||
break;
|
||||
case 'want_to_watch':
|
||||
label = '想看';
|
||||
color = const Color(0xFF999999);
|
||||
break;
|
||||
default:
|
||||
label = '未知';
|
||||
color = const Color(0xFFCCCCCC);
|
||||
}
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
color: color,
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Colors.white,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建导演区域
|
||||
Widget _buildDirectorsSection(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'导演',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: movie.directors.map((director) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Text(
|
||||
director,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建编剧区域
|
||||
Widget _buildWritersSection(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'编剧',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: movie.writers.map((writer) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Text(
|
||||
writer,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建主演区域
|
||||
Widget _buildActorsSection(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'主演',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: movie.actors.map((actor) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Text(
|
||||
actor,
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建类型区域
|
||||
Widget _buildGenresSection(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(24, 0, 24, 24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'类型',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
children: movie.genres.map((genre) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Text(
|
||||
genre,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
);
|
||||
}).toList(),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建简介区域
|
||||
Widget _buildSummarySection(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'简介',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
movie.summary!,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
color: Color(0xFF1A1A1A),
|
||||
height: 1.6,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建额外功能区域(影评、海报墙)
|
||||
Widget _buildExtraSections(Movie movie) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'更多',
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF999999),
|
||||
letterSpacing: 1,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
// 影评入口
|
||||
GestureDetector(
|
||||
onTap: () => _navigateToReviews(movie),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.rate_review_outlined,
|
||||
size: 24,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'影评',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
FutureBuilder<int>(
|
||||
future: context.read<AppProvider>().getMovieReviewCount(movie.id),
|
||||
builder: (context, snapshot) {
|
||||
final count = snapshot.data ?? 0;
|
||||
return Text(
|
||||
count > 0 ? '$count 条影评' : '暂无影评',
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
// 海报墙入口
|
||||
GestureDetector(
|
||||
onTap: () => _navigateToPosters(movie),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.photo_library_outlined,
|
||||
size: 24,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'海报墙',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
FutureBuilder<int>(
|
||||
future: context.read<AppProvider>().getMoviePosterCount(movie.id),
|
||||
builder: (context, snapshot) {
|
||||
final count = snapshot.data ?? 0;
|
||||
return Text(
|
||||
count > 0 ? '$count 张海报' : '暂无海报',
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _navigateToReviews(Movie movie) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MovieReviewsPage(movie: movie),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _navigateToPosters(Movie movie) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MoviePostersPage(movie: movie),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建底部操作栏
|
||||
Widget _buildBottomBar() {
|
||||
return Container(
|
||||
decoration: const BoxDecoration(
|
||||
border: Border(
|
||||
top: BorderSide(color: Color(0xFFE5E5E5), width: 0.5),
|
||||
),
|
||||
),
|
||||
child: SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => _navigateToEdit(context),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: const Color(0xFF1A1A1A),
|
||||
side: const BorderSide(color: Color(0xFF1A1A1A)),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
child: const Text('编辑'),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: OutlinedButton(
|
||||
onPressed: () => _showDeleteDialog(context),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: Colors.red,
|
||||
side: const BorderSide(color: Colors.red),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
padding: const EdgeInsets.symmetric(vertical: 12),
|
||||
),
|
||||
child: const Text('删除'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 格式化日期
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
/// 跳转到编辑页面
|
||||
void _navigateToEdit(BuildContext context) {
|
||||
Navigator.pushNamed(context, '/movie-form', arguments: widget.movie).then((_) {
|
||||
context.read<AppProvider>().loadMovies();
|
||||
});
|
||||
}
|
||||
|
||||
/// 显示删除对话框
|
||||
void _showDeleteDialog(BuildContext context) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('确认删除'),
|
||||
content: Text('确定要删除"${widget.movie.title}"吗?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await context.read<AppProvider>().removeMovie(widget.movie.id);
|
||||
if (!mounted) return;
|
||||
Navigator.pop(context);
|
||||
Navigator.pop(context);
|
||||
ToastUtil.show(context, '已删除');
|
||||
},
|
||||
child: const Text('删除', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 下载海报到本地
|
||||
Future<void> _downloadPoster(Movie movie) async {
|
||||
if (movie.posterPath == null || movie.posterPath!.isEmpty) {
|
||||
ToastUtil.show(context, '没有可下载的海报');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final sourceFile = File(movie.posterPath!);
|
||||
if (!await sourceFile.exists()) {
|
||||
ToastUtil.show(context, '海报文件不存在');
|
||||
return;
|
||||
}
|
||||
|
||||
// 生成文件名:影视名称_时间戳_海报.扩展名
|
||||
final timestamp = DateTime.now().millisecondsSinceEpoch;
|
||||
final fileName = '${movie.title}_${timestamp}_海报${path.extension(movie.posterPath!)}';
|
||||
|
||||
// 复制到临时目录
|
||||
final tempDir = await getTemporaryDirectory();
|
||||
final tempFile = File(path.join(tempDir.path, fileName));
|
||||
await sourceFile.copy(tempFile.path);
|
||||
|
||||
// 使用分享功能让用户选择保存位置
|
||||
await Share.shareXFiles(
|
||||
[XFile(tempFile.path)],
|
||||
subject: '${movie.title} 海报',
|
||||
text: '下载自 MookNote',
|
||||
);
|
||||
} catch (e) {
|
||||
ToastUtil.show(context, '下载失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 请求存储权限
|
||||
Future<bool> _requestStoragePermission() async {
|
||||
// Android 13+ 使用新的权限
|
||||
if (Platform.isAndroid) {
|
||||
final sdkInt = await _getAndroidSdkInt();
|
||||
if (sdkInt >= 33) {
|
||||
// Android 13+ 使用 READ_MEDIA_IMAGES
|
||||
final status = await Permission.photos.request();
|
||||
return status.isGranted;
|
||||
} else {
|
||||
// Android 12 及以下使用存储权限
|
||||
var status = await Permission.storage.request();
|
||||
if (status.isDenied) {
|
||||
status = await Permission.storage.request();
|
||||
}
|
||||
return status.isGranted;
|
||||
}
|
||||
}
|
||||
// iOS 不需要额外权限来保存到应用沙盒
|
||||
return true;
|
||||
}
|
||||
|
||||
/// 获取 Android SDK 版本
|
||||
Future<int> _getAndroidSdkInt() async {
|
||||
// 简化处理,实际可以通过 platform channel 获取
|
||||
// 这里默认返回较低版本,使用传统存储权限
|
||||
return 30;
|
||||
}
|
||||
}
|
||||
849
lib/pages/movies/movie_form_page.dart
Normal file
849
lib/pages/movies/movie_form_page.dart
Normal file
@@ -0,0 +1,849 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
|
||||
/// 添加/编辑影视页面 - 紧凑双行布局设计
|
||||
class MovieFormPage extends StatefulWidget {
|
||||
final Movie? movie;
|
||||
final String? initialStatus; // 添加时的默认状态
|
||||
|
||||
const MovieFormPage({super.key, this.movie, this.initialStatus});
|
||||
|
||||
@override
|
||||
State<MovieFormPage> createState() => _MovieFormPageState();
|
||||
}
|
||||
|
||||
class _MovieFormPageState extends State<MovieFormPage> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final ImagePicker _picker = ImagePicker();
|
||||
|
||||
// 输入框控制器
|
||||
late TextEditingController _titleController;
|
||||
late TextEditingController _summaryController;
|
||||
late TextEditingController _ratingController;
|
||||
|
||||
// 多值字段的临时输入控制器
|
||||
final Map<String, TextEditingController> _tagControllers = {};
|
||||
|
||||
// 数据
|
||||
List<String> _directors = [];
|
||||
List<String> _writers = [];
|
||||
List<String> _actors = [];
|
||||
List<String> _genres = [];
|
||||
List<String> _alternateTitles = [];
|
||||
String? _posterPath;
|
||||
String _status = 'want_to_watch';
|
||||
DateTime? _releaseDate;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_initializeData();
|
||||
}
|
||||
|
||||
void _initializeData() {
|
||||
// 如果有传入movie,尝试从Provider获取最新数据
|
||||
Movie? movie = widget.movie;
|
||||
if (movie != null) {
|
||||
final appProvider = context.read<AppProvider>();
|
||||
final latestMovie = appProvider.movies
|
||||
.where((m) => m.id == movie!.id)
|
||||
.firstOrNull;
|
||||
if (latestMovie != null) {
|
||||
movie = latestMovie;
|
||||
}
|
||||
}
|
||||
|
||||
_titleController = TextEditingController(text: movie?.title ?? '');
|
||||
_summaryController = TextEditingController(text: movie?.summary ?? '');
|
||||
_ratingController = TextEditingController(text: movie?.rating?.toString() ?? '');
|
||||
|
||||
if (movie != null) {
|
||||
_directors = List.from(movie.directors);
|
||||
_writers = List.from(movie.writers);
|
||||
_actors = List.from(movie.actors);
|
||||
_genres = List.from(movie.genres);
|
||||
_alternateTitles = List.from(movie.alternateTitles);
|
||||
_posterPath = movie.posterPath;
|
||||
_status = movie.status;
|
||||
_releaseDate = movie.releaseDate;
|
||||
} else if (widget.initialStatus != null) {
|
||||
// 添加模式:使用传入的默认状态
|
||||
_status = widget.initialStatus!;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_summaryController.dispose();
|
||||
_ratingController.dispose();
|
||||
_tagControllers.values.forEach((c) => c.dispose());
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
TextEditingController _getTagController(String key) {
|
||||
return _tagControllers.putIfAbsent(key, () => TextEditingController());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isEdit = widget.movie != null;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(isEdit ? '编辑影视' : '添加影视'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _saveMovie,
|
||||
child: const Text(
|
||||
'保存',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: ListView(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
|
||||
children: [
|
||||
// 封面选择 - 居中显示
|
||||
Center(child: _buildCoverPicker()),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// 状态选择(靠左显示)
|
||||
_buildStatusSelector(),
|
||||
|
||||
const SizedBox(height: 20),
|
||||
|
||||
// 评分 - 星星选择(靠左显示)
|
||||
_buildStarRating(),
|
||||
|
||||
const SizedBox(height: 32),
|
||||
|
||||
// 基本信息区域
|
||||
_buildFormItem(
|
||||
label: '影视名称 *',
|
||||
child: TextFormField(
|
||||
controller: _titleController,
|
||||
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A)),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '请输入影视名称',
|
||||
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入影视名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 别名
|
||||
_buildMultiValueItem(
|
||||
label: '别名',
|
||||
values: _alternateTitles,
|
||||
hint: '输入别名',
|
||||
controllerKey: 'alternateTitles',
|
||||
onAdd: (v) => setState(() => _alternateTitles.add(v)),
|
||||
onRemove: (i) => setState(() => _alternateTitles.removeAt(i)),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 上映日期
|
||||
_buildFormItem(
|
||||
label: '上映日期',
|
||||
child: GestureDetector(
|
||||
onTap: _selectReleaseDate,
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(
|
||||
_releaseDate != null
|
||||
? '${_releaseDate!.year}.${_releaseDate!.month.toString().padLeft(2, '0')}.${_releaseDate!.day.toString().padLeft(2, '0')}'
|
||||
: '选择日期',
|
||||
style: TextStyle(
|
||||
fontSize: 15,
|
||||
color: _releaseDate != null
|
||||
? const Color(0xFF1A1A1A)
|
||||
: const Color(0xFFCCCCCC),
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_releaseDate != null)
|
||||
GestureDetector(
|
||||
onTap: () => setState(() => _releaseDate = null),
|
||||
child: const Icon(Icons.close, size: 18, color: Color(0xFF999999)),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 导演
|
||||
_buildMultiValueItem(
|
||||
label: '导演',
|
||||
values: _directors,
|
||||
hint: '输入导演姓名',
|
||||
controllerKey: 'directors',
|
||||
onAdd: (v) => setState(() => _directors.add(v)),
|
||||
onRemove: (i) => setState(() => _directors.removeAt(i)),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 编剧
|
||||
_buildMultiValueItem(
|
||||
label: '编剧',
|
||||
values: _writers,
|
||||
hint: '输入编剧姓名',
|
||||
controllerKey: 'writers',
|
||||
onAdd: (v) => setState(() => _writers.add(v)),
|
||||
onRemove: (i) => setState(() => _writers.removeAt(i)),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 主演
|
||||
_buildMultiValueItem(
|
||||
label: '主演',
|
||||
values: _actors,
|
||||
hint: '输入主演姓名',
|
||||
controllerKey: 'actors',
|
||||
onAdd: (v) => setState(() => _actors.add(v)),
|
||||
onRemove: (i) => setState(() => _actors.removeAt(i)),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 类型
|
||||
_buildMultiValueItem(
|
||||
label: '类型',
|
||||
values: _genres,
|
||||
hint: '如:剧情、科幻',
|
||||
controllerKey: 'genres',
|
||||
onAdd: (v) => setState(() => _genres.add(v)),
|
||||
onRemove: (i) => setState(() => _genres.removeAt(i)),
|
||||
),
|
||||
|
||||
_buildDivider(),
|
||||
|
||||
// 剧情简介
|
||||
_buildFormItem(
|
||||
label: '剧情简介',
|
||||
child: TextFormField(
|
||||
controller: _summaryController,
|
||||
maxLines: 4,
|
||||
style: const TextStyle(fontSize: 15, color: Color(0xFF1A1A1A), height: 1.5),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '写下剧情简介...',
|
||||
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 48),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建表单条目(标签 + 内容)
|
||||
Widget _buildFormItem({required String label, required Widget child}) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: label.contains('*') ? const Color(0xFF1A1A1A) : const Color(0xFF666666),
|
||||
fontWeight: label.contains('*') ? FontWeight.w500 : FontWeight.normal,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
child,
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建多值条目
|
||||
Widget _buildMultiValueItem({
|
||||
required String label,
|
||||
required List<String> values,
|
||||
required String hint,
|
||||
required String controllerKey,
|
||||
required Function(String) onAdd,
|
||||
required Function(int) onRemove,
|
||||
}) {
|
||||
final controller = _getTagController(controllerKey);
|
||||
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 第一行:标签 + 添加按钮
|
||||
Row(
|
||||
children: [
|
||||
Text(
|
||||
label,
|
||||
style: const TextStyle(fontSize: 13, color: Color(0xFF666666)),
|
||||
),
|
||||
const Spacer(),
|
||||
// 添加按钮(当输入框有内容时显示)
|
||||
ValueListenableBuilder<TextEditingValue>(
|
||||
valueListenable: controller,
|
||||
builder: (context, value, child) {
|
||||
final hasText = value.text.trim().isNotEmpty;
|
||||
return GestureDetector(
|
||||
onTap: hasText
|
||||
? () {
|
||||
final text = controller.text.trim();
|
||||
if (text.isNotEmpty && !values.contains(text)) {
|
||||
onAdd(text);
|
||||
controller.clear();
|
||||
}
|
||||
}
|
||||
: null,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(
|
||||
color: hasText ? const Color(0xFF1A1A1A) : const Color(0xFFE5E5E5),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.add,
|
||||
size: 14,
|
||||
color: hasText ? const Color(0xFF1A1A1A) : const Color(0xFFCCCCCC),
|
||||
),
|
||||
const SizedBox(width: 2),
|
||||
Text(
|
||||
'添加',
|
||||
style: TextStyle(
|
||||
fontSize: 12,
|
||||
color: hasText ? const Color(0xFF1A1A1A) : const Color(0xFFCCCCCC),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
// 第二行:已选标签 + 输入框
|
||||
Wrap(
|
||||
spacing: 8,
|
||||
runSpacing: 8,
|
||||
crossAxisAlignment: WrapCrossAlignment.center,
|
||||
children: [
|
||||
...values.asMap().entries.map((entry) {
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
entry.value,
|
||||
style: const TextStyle(fontSize: 14, color: Color(0xFF1A1A1A)),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
GestureDetector(
|
||||
onTap: () => onRemove(entry.key),
|
||||
child: const Icon(Icons.close, size: 14, color: Color(0xFF999999)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}),
|
||||
// 输入框
|
||||
ConstrainedBox(
|
||||
constraints: const BoxConstraints(minWidth: 100, maxWidth: 150),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
style: const TextStyle(fontSize: 14, color: Color(0xFF1A1A1A)),
|
||||
decoration: InputDecoration(
|
||||
hintText: values.isEmpty ? hint : '',
|
||||
hintStyle: const TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
isDense: true,
|
||||
contentPadding: const EdgeInsets.symmetric(vertical: 5),
|
||||
),
|
||||
onSubmitted: (value) {
|
||||
final trimmed = value.trim();
|
||||
if (trimmed.isNotEmpty && !values.contains(trimmed)) {
|
||||
onAdd(trimmed);
|
||||
controller.clear();
|
||||
}
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建分隔线
|
||||
Widget _buildDivider() {
|
||||
return Container(
|
||||
margin: const EdgeInsets.symmetric(vertical: 16),
|
||||
height: 0.5,
|
||||
color: const Color(0xFFE5E5E5),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建状态选择器(靠左显示,带标签)
|
||||
Widget _buildStatusSelector() {
|
||||
return Row(
|
||||
children: [
|
||||
const Text(
|
||||
'状态',
|
||||
style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Container(
|
||||
padding: const EdgeInsets.all(4),
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
_buildStatusOption('想看', 'want_to_watch'),
|
||||
_buildStatusOption('在看', 'watching'),
|
||||
_buildStatusOption('已看', 'watched'),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建星星评分(5星制,每星2分,支持手动输入)
|
||||
Widget _buildStarRating() {
|
||||
return Row(
|
||||
children: [
|
||||
const Text(
|
||||
'评分',
|
||||
style: TextStyle(fontSize: 14, color: Color(0xFF666666)),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
// 星星选择
|
||||
_buildStarSelector(),
|
||||
const SizedBox(width: 12),
|
||||
// 手动输入框
|
||||
_buildRatingInputField(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建星星选择器
|
||||
Widget _buildStarSelector() {
|
||||
final currentRating = double.tryParse(_ratingController.text) ?? 0;
|
||||
final starRating = currentRating / 2;
|
||||
|
||||
return Container(
|
||||
padding: const EdgeInsets.symmetric(vertical: 8),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: List.generate(5, (index) {
|
||||
final starValue = index + 1;
|
||||
final scoreValue = starValue * 2;
|
||||
final isFilled = starValue <= starRating;
|
||||
final isHalf = starValue == starRating.ceil() && starRating % 1 != 0;
|
||||
|
||||
return InkWell(
|
||||
onTap: () {
|
||||
setState(() {
|
||||
_ratingController.text = scoreValue.toString();
|
||||
});
|
||||
},
|
||||
borderRadius: BorderRadius.circular(4),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 2, vertical: 4),
|
||||
child: Icon(
|
||||
isHalf
|
||||
? Icons.star_half
|
||||
: isFilled
|
||||
? Icons.star
|
||||
: Icons.star_border,
|
||||
size: 24,
|
||||
color: isFilled || isHalf
|
||||
? const Color(0xFFFFB800)
|
||||
: const Color(0xFFE5E5E5),
|
||||
),
|
||||
),
|
||||
);
|
||||
}),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建评分输入框
|
||||
Widget _buildRatingInputField() {
|
||||
return Container(
|
||||
width: 56,
|
||||
height: 36,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
child: TextFormField(
|
||||
controller: _ratingController,
|
||||
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||
textAlign: TextAlign.center,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '-',
|
||||
hintStyle: TextStyle(fontSize: 15, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
final rating = double.tryParse(value);
|
||||
if (rating == null || rating < 0 || rating > 10) {
|
||||
return '0-10';
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
onChanged: (value) {
|
||||
// 限制输入范围
|
||||
if (value.isNotEmpty) {
|
||||
final rating = double.tryParse(value);
|
||||
if (rating != null) {
|
||||
if (rating > 10) {
|
||||
_ratingController.text = '10';
|
||||
} else if (rating < 0) {
|
||||
_ratingController.text = '0';
|
||||
}
|
||||
}
|
||||
}
|
||||
setState(() {}); // 更新星星显示
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建状态选项
|
||||
Widget _buildStatusOption(String label, String value) {
|
||||
final isSelected = _status == value;
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => setState(() => _status = value),
|
||||
child: AnimatedContainer(
|
||||
duration: const Duration(milliseconds: 200),
|
||||
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
color: isSelected ? Colors.white : Colors.transparent,
|
||||
borderRadius: BorderRadius.circular(6),
|
||||
boxShadow: isSelected
|
||||
? [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.05),
|
||||
blurRadius: 4,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
]
|
||||
: null,
|
||||
),
|
||||
child: Text(
|
||||
label,
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: isSelected ? FontWeight.w500 : FontWeight.normal,
|
||||
color: isSelected ? const Color(0xFF1A1A1A) : const Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建封面选择器
|
||||
Widget _buildCoverPicker() {
|
||||
final hasPoster = _posterPath != null && _posterPath!.isNotEmpty;
|
||||
|
||||
return Column(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: _pickCover,
|
||||
child: Container(
|
||||
width: 140,
|
||||
height: 200,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
border: Border.all(color: const Color(0xFFE5E5E5), width: 0.5),
|
||||
),
|
||||
child: hasPoster
|
||||
? Image.file(
|
||||
File(_posterPath!),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => _buildCoverPlaceholder(),
|
||||
)
|
||||
: _buildCoverPlaceholder(),
|
||||
),
|
||||
),
|
||||
// 清空海报按钮(仅当有海报时显示)
|
||||
if (hasPoster)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 12),
|
||||
child: GestureDetector(
|
||||
onTap: () => setState(() => _posterPath = null),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 6),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: const Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.hide_image_outlined,
|
||||
size: 16,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
SizedBox(width: 4),
|
||||
Text(
|
||||
'清空海报',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildCoverPlaceholder() {
|
||||
return const Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.add_photo_alternate_outlined,
|
||||
size: 40,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
SizedBox(height: 12),
|
||||
Text(
|
||||
'点击添加海报',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 选择封面
|
||||
Future<void> _pickCover() async {
|
||||
try {
|
||||
final XFile? pickedFile = await _picker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
maxWidth: 800,
|
||||
maxHeight: 1200,
|
||||
imageQuality: 85,
|
||||
);
|
||||
|
||||
if (pickedFile != null) {
|
||||
// 生成文件名
|
||||
final fileName = 'poster_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
|
||||
// 如果是编辑模式,使用现有影视ID;如果是新建模式,使用临时ID(保存时会替换)
|
||||
final movieId = widget.movie?.id ?? DateTime.now().millisecondsSinceEpoch.toString();
|
||||
|
||||
// 保存到新的路径结构: images/movies/{movieId}/{fileName}
|
||||
final targetPath = await ImagePathHelper.instance.getMoviePosterPath(
|
||||
movieId,
|
||||
fileName
|
||||
);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
|
||||
await File(pickedFile.path).copy(targetPath);
|
||||
|
||||
setState(() => _posterPath = targetPath);
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '选择海报失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择日期
|
||||
Future<void> _selectReleaseDate() async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: _releaseDate ?? DateTime.now(),
|
||||
firstDate: DateTime(1900),
|
||||
lastDate: DateTime.now().add(const Duration(days: 365 * 5)),
|
||||
builder: (context, child) {
|
||||
return Theme(
|
||||
data: Theme.of(context).copyWith(
|
||||
colorScheme: const ColorScheme.light(
|
||||
primary: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
child: child!,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
setState(() => _releaseDate = picked);
|
||||
}
|
||||
}
|
||||
|
||||
/// 保存影视
|
||||
Future<void> _saveMovie() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final rating = _ratingController.text.isNotEmpty
|
||||
? double.tryParse(_ratingController.text)
|
||||
: null;
|
||||
|
||||
final now = DateTime.now();
|
||||
|
||||
if (widget.movie == null) {
|
||||
// 生成新的影视ID
|
||||
final newMovieId = now.millisecondsSinceEpoch.toString();
|
||||
|
||||
// 如果有海报,需要移动到正确的ID目录
|
||||
String? finalPosterPath;
|
||||
if (_posterPath != null && _posterPath!.isNotEmpty) {
|
||||
finalPosterPath = await _movePosterToNewId(_posterPath!, newMovieId);
|
||||
}
|
||||
|
||||
final newMovie = Movie(
|
||||
id: newMovieId,
|
||||
title: _titleController.text.trim(),
|
||||
posterPath: finalPosterPath,
|
||||
releaseDate: _releaseDate,
|
||||
directors: _directors,
|
||||
writers: _writers,
|
||||
actors: _actors,
|
||||
genres: _genres,
|
||||
alternateTitles: _alternateTitles,
|
||||
summary: _summaryController.text.trim(),
|
||||
rating: rating,
|
||||
status: _status,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
await context.read<AppProvider>().addMovie(newMovie);
|
||||
} else {
|
||||
final updatedMovie = widget.movie!.copyWith(
|
||||
title: _titleController.text.trim(),
|
||||
posterPath: _posterPath,
|
||||
releaseDate: _releaseDate,
|
||||
directors: _directors,
|
||||
writers: _writers,
|
||||
actors: _actors,
|
||||
genres: _genres,
|
||||
alternateTitles: _alternateTitles,
|
||||
summary: _summaryController.text.trim(),
|
||||
rating: rating,
|
||||
status: _status,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
await context.read<AppProvider>().updateMovie(updatedMovie);
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ToastUtil.show(context, widget.movie == null ? '添加成功' : '更新成功');
|
||||
|
||||
Navigator.pop(context);
|
||||
}
|
||||
|
||||
/// 将海报从临时ID目录移动到新的影视ID目录
|
||||
Future<String?> _movePosterToNewId(String currentPath, String newMovieId) async {
|
||||
// 检查是否已经在正确的目录中(兼容 Windows 路径分隔符)
|
||||
final normalizedPath = currentPath.replaceAll('\\', '/');
|
||||
if (normalizedPath.contains('/movies/$newMovieId/')) {
|
||||
return currentPath;
|
||||
}
|
||||
|
||||
// 提取文件名
|
||||
final fileName = p.basename(currentPath);
|
||||
|
||||
// 获取新路径
|
||||
final newPath = await ImagePathHelper.instance.getMoviePosterPath(
|
||||
newMovieId,
|
||||
fileName
|
||||
);
|
||||
|
||||
// 确保目标目录存在
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(newPath));
|
||||
|
||||
// 移动文件
|
||||
final currentFile = File(currentPath);
|
||||
if (await currentFile.exists()) {
|
||||
await currentFile.rename(newPath);
|
||||
|
||||
// 删除空的临时目录
|
||||
final tempDir = Directory(p.dirname(currentPath));
|
||||
if (await tempDir.exists()) {
|
||||
try {
|
||||
await tempDir.delete(recursive: true);
|
||||
} catch (e) {
|
||||
// 忽略删除目录失败的情况
|
||||
}
|
||||
}
|
||||
|
||||
return newPath;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
554
lib/pages/movies/movie_form_page_new.dart
Normal file
554
lib/pages/movies/movie_form_page_new.dart
Normal file
@@ -0,0 +1,554 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:uuid/uuid.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
|
||||
/// 添加/编辑影视记录页面(Typecho 风格)
|
||||
class MovieFormPage extends StatefulWidget {
|
||||
final Movie? movie;
|
||||
|
||||
const MovieFormPage({super.key, this.movie});
|
||||
|
||||
@override
|
||||
State<MovieFormPage> createState() => _MovieFormPageState();
|
||||
}
|
||||
|
||||
class _MovieFormPageState extends State<MovieFormPage> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late TextEditingController _titleController;
|
||||
late TextEditingController _releaseDateController;
|
||||
late TextEditingController _directorsController;
|
||||
late TextEditingController _writersController;
|
||||
late TextEditingController _actorsController;
|
||||
late TextEditingController _genresController;
|
||||
late TextEditingController _alternateTitlesController;
|
||||
late TextEditingController _summaryController;
|
||||
late TextEditingController _ratingController;
|
||||
|
||||
String _status = 'want_to_watch';
|
||||
File? _posterImage;
|
||||
bool _isLoading = false;
|
||||
|
||||
final ImagePicker _picker = ImagePicker();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_titleController = TextEditingController(text: widget.movie?.title ?? '');
|
||||
_releaseDateController = TextEditingController(
|
||||
text: widget.movie?.releaseDate != null
|
||||
? _formatDate(widget.movie!.releaseDate!)
|
||||
: '',
|
||||
);
|
||||
_directorsController = TextEditingController(
|
||||
text: (widget.movie?.directors ?? []).join(', '),
|
||||
);
|
||||
_writersController = TextEditingController(
|
||||
text: (widget.movie?.writers ?? []).join(', '),
|
||||
);
|
||||
_actorsController = TextEditingController(
|
||||
text: (widget.movie?.actors ?? []).join(', '),
|
||||
);
|
||||
_genresController = TextEditingController(
|
||||
text: (widget.movie?.genres ?? []).join(', '),
|
||||
);
|
||||
_alternateTitlesController = TextEditingController(
|
||||
text: (widget.movie?.alternateTitles ?? []).join(', '),
|
||||
);
|
||||
_summaryController = TextEditingController(text: widget.movie?.summary ?? '');
|
||||
_ratingController = TextEditingController(
|
||||
text: widget.movie?.rating?.toString() ?? '',
|
||||
);
|
||||
_status = widget.movie?.status ?? 'want_to_watch';
|
||||
|
||||
if (widget.movie?.posterPath != null) {
|
||||
_posterImage = File(widget.movie!.posterPath!);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_titleController.dispose();
|
||||
_releaseDateController.dispose();
|
||||
_directorsController.dispose();
|
||||
_writersController.dispose();
|
||||
_actorsController.dispose();
|
||||
_genresController.dispose();
|
||||
_alternateTitlesController.dispose();
|
||||
_summaryController.dispose();
|
||||
_ratingController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isEdit = widget.movie != null;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: Text(isEdit ? '编辑影视' : '添加影视'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.save),
|
||||
onPressed: _isLoading ? null : _saveMovie,
|
||||
),
|
||||
],
|
||||
),
|
||||
body: SingleChildScrollView(
|
||||
child: Column(
|
||||
children: [
|
||||
// 封面上传区域
|
||||
_buildCoverSection(),
|
||||
|
||||
const SizedBox(height: 24),
|
||||
|
||||
// 表单区域
|
||||
Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16),
|
||||
child: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildBasicInfoSection(),
|
||||
const SizedBox(height: 32),
|
||||
_buildCastSection(),
|
||||
const SizedBox(height: 32),
|
||||
_buildDetailSection(),
|
||||
const SizedBox(height: 48),
|
||||
_buildSaveButton(isEdit),
|
||||
const SizedBox(height: 32),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建封面上传区域
|
||||
Widget _buildCoverSection() {
|
||||
return GestureDetector(
|
||||
onTap: _pickImage,
|
||||
child: Container(
|
||||
width: double.infinity,
|
||||
height: 300,
|
||||
color: Theme.of(context).colorScheme.surfaceContainerHighest,
|
||||
child: _posterImage != null
|
||||
? Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
Image.file(
|
||||
_posterImage!,
|
||||
fit: BoxFit.cover,
|
||||
),
|
||||
Positioned(
|
||||
top: 16,
|
||||
right: 16,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.all(8),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.black54,
|
||||
borderRadius: BorderRadius.circular(20),
|
||||
),
|
||||
child: const Icon(
|
||||
Icons.camera_alt,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Icon(
|
||||
Icons.add_photo_alternate_outlined,
|
||||
size: 64,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'点击上传海报',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Text(
|
||||
'建议尺寸:2:3 比例',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Theme.of(context).colorScheme.onSurfaceVariant.withOpacity(0.6),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建基本信息区域
|
||||
Widget _buildBasicInfoSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSectionTitle('基本信息'),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _titleController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '影视名称 *',
|
||||
hintText: '请输入影视名称',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.title),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入影视名称';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _releaseDateController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '上映时间',
|
||||
hintText: 'YYYY-MM-DD',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.calendar_today),
|
||||
),
|
||||
readOnly: true,
|
||||
onTap: _selectReleaseDate,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(width: 16),
|
||||
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _ratingController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '评分',
|
||||
hintText: '1-10',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.star),
|
||||
),
|
||||
keyboardType: const TextInputType.numberWithOptions(decimal: true),
|
||||
validator: (value) {
|
||||
if (value != null && value.isNotEmpty) {
|
||||
final rating = double.tryParse(value);
|
||||
if (rating == null || rating < 1 || rating > 10) {
|
||||
return '1-10';
|
||||
}
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
DropdownButtonFormField<String>(
|
||||
value: _status,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '状态',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.check_circle_outline),
|
||||
),
|
||||
items: const [
|
||||
DropdownMenuItem(value: 'watched', child: Text('已看')),
|
||||
DropdownMenuItem(value: 'want_to_watch', child: Text('想看')),
|
||||
DropdownMenuItem(value: 'watching', child: Text('在看')),
|
||||
],
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
_status = value!;
|
||||
});
|
||||
},
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建演职人员区域
|
||||
Widget _buildCastSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSectionTitle('演职人员'),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _directorsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '导演',
|
||||
hintText: '多个用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.person),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _writersController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '编剧',
|
||||
hintText: '多个用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.edit_note),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _actorsController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '主演',
|
||||
hintText: '多个用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.people),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建详细信息区域
|
||||
Widget _buildDetailSection() {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildSectionTitle('详细信息'),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _genresController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '类型',
|
||||
hintText: '多个用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.category),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _alternateTitlesController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '别名',
|
||||
hintText: '多个用逗号分隔',
|
||||
border: OutlineInputBorder(),
|
||||
prefixIcon: Icon(Icons.alt_route),
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 16),
|
||||
|
||||
TextFormField(
|
||||
controller: _summaryController,
|
||||
decoration: const InputDecoration(
|
||||
labelText: '剧情简介',
|
||||
hintText: '请输入剧情简介...',
|
||||
border: OutlineInputBorder(),
|
||||
alignLabelWithHint: true,
|
||||
prefixIcon: Icon(Icons.description),
|
||||
),
|
||||
maxLines: 6,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建保存按钮
|
||||
Widget _buildSaveButton(bool isEdit) {
|
||||
return SizedBox(
|
||||
width: double.infinity,
|
||||
height: 48,
|
||||
child: ElevatedButton.icon(
|
||||
onPressed: _isLoading ? null : _saveMovie,
|
||||
icon: _isLoading
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(strokeWidth: 2),
|
||||
)
|
||||
: const Icon(Icons.save),
|
||||
label: Text(isEdit ? '保存修改' : '添加记录'),
|
||||
style: ElevatedButton.styleFrom(
|
||||
shape: RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建区块标题
|
||||
Widget _buildSectionTitle(String title) {
|
||||
return Text(
|
||||
title,
|
||||
style: Theme.of(context).textTheme.titleMedium?.copyWith(
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 选择图片
|
||||
Future<void> _pickImage() async {
|
||||
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
|
||||
|
||||
if (image != null) {
|
||||
setState(() {
|
||||
_posterImage = File(image.path);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择日期
|
||||
Future<void> _selectReleaseDate() async {
|
||||
final picked = await showDatePicker(
|
||||
context: context,
|
||||
initialDate: DateTime.now(),
|
||||
firstDate: DateTime(1900),
|
||||
lastDate: DateTime.now(),
|
||||
);
|
||||
|
||||
if (picked != null) {
|
||||
setState(() {
|
||||
_releaseDateController.text = _formatDate(picked);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// 格式化日期
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
/// 保存影视记录
|
||||
Future<void> _saveMovie() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_isLoading = true;
|
||||
});
|
||||
|
||||
try {
|
||||
// 处理列表字段
|
||||
final directors = _parseList(_directorsController.text);
|
||||
final writers = _parseList(_writersController.text);
|
||||
final actors = _parseList(_actorsController.text);
|
||||
final genres = _parseList(_genresController.text);
|
||||
final alternateTitles = _parseList(_alternateTitlesController.text);
|
||||
|
||||
// 处理评分
|
||||
final rating = _ratingController.text.isNotEmpty
|
||||
? double.parse(_ratingController.text)
|
||||
: null;
|
||||
|
||||
// 处理日期
|
||||
final releaseDate = _releaseDateController.text.isNotEmpty
|
||||
? DateTime.tryParse(_releaseDateController.text)
|
||||
: null;
|
||||
|
||||
// 处理图片
|
||||
String? posterPath;
|
||||
if (_posterImage != null) {
|
||||
final dir = await getApplicationDocumentsDirectory();
|
||||
final fileName = '${const Uuid().v4()}.jpg';
|
||||
final savedImage = await _posterImage!.copy('${dir.path}/posters/$fileName');
|
||||
posterPath = savedImage.path;
|
||||
}
|
||||
|
||||
if (widget.movie == null) {
|
||||
// 添加新模式
|
||||
final now = DateTime.now();
|
||||
final newMovie = Movie(
|
||||
id: const Uuid().v4(),
|
||||
title: _titleController.text.trim(),
|
||||
posterPath: posterPath,
|
||||
releaseDate: releaseDate,
|
||||
directors: directors,
|
||||
writers: writers,
|
||||
actors: actors,
|
||||
genres: genres,
|
||||
alternateTitles: alternateTitles,
|
||||
summary: _summaryController.text.trim(),
|
||||
rating: rating,
|
||||
status: _status,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
|
||||
await context.read<AppProvider>().addMovie(newMovie);
|
||||
} else {
|
||||
// 编辑现有模式
|
||||
final updatedMovie = Movie(
|
||||
id: widget.movie!.id,
|
||||
title: _titleController.text.trim(),
|
||||
posterPath: posterPath ?? widget.movie!.posterPath,
|
||||
releaseDate: releaseDate,
|
||||
directors: directors,
|
||||
writers: writers,
|
||||
actors: actors,
|
||||
genres: genres,
|
||||
alternateTitles: alternateTitles,
|
||||
summary: _summaryController.text.trim(),
|
||||
rating: rating,
|
||||
status: _status,
|
||||
createdAt: widget.movie!.createdAt,
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await context.read<AppProvider>().updateMovie(updatedMovie);
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ToastUtil.show(context, widget.movie == null ? '添加成功' : '更新成功');
|
||||
|
||||
Navigator.pop(context);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ToastUtil.show(context, '保存失败:$e');
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() {
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 解析列表
|
||||
List<String> _parseList(String text) {
|
||||
return text
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.where((item) => item.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
261
lib/pages/movies/movie_posters_page.dart
Normal file
261
lib/pages/movies/movie_posters_page.dart
Normal file
@@ -0,0 +1,261 @@
|
||||
import 'dart:io';
|
||||
import 'dart:math';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:image_picker/image_picker.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:provider/provider.dart';
|
||||
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import '../../utils/image_path_helper.dart';
|
||||
import 'poster_gallery_page.dart';
|
||||
|
||||
/// 影视海报墙页面
|
||||
class MoviePostersPage extends StatefulWidget {
|
||||
final Movie movie;
|
||||
|
||||
const MoviePostersPage({super.key, required this.movie});
|
||||
|
||||
@override
|
||||
State<MoviePostersPage> createState() => _MoviePostersPageState();
|
||||
}
|
||||
|
||||
class _MoviePostersPageState extends State<MoviePostersPage> {
|
||||
final ImagePicker _picker = ImagePicker();
|
||||
List<MoviePoster> _posters = [];
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadPosters();
|
||||
}
|
||||
|
||||
Future<void> _loadPosters() async {
|
||||
setState(() => _isLoading = true);
|
||||
final posters = await context.read<AppProvider>().getMoviePosters(widget.movie.id);
|
||||
setState(() {
|
||||
_posters = posters;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: const Text('海报墙'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add_photo_alternate),
|
||||
onPressed: _pickPoster,
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _posters.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildPosterGrid(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.photo_library_outlined,
|
||||
size: 64,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'暂无海报',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
OutlinedButton(
|
||||
onPressed: _pickPoster,
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: const Color(0xFF1A1A1A),
|
||||
side: const BorderSide(color: Color(0xFF1A1A1A)),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
),
|
||||
child: const Text('添加海报'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPosterGrid() {
|
||||
return MasonryGridView.count(
|
||||
padding: const EdgeInsets.all(16),
|
||||
crossAxisCount: 2,
|
||||
mainAxisSpacing: 12,
|
||||
crossAxisSpacing: 12,
|
||||
itemCount: _posters.length,
|
||||
itemBuilder: (context, index) {
|
||||
final poster = _posters[index];
|
||||
return _buildPosterItem(poster, index);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildPosterItem(MoviePoster poster, int index) {
|
||||
// 根据索引生成不同的高度,实现瀑布流效果
|
||||
final heights = [180.0, 220.0, 160.0, 200.0, 240.0, 190.0];
|
||||
final height = heights[index % heights.length];
|
||||
|
||||
return GestureDetector(
|
||||
onTap: () => _showPosterDetail(poster),
|
||||
onLongPress: () => _showDeleteDialog(poster),
|
||||
child: Container(
|
||||
height: height,
|
||||
decoration: BoxDecoration(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
boxShadow: [
|
||||
BoxShadow(
|
||||
color: Colors.black.withOpacity(0.08),
|
||||
blurRadius: 8,
|
||||
offset: const Offset(0, 2),
|
||||
),
|
||||
],
|
||||
),
|
||||
child: ClipRRect(
|
||||
borderRadius: BorderRadius.circular(8),
|
||||
child: Stack(
|
||||
fit: StackFit.expand,
|
||||
children: [
|
||||
// 海报图片
|
||||
Image.file(
|
||||
File(poster.posterPath),
|
||||
fit: BoxFit.cover,
|
||||
errorBuilder: (_, __, ___) => const Center(
|
||||
child: Icon(
|
||||
Icons.broken_image,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
),
|
||||
),
|
||||
// 渐变遮罩(底部)
|
||||
Positioned(
|
||||
bottom: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: Container(
|
||||
height: 40,
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.transparent,
|
||||
Colors.black.withOpacity(0.3),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showPosterDetail(MoviePoster poster) {
|
||||
// 找到当前海报的索引
|
||||
final initialIndex = _posters.indexWhere((p) => p.id == poster.id);
|
||||
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => PosterGalleryPage(
|
||||
posters: _posters,
|
||||
initialIndex: initialIndex >= 0 ? initialIndex : 0,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _pickPoster() async {
|
||||
try {
|
||||
final XFile? pickedFile = await _picker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
maxWidth: 1200,
|
||||
maxHeight: 1800,
|
||||
imageQuality: 85,
|
||||
);
|
||||
|
||||
if (pickedFile != null) {
|
||||
// 生成文件名
|
||||
final fileName = 'posterimg_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
|
||||
// 保存到 posterimgs 子目录: images/movies/{movieId}/posterimgs/{fileName}
|
||||
final targetPath = await ImagePathHelper.instance.getMoviePosterImgPath(
|
||||
widget.movie.id,
|
||||
fileName
|
||||
);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
|
||||
await File(pickedFile.path).copy(targetPath);
|
||||
|
||||
final newPoster = MoviePoster(
|
||||
id: DateTime.now().millisecondsSinceEpoch.toString(),
|
||||
movieId: widget.movie.id,
|
||||
posterPath: targetPath,
|
||||
createdAt: DateTime.now(),
|
||||
);
|
||||
|
||||
await context.read<AppProvider>().addMoviePoster(newPoster);
|
||||
_loadPosters();
|
||||
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '添加成功');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '添加海报失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _showDeleteDialog(MoviePoster poster) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('确认删除'),
|
||||
content: const Text('确定要删除这张海报吗?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await context.read<AppProvider>().removeMoviePoster(poster.id);
|
||||
Navigator.pop(context);
|
||||
_loadPosters();
|
||||
ToastUtil.show(context, '已删除');
|
||||
},
|
||||
child: const Text('删除', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
259
lib/pages/movies/movie_review_form_page.dart
Normal file
259
lib/pages/movies/movie_review_form_page.dart
Normal file
@@ -0,0 +1,259 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
|
||||
/// 添加/编辑影评页面 - 极简设计
|
||||
class MovieReviewFormPage extends StatefulWidget {
|
||||
final String movieId;
|
||||
final MovieReview? review;
|
||||
|
||||
const MovieReviewFormPage({
|
||||
super.key,
|
||||
required this.movieId,
|
||||
this.review,
|
||||
});
|
||||
|
||||
@override
|
||||
State<MovieReviewFormPage> createState() => _MovieReviewFormPageState();
|
||||
}
|
||||
|
||||
class _MovieReviewFormPageState extends State<MovieReviewFormPage> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
late TextEditingController _contentController;
|
||||
late TextEditingController _reviewerController;
|
||||
late TextEditingController _sourceController;
|
||||
late int _reviewType;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final review = widget.review;
|
||||
_contentController = TextEditingController(text: review?.content ?? '');
|
||||
_reviewerController = TextEditingController(text: review?.reviewer ?? '');
|
||||
_sourceController = TextEditingController(text: review?.source ?? '');
|
||||
_reviewType = review?.reviewType ?? 1;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_contentController.dispose();
|
||||
_reviewerController.dispose();
|
||||
_sourceController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isEdit = widget.review != null;
|
||||
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: Text(isEdit ? '编辑影评' : '写影评'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: _saveReview,
|
||||
child: const Text(
|
||||
'保存',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
child: Column(
|
||||
children: [
|
||||
// 顶部信息栏
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
|
||||
decoration: const BoxDecoration(
|
||||
border: Border(
|
||||
bottom: BorderSide(color: Color(0xFFE5E5E5), width: 0.5),
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 类型选择
|
||||
_buildTypeSelector(),
|
||||
const SizedBox(width: 16),
|
||||
// 评论人
|
||||
Expanded(
|
||||
child: TextField(
|
||||
controller: _reviewerController,
|
||||
style: const TextStyle(fontSize: 14, color: Color(0xFF1A1A1A)),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '评论人',
|
||||
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
// 来源
|
||||
SizedBox(
|
||||
width: 100,
|
||||
child: TextField(
|
||||
controller: _sourceController,
|
||||
style: const TextStyle(fontSize: 14, color: Color(0xFF1A1A1A)),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '来源',
|
||||
hintStyle: TextStyle(fontSize: 14, color: Color(0xFFCCCCCC)),
|
||||
border: InputBorder.none,
|
||||
isDense: true,
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
|
||||
// 评论内容区域
|
||||
Expanded(
|
||||
child: TextFormField(
|
||||
controller: _contentController,
|
||||
maxLines: null,
|
||||
expands: true,
|
||||
textAlignVertical: TextAlignVertical.top,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
color: Color(0xFF1A1A1A),
|
||||
height: 1.7,
|
||||
),
|
||||
decoration: const InputDecoration(
|
||||
hintText: '写下你的影评...',
|
||||
hintStyle: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
border: InputBorder.none,
|
||||
contentPadding: EdgeInsets.all(16),
|
||||
),
|
||||
validator: (value) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '请输入评论内容';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建类型选择器
|
||||
Widget _buildTypeSelector() {
|
||||
return GestureDetector(
|
||||
onTap: () => _showTypeSelector(),
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 4),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(
|
||||
_reviewType == 1 ? '短评' : '长评',
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 4),
|
||||
const Icon(
|
||||
Icons.arrow_drop_down,
|
||||
size: 16,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/// 显示类型选择
|
||||
void _showTypeSelector() {
|
||||
showModalBottomSheet(
|
||||
context: context,
|
||||
backgroundColor: Colors.white,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
builder: (context) => SafeArea(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ListTile(
|
||||
title: const Text('短评'),
|
||||
trailing: _reviewType == 1
|
||||
? const Icon(Icons.check, color: Color(0xFF1A1A1A))
|
||||
: null,
|
||||
onTap: () {
|
||||
setState(() => _reviewType = 1);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
const Divider(height: 0.5),
|
||||
ListTile(
|
||||
title: const Text('长评'),
|
||||
trailing: _reviewType == 2
|
||||
? const Icon(Icons.check, color: Color(0xFF1A1A1A))
|
||||
: null,
|
||||
onTap: () {
|
||||
setState(() => _reviewType = 2);
|
||||
Navigator.pop(context);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _saveReview() async {
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final now = DateTime.now();
|
||||
|
||||
if (widget.review == null) {
|
||||
final newReview = MovieReview(
|
||||
id: now.millisecondsSinceEpoch.toString(),
|
||||
movieId: widget.movieId,
|
||||
content: _contentController.text.trim(),
|
||||
reviewer: _reviewerController.text.trim(),
|
||||
source: _sourceController.text.trim(),
|
||||
reviewType: _reviewType,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
);
|
||||
await context.read<AppProvider>().addMovieReview(newReview);
|
||||
} else {
|
||||
final updatedReview = widget.review!.copyWith(
|
||||
content: _contentController.text.trim(),
|
||||
reviewer: _reviewerController.text.trim(),
|
||||
source: _sourceController.text.trim(),
|
||||
reviewType: _reviewType,
|
||||
updatedAt: now,
|
||||
);
|
||||
await context.read<AppProvider>().updateMovieReview(updatedReview);
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
|
||||
ToastUtil.show(context, widget.review == null ? '添加成功' : '更新成功');
|
||||
|
||||
Navigator.pop(context);
|
||||
}
|
||||
}
|
||||
265
lib/pages/movies/movie_reviews_page.dart
Normal file
265
lib/pages/movies/movie_reviews_page.dart
Normal file
@@ -0,0 +1,265 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
import 'movie_review_form_page.dart';
|
||||
|
||||
/// 影视影评列表页面
|
||||
class MovieReviewsPage extends StatefulWidget {
|
||||
final Movie movie;
|
||||
|
||||
const MovieReviewsPage({super.key, required this.movie});
|
||||
|
||||
@override
|
||||
State<MovieReviewsPage> createState() => _MovieReviewsPageState();
|
||||
}
|
||||
|
||||
class _MovieReviewsPageState extends State<MovieReviewsPage> {
|
||||
List<MovieReview> _reviews = [];
|
||||
bool _isLoading = true;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadReviews();
|
||||
}
|
||||
|
||||
Future<void> _loadReviews() async {
|
||||
setState(() => _isLoading = true);
|
||||
final reviews = await context.read<AppProvider>().getMovieReviews(widget.movie.id);
|
||||
setState(() {
|
||||
_reviews = reviews;
|
||||
_isLoading = false;
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.white,
|
||||
appBar: AppBar(
|
||||
title: const Text('影评'),
|
||||
actions: [
|
||||
IconButton(
|
||||
icon: const Icon(Icons.add),
|
||||
onPressed: () => _navigateToAddReview(),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
body: _isLoading
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: _reviews.isEmpty
|
||||
? _buildEmptyState()
|
||||
: _buildReviewList(),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildEmptyState() {
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.rate_review_outlined,
|
||||
size: 64,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
const Text(
|
||||
'暂无影评',
|
||||
style: TextStyle(
|
||||
fontSize: 16,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
OutlinedButton(
|
||||
onPressed: () => _navigateToAddReview(),
|
||||
style: OutlinedButton.styleFrom(
|
||||
foregroundColor: const Color(0xFF1A1A1A),
|
||||
side: const BorderSide(color: Color(0xFF1A1A1A)),
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
),
|
||||
child: const Text('写影评'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildReviewList() {
|
||||
return ListView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
itemCount: _reviews.length,
|
||||
itemBuilder: (context, index) {
|
||||
final review = _reviews[index];
|
||||
return _buildReviewItem(review);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Widget _buildReviewItem(MovieReview review) {
|
||||
return InkWell(
|
||||
onLongPress: () => _showDeleteDialog(review),
|
||||
child: Container(
|
||||
margin: const EdgeInsets.only(bottom: 16),
|
||||
padding: const EdgeInsets.all(16),
|
||||
decoration: BoxDecoration(
|
||||
border: Border.all(color: const Color(0xFFE5E5E5)),
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// 头部:类型标签 + 操作按钮
|
||||
Row(
|
||||
children: [
|
||||
Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
|
||||
decoration: BoxDecoration(
|
||||
color: review.reviewType == 1
|
||||
? const Color(0xFFF5F5F5)
|
||||
: const Color(0xFF1A1A1A),
|
||||
),
|
||||
child: Text(
|
||||
review.typeText,
|
||||
style: TextStyle(
|
||||
fontSize: 11,
|
||||
color: review.reviewType == 1
|
||||
? const Color(0xFF666666)
|
||||
: Colors.white,
|
||||
),
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
// 编辑按钮
|
||||
GestureDetector(
|
||||
onTap: () => _navigateToEditReview(review),
|
||||
child: const Icon(
|
||||
Icons.edit_outlined,
|
||||
size: 18,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
// 删除按钮
|
||||
GestureDetector(
|
||||
onTap: () => _showDeleteDialog(review),
|
||||
child: const Icon(
|
||||
Icons.delete_outline,
|
||||
size: 18,
|
||||
color: Colors.red,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 评论内容
|
||||
Text(
|
||||
review.content,
|
||||
maxLines: review.reviewType == 1 ? 3 : 5,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
color: Color(0xFF1A1A1A),
|
||||
height: 1.6,
|
||||
),
|
||||
),
|
||||
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// 底部信息
|
||||
Row(
|
||||
children: [
|
||||
if (review.reviewer.isNotEmpty) ...[
|
||||
Text(
|
||||
review.reviewer,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
if (review.source.isNotEmpty) ...[
|
||||
Text(
|
||||
'来源:${review.source}',
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
const Spacer(),
|
||||
Text(
|
||||
_formatDate(review.createdAt),
|
||||
style: const TextStyle(
|
||||
fontSize: 12,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime date) {
|
||||
return '${date.year}.${date.month.toString().padLeft(2, '0')}.${date.day.toString().padLeft(2, '0')}';
|
||||
}
|
||||
|
||||
void _navigateToAddReview() {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MovieReviewFormPage(movieId: widget.movie.id),
|
||||
),
|
||||
).then((_) => _loadReviews());
|
||||
}
|
||||
|
||||
void _navigateToEditReview(MovieReview review) {
|
||||
Navigator.push(
|
||||
context,
|
||||
MaterialPageRoute(
|
||||
builder: (context) => MovieReviewFormPage(
|
||||
movieId: widget.movie.id,
|
||||
review: review,
|
||||
),
|
||||
),
|
||||
).then((_) => _loadReviews());
|
||||
}
|
||||
|
||||
void _showDeleteDialog(MovieReview review) {
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('确认删除'),
|
||||
content: const Text('确定要删除这条影评吗?'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () async {
|
||||
await context.read<AppProvider>().removeMovieReview(review.id);
|
||||
Navigator.pop(context);
|
||||
_loadReviews();
|
||||
ToastUtil.show(context, '已删除');
|
||||
},
|
||||
child: const Text('删除', style: TextStyle(color: Colors.red)),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
108
lib/pages/movies/movie_tab_page.dart
Normal file
108
lib/pages/movies/movie_tab_page.dart
Normal file
@@ -0,0 +1,108 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../widgets/movie_status_bar.dart';
|
||||
import '../../widgets/movie_list_item.dart';
|
||||
|
||||
/// 观影标签页 - 极简主义设计
|
||||
class MovieTabPage extends StatelessWidget {
|
||||
const MovieTabPage({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Column(
|
||||
children: [
|
||||
// 状态选择栏
|
||||
const MovieStatusBar(),
|
||||
|
||||
const Divider(height: 0.5, thickness: 0.5, color: Color(0xFFE5E5E5)),
|
||||
|
||||
// 影片列表
|
||||
Expanded(
|
||||
child: _buildMovieList(context),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建影片列表
|
||||
Widget _buildMovieList(BuildContext context) {
|
||||
return Consumer<AppProvider>(
|
||||
builder: (context, provider, child) {
|
||||
final statusMap = {
|
||||
0: 'watched',
|
||||
1: 'watching',
|
||||
2: 'want_to_watch',
|
||||
};
|
||||
final currentStatus = statusMap[provider.movieStatusIndex]!;
|
||||
final movies = provider.getMoviesByStatus(currentStatus);
|
||||
|
||||
if (movies.isEmpty) {
|
||||
return _buildEmptyState(context, provider.movieStatusIndex);
|
||||
}
|
||||
|
||||
return RefreshIndicator(
|
||||
onRefresh: () async => await provider.loadMovies(),
|
||||
color: const Color(0xFF1A1A1A),
|
||||
backgroundColor: Colors.white,
|
||||
child: GridView.builder(
|
||||
padding: const EdgeInsets.all(16),
|
||||
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
|
||||
crossAxisCount: 3,
|
||||
childAspectRatio: 0.55,
|
||||
crossAxisSpacing: 12,
|
||||
mainAxisSpacing: 16,
|
||||
),
|
||||
itemCount: movies.length,
|
||||
itemBuilder: (context, index) {
|
||||
return MovieListItem(movie: movies[index]);
|
||||
},
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
/// 构建空状态
|
||||
Widget _buildEmptyState(BuildContext context, int statusIndex) {
|
||||
final statusText = ['已看', '在看', '想看'][statusIndex];
|
||||
|
||||
return Center(
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
const Icon(
|
||||
Icons.movie_outlined,
|
||||
size: 48,
|
||||
color: Color(0xFFCCCCCC),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Text(
|
||||
'暂无$statusText的影片',
|
||||
style: const TextStyle(
|
||||
fontSize: 15,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
final statusMap = {
|
||||
0: 'watched',
|
||||
1: 'watching',
|
||||
2: 'want_to_watch',
|
||||
};
|
||||
final currentStatus = statusMap[statusIndex]!;
|
||||
Navigator.pushNamed(
|
||||
context,
|
||||
'/movie-form',
|
||||
arguments: {'initialStatus': currentStatus},
|
||||
);
|
||||
},
|
||||
child: const Text('添加记录'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
146
lib/pages/movies/poster_gallery_page.dart
Normal file
146
lib/pages/movies/poster_gallery_page.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'dart:io';
|
||||
import 'package:flutter/material.dart';
|
||||
import '../../models/data_models.dart';
|
||||
|
||||
/// 海报画廊页面 - 支持左右滑动浏览
|
||||
class PosterGalleryPage extends StatefulWidget {
|
||||
final List<MoviePoster> posters;
|
||||
final int initialIndex;
|
||||
|
||||
const PosterGalleryPage({
|
||||
super.key,
|
||||
required this.posters,
|
||||
required this.initialIndex,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PosterGalleryPage> createState() => _PosterGalleryPageState();
|
||||
}
|
||||
|
||||
class _PosterGalleryPageState extends State<PosterGalleryPage> {
|
||||
late PageController _pageController;
|
||||
late int _currentIndex;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentIndex = widget.initialIndex;
|
||||
_pageController = PageController(initialPage: widget.initialIndex);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_pageController.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
backgroundColor: Colors.black,
|
||||
body: Stack(
|
||||
children: [
|
||||
// 页面视图 - 支持左右滑动
|
||||
PageView.builder(
|
||||
controller: _pageController,
|
||||
itemCount: widget.posters.length,
|
||||
onPageChanged: (index) {
|
||||
setState(() => _currentIndex = index);
|
||||
},
|
||||
itemBuilder: (context, index) {
|
||||
final poster = widget.posters[index];
|
||||
return InteractiveViewer(
|
||||
minScale: 0.5,
|
||||
maxScale: 3.0,
|
||||
child: Center(
|
||||
child: Image.file(
|
||||
File(poster.posterPath),
|
||||
fit: BoxFit.contain,
|
||||
errorBuilder: (_, __, ___) => const Center(
|
||||
child: Icon(
|
||||
Icons.broken_image,
|
||||
color: Colors.white54,
|
||||
size: 64,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
},
|
||||
),
|
||||
|
||||
// 顶部导航栏
|
||||
Positioned(
|
||||
top: 0,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: SafeArea(
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 8),
|
||||
decoration: BoxDecoration(
|
||||
gradient: LinearGradient(
|
||||
begin: Alignment.topCenter,
|
||||
end: Alignment.bottomCenter,
|
||||
colors: [
|
||||
Colors.black.withOpacity(0.7),
|
||||
Colors.transparent,
|
||||
],
|
||||
),
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
// 返回按钮
|
||||
IconButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
icon: const Icon(Icons.arrow_back, color: Colors.white),
|
||||
),
|
||||
const Spacer(),
|
||||
// 页码指示器
|
||||
Text(
|
||||
'${_currentIndex + 1} / ${widget.posters.length}',
|
||||
style: const TextStyle(
|
||||
color: Colors.white,
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
),
|
||||
),
|
||||
const Spacer(),
|
||||
// 占位,保持对称
|
||||
const SizedBox(width: 48),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
|
||||
// 底部指示器(点状)
|
||||
if (widget.posters.length > 1)
|
||||
Positioned(
|
||||
bottom: 20,
|
||||
left: 0,
|
||||
right: 0,
|
||||
child: SafeArea(
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: List.generate(
|
||||
widget.posters.length,
|
||||
(index) => Container(
|
||||
width: 8,
|
||||
height: 8,
|
||||
margin: const EdgeInsets.symmetric(horizontal: 4),
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: index == _currentIndex
|
||||
? Colors.white
|
||||
: Colors.white.withOpacity(0.4),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user