generated from dellevin/template
继续优化
This commit is contained in:
@@ -172,8 +172,6 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
_buildInfoRow('标题', movieInfo['title']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('年份', movieInfo['year']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('评分', movieInfo['rating']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('导演', movieInfo['director']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('类型', movieInfo['genres']?.toString() ?? '未提取到'),
|
||||
_buildInfoRow('上映日期', movieInfo['releaseDate']?.toString() ?? '未提取到'),
|
||||
@@ -273,7 +271,19 @@ class _DoubanWebViewPageState extends State<DoubanWebViewPage> {
|
||||
info.year = '';
|
||||
}
|
||||
|
||||
|
||||
// 获取封面图 - 从 sub-cover 中的 img 标签获取
|
||||
const coverEl = document.querySelector('.sub-cover img');
|
||||
if (coverEl) {
|
||||
let coverUrl = coverEl.src;
|
||||
// 将 webp 转换为 jpg 格式,提高兼容性
|
||||
if (coverUrl && coverUrl.includes('.webp')) {
|
||||
coverUrl = coverUrl.replace('.webp', '.jpg');
|
||||
}
|
||||
info.coverUrl = coverUrl;
|
||||
} else {
|
||||
info.coverUrl = '';
|
||||
}
|
||||
|
||||
// 获取评分 - 移动版可能在 mark-item 中
|
||||
const ratingEl = document.querySelector('.rating-num') || document.querySelector('.score');
|
||||
info.rating = ratingEl ? ratingEl.textContent.trim() : '';
|
||||
|
||||
@@ -4,6 +4,7 @@ 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:http/http.dart' as http;
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
@@ -150,7 +151,7 @@ class _MovieFormPageState extends State<MovieFormPage> {
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'请输入豆瓣影视链接:',
|
||||
'请输入分享的豆瓣影视链接:',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF666666),
|
||||
@@ -307,6 +308,11 @@ class _MovieFormPageState extends State<MovieFormPage> {
|
||||
}
|
||||
}
|
||||
|
||||
// 下载封面图
|
||||
if (info['coverUrl'] != null && info['coverUrl'].toString().isNotEmpty) {
|
||||
_downloadCoverFromUrl(info['coverUrl'].toString());
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
// 显示成功提示
|
||||
@@ -317,6 +323,57 @@ class _MovieFormPageState extends State<MovieFormPage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 从URL下载封面图
|
||||
Future<void> _downloadCoverFromUrl(String url) async {
|
||||
try {
|
||||
// 下载网络图片,添加请求头模拟浏览器
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1',
|
||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8',
|
||||
'Referer': 'https://movie.douban.com/',
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('下载失败: HTTP ${response.statusCode}');
|
||||
}
|
||||
|
||||
// 检查内容类型
|
||||
final contentType = response.headers['content-type'];
|
||||
if (contentType != null && !contentType.startsWith('image/')) {
|
||||
throw Exception('链接返回的不是图片');
|
||||
}
|
||||
|
||||
// 检查文件大小(最大 10MB)
|
||||
if (response.bodyBytes.length > 10 * 1024 * 1024) {
|
||||
throw Exception('图片太大');
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
final fileName = 'poster_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
|
||||
// 如果是编辑模式,使用现有影视ID;如果是新建模式,使用临时ID
|
||||
final movieId = widget.movie?.id ?? DateTime.now().millisecondsSinceEpoch.toString();
|
||||
|
||||
// 保存到新的路径结构
|
||||
final targetPath = await ImagePathHelper.instance.getMoviePosterPath(
|
||||
movieId,
|
||||
fileName
|
||||
);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
|
||||
// 写入文件
|
||||
await File(targetPath).writeAsBytes(response.bodyBytes);
|
||||
|
||||
setState(() => _posterPath = targetPath);
|
||||
} catch (e) {
|
||||
// 封面下载失败不影响其他信息填充
|
||||
debugPrint('封面下载失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final isEdit = widget.movie != null;
|
||||
@@ -1065,6 +1122,15 @@ class _MovieFormPageState extends State<MovieFormPage> {
|
||||
_pickCover();
|
||||
},
|
||||
),
|
||||
// 网络链接选项
|
||||
_buildCoverOption(
|
||||
icon: Icons.link_outlined,
|
||||
title: '网络链接',
|
||||
onTap: () {
|
||||
Navigator.pop(context);
|
||||
_pickCoverFromUrl();
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
@@ -1173,6 +1239,122 @@ class _MovieFormPageState extends State<MovieFormPage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 从网络链接选择封面
|
||||
Future<void> _pickCoverFromUrl() async {
|
||||
final urlController = TextEditingController();
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('添加网络图片'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'请输入图片链接地址',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: urlController,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'https://movie.douban.com/image.jpg',
|
||||
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
|
||||
border: UnderlineInputBorder(),
|
||||
enabledBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(color: Color(0xFFE5E5E5)),
|
||||
),
|
||||
focusedBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(color: Color(0xFF1A1A1A)),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
keyboardType: TextInputType.url,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('确定', style: TextStyle(color: Color(0xFF1A1A1A))),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
final url = urlController.text.trim();
|
||||
if (url.isEmpty) {
|
||||
ToastUtil.show(context, '请输入图片链接');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 下载网络图片,添加请求头模拟浏览器
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
||||
'Referer': Uri.parse(url).replace(path: '/').toString(),
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('下载失败: HTTP ${response.statusCode}');
|
||||
}
|
||||
|
||||
// 检查内容类型
|
||||
final contentType = response.headers['content-type'];
|
||||
if (contentType != null && !contentType.startsWith('image/')) {
|
||||
throw Exception('链接返回的不是图片,可能是网页或需要登录');
|
||||
}
|
||||
|
||||
// 检查文件大小(最大 10MB)
|
||||
if (response.bodyBytes.length > 10 * 1024 * 1024) {
|
||||
throw Exception('图片太大,请使用小于 10MB 的图片');
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
final fileName = 'poster_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
|
||||
// 如果是编辑模式,使用现有影视ID;如果是新建模式,使用临时ID
|
||||
final movieId = widget.movie?.id ?? DateTime.now().millisecondsSinceEpoch.toString();
|
||||
|
||||
// 保存到新的路径结构
|
||||
final targetPath = await ImagePathHelper.instance.getMoviePosterPath(
|
||||
movieId,
|
||||
fileName
|
||||
);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
|
||||
// 写入文件
|
||||
await File(targetPath).writeAsBytes(response.bodyBytes);
|
||||
|
||||
setState(() => _posterPath = targetPath);
|
||||
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '添加成功');
|
||||
}
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '添加失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 选择上映日期
|
||||
Future<void> _selectReleaseDate() async {
|
||||
final picked = await showDatePicker(
|
||||
|
||||
@@ -6,6 +6,7 @@ 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 'package:http/http.dart' as http;
|
||||
import '../../providers/app_provider.dart';
|
||||
import '../../models/data_models.dart';
|
||||
import '../../utils/toast_util.dart';
|
||||
@@ -206,6 +207,77 @@ class _MoviePostersPageState extends State<MoviePostersPage> {
|
||||
}
|
||||
|
||||
Future<void> _pickPoster() async {
|
||||
// 显示选择对话框
|
||||
final result = await showModalBottomSheet<int>(
|
||||
context: context,
|
||||
backgroundColor: Colors.white,
|
||||
shape: const RoundedRectangleBorder(
|
||||
borderRadius: BorderRadius.vertical(top: Radius.circular(16)),
|
||||
),
|
||||
builder: (context) => SafeArea(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(vertical: 16),
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
// 顶部指示条
|
||||
Container(
|
||||
width: 40,
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFE0E0E0),
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 20),
|
||||
// 标题
|
||||
const Padding(
|
||||
padding: EdgeInsets.symmetric(horizontal: 24),
|
||||
child: Row(
|
||||
children: [
|
||||
Text(
|
||||
'添加海报',
|
||||
style: TextStyle(
|
||||
fontSize: 18,
|
||||
fontWeight: FontWeight.w600,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
// 从相册选择
|
||||
_buildAddOption(
|
||||
icon: Icons.photo_library_outlined,
|
||||
title: '从相册选择',
|
||||
subtitle: '选择本地图片',
|
||||
onTap: () => Navigator.pop(context, 0),
|
||||
),
|
||||
// 网络链接
|
||||
_buildAddOption(
|
||||
icon: Icons.link_outlined,
|
||||
title: '网络链接',
|
||||
subtitle: '输入图片URL地址',
|
||||
onTap: () => Navigator.pop(context, 1),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
if (result == null) return;
|
||||
|
||||
if (result == 0) {
|
||||
await _pickFromGallery();
|
||||
} else if (result == 1) {
|
||||
await _pickFromUrl();
|
||||
}
|
||||
}
|
||||
|
||||
/// 从相册选择
|
||||
Future<void> _pickFromGallery() async {
|
||||
try {
|
||||
final XFile? pickedFile = await _picker.pickImage(
|
||||
source: ImageSource.gallery,
|
||||
@@ -248,6 +320,198 @@ class _MoviePostersPageState extends State<MoviePostersPage> {
|
||||
}
|
||||
}
|
||||
|
||||
/// 从网络链接添加
|
||||
Future<void> _pickFromUrl() async {
|
||||
final urlController = TextEditingController();
|
||||
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
backgroundColor: Colors.white,
|
||||
elevation: 0,
|
||||
shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
|
||||
title: const Text('添加网络图片'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'请输入图片链接地址',
|
||||
style: TextStyle(
|
||||
fontSize: 14,
|
||||
color: Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: urlController,
|
||||
decoration: const InputDecoration(
|
||||
hintText: 'https://example.com/image.jpg',
|
||||
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
|
||||
border: UnderlineInputBorder(),
|
||||
enabledBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(color: Color(0xFFE5E5E5)),
|
||||
),
|
||||
focusedBorder: UnderlineInputBorder(
|
||||
borderSide: BorderSide(color: Color(0xFF1A1A1A)),
|
||||
),
|
||||
),
|
||||
style: const TextStyle(fontSize: 14),
|
||||
keyboardType: TextInputType.url,
|
||||
),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('取消', style: TextStyle(color: Color(0xFF666666))),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('确定', style: TextStyle(color: Color(0xFF1A1A1A))),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
|
||||
if (confirmed != true) return;
|
||||
|
||||
final url = urlController.text.trim();
|
||||
if (url.isEmpty) {
|
||||
ToastUtil.show(context, '请输入图片链接');
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// 下载网络图片
|
||||
await _downloadAndSavePoster(url);
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ToastUtil.show(context, '添加失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 下载网络图片并保存
|
||||
Future<void> _downloadAndSavePoster(String url) async {
|
||||
try {
|
||||
// 使用 http 下载图片,添加请求头模拟浏览器
|
||||
final response = await http.get(
|
||||
Uri.parse(url),
|
||||
headers: {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
|
||||
'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
|
||||
'Referer': Uri.parse(url).replace(path: '/').toString(),
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode != 200) {
|
||||
throw Exception('下载失败: HTTP ${response.statusCode}');
|
||||
}
|
||||
|
||||
// 检查内容类型
|
||||
final contentType = response.headers['content-type'];
|
||||
if (contentType != null && !contentType.startsWith('image/')) {
|
||||
throw Exception('链接返回的不是图片,可能是网页或需要登录');
|
||||
}
|
||||
|
||||
// 检查文件大小(最大 10MB)
|
||||
if (response.bodyBytes.length > 10 * 1024 * 1024) {
|
||||
throw Exception('图片太大,请使用小于 10MB 的图片');
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
final fileName = 'posterimg_${DateTime.now().millisecondsSinceEpoch}.jpg';
|
||||
|
||||
// 保存到 posterimgs 子目录
|
||||
final targetPath = await ImagePathHelper.instance.getMoviePosterImgPath(
|
||||
widget.movie.id,
|
||||
fileName
|
||||
);
|
||||
await ImagePathHelper.instance.ensureDirExists(p.dirname(targetPath));
|
||||
|
||||
// 写入文件
|
||||
await File(targetPath).writeAsBytes(response.bodyBytes);
|
||||
|
||||
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) {
|
||||
throw Exception('下载图片失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 构建添加选项
|
||||
Widget _buildAddOption({
|
||||
required IconData icon,
|
||||
required String title,
|
||||
required String subtitle,
|
||||
required VoidCallback onTap,
|
||||
}) {
|
||||
return InkWell(
|
||||
onTap: onTap,
|
||||
child: Container(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 14),
|
||||
child: Row(
|
||||
children: [
|
||||
Container(
|
||||
width: 44,
|
||||
height: 44,
|
||||
decoration: BoxDecoration(
|
||||
color: const Color(0xFFF5F5F5),
|
||||
borderRadius: BorderRadius.circular(10),
|
||||
),
|
||||
child: Icon(
|
||||
icon,
|
||||
size: 22,
|
||||
color: const Color(0xFF666666),
|
||||
),
|
||||
),
|
||||
const SizedBox(width: 16),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(
|
||||
title,
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.w500,
|
||||
color: Color(0xFF1A1A1A),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 2),
|
||||
Text(
|
||||
subtitle,
|
||||
style: const TextStyle(
|
||||
fontSize: 13,
|
||||
color: Color(0xFF999999),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
const Icon(
|
||||
Icons.chevron_right,
|
||||
color: Color(0xFFCCCCCC),
|
||||
size: 20,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showDeleteDialog(MoviePoster poster) {
|
||||
showDialog(
|
||||
context: context,
|
||||
|
||||
Reference in New Issue
Block a user