220 lines
6.3 KiB
Dart
220 lines
6.3 KiB
Dart
import 'package:ar_tourism_flutter_unity/components/CustomAlertDialog.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'package:wechat_assets_picker/wechat_assets_picker.dart';
|
||
import '../../apis/app.dart';
|
||
import '../../components/work_edit.dart';
|
||
import '../../components/work_utils.dart';
|
||
|
||
class CenterPage extends StatefulWidget {
|
||
final AssetEntity? selectedAsset;
|
||
|
||
const CenterPage({
|
||
super.key,
|
||
this.selectedAsset,
|
||
});
|
||
|
||
@override
|
||
CenterPageState createState() => CenterPageState();
|
||
}
|
||
|
||
// 将State类更改为公开的,以便外部访问
|
||
class CenterPageState extends State<CenterPage> {
|
||
AssetEntity? _currentAsset;
|
||
bool _didRunInitialCheck = false;
|
||
bool _isLoading = false; // 添加加载状态
|
||
|
||
@override
|
||
void initState() {
|
||
super.initState();
|
||
_currentAsset = widget.selectedAsset;
|
||
if (_currentAsset != null) {
|
||
_preloadAsset();
|
||
}
|
||
}
|
||
|
||
// 预加载资源
|
||
Future<void> _preloadAsset() async {
|
||
if (_currentAsset == null) return;
|
||
setState(() => _isLoading = true);
|
||
try {
|
||
await _currentAsset!.file; // 预加载文件
|
||
await _currentAsset!.thumbnailData; // 预加载缩略图
|
||
} catch (e) {
|
||
print('资源预加载失败: $e');
|
||
} finally {
|
||
if (mounted) {
|
||
setState(() => _isLoading = false);
|
||
}
|
||
}
|
||
}
|
||
|
||
@override
|
||
void didUpdateWidget(CenterPage oldWidget) {
|
||
super.didUpdateWidget(oldWidget);
|
||
if (widget.selectedAsset != oldWidget.selectedAsset) {
|
||
_currentAsset = widget.selectedAsset;
|
||
if (_currentAsset != null) {
|
||
_preloadAsset();
|
||
}
|
||
}
|
||
}
|
||
|
||
@override
|
||
void didChangeDependencies() {
|
||
super.didChangeDependencies();
|
||
|
||
if (!_didRunInitialCheck) {
|
||
_didRunInitialCheck = true;
|
||
|
||
final routeName = ModalRoute.of(context)?.settings.name;
|
||
final isFromAssetPicker = routeName == 'asset_picker_result';
|
||
final isTabsRoute = routeName == '/tabs';
|
||
|
||
// 如果有资源且是从图片选择器来的,或者没有资源但不是从底部导航栏直接进入的
|
||
if (_currentAsset != null) {
|
||
_checkAuthStatus();
|
||
} else if (!isTabsRoute && !isFromAssetPicker) {
|
||
// 立即打开选择器,不添加延迟,避免闪烁
|
||
_openAssetPicker();
|
||
}
|
||
}
|
||
}
|
||
|
||
Future<bool> _checkRealNameAuthStatus() async {
|
||
try {
|
||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||
String phone = prefs.getString('phone') ?? '';
|
||
|
||
if (phone.isEmpty) {
|
||
return false;
|
||
}
|
||
|
||
// 调用API获取用户信息(包括认证状态)
|
||
final response = await userApi.getSetting(phone);
|
||
print("调用API获取用户信息(包括认证状态),${response}");
|
||
|
||
if (response['code'] == 200) {
|
||
// 获取认证状态
|
||
bool isAuthenticated = response['authenticationStatus'] == '1';
|
||
|
||
// 更新本地存储
|
||
await prefs.setBool('isRealNameAuth', isAuthenticated);
|
||
|
||
print('从服务器获取的认证状态: $isAuthenticated');
|
||
return isAuthenticated;
|
||
}
|
||
return false;
|
||
} catch (e) {
|
||
print('检查认证状态失败: $e');
|
||
// 出错时返回本地存储的状态(作为后备方案)
|
||
SharedPreferences prefs = await SharedPreferences.getInstance();
|
||
return prefs.getBool('isRealNameAuth') ?? false;
|
||
}
|
||
}
|
||
|
||
Future<void> _checkAuthStatus() async {
|
||
if (_currentAsset == null) return; // 如果是从其他页面进入,不需要检查
|
||
|
||
// 使用方法检查认证状态
|
||
bool isRealNameAuth = await _checkRealNameAuthStatus();
|
||
|
||
if (!isRealNameAuth && mounted) {
|
||
// 用户未认证,显示对话框
|
||
showDialog(
|
||
context: context,
|
||
barrierDismissible: false,
|
||
builder: (BuildContext context) {
|
||
return CustomAlertDialog(
|
||
title: '需要实名认证',
|
||
content: '发布作品需要先完成实名认证',
|
||
onConfirm: () {
|
||
Navigator.of(context).pop();
|
||
Navigator.pushNamed(context, '/realName');
|
||
},
|
||
onCancel: () {
|
||
Navigator.of(context).pop();
|
||
Navigator.pushNamedAndRemoveUntil(
|
||
context,
|
||
'/tabs',
|
||
(route) => false,
|
||
arguments: {'initialIndex': 0},
|
||
);
|
||
},
|
||
confirmText: '去认证',
|
||
cancelText: '取消',
|
||
);
|
||
},
|
||
);
|
||
}
|
||
}
|
||
|
||
Future<void> _openAssetPicker() async {
|
||
// 防止已经有资源的情况下再次打开选择器
|
||
if (_currentAsset != null) return;
|
||
|
||
final List<AssetEntity>? result = await AssetPicker.pickAssets(
|
||
context,
|
||
pickerConfig: const AssetPickerConfig(
|
||
textDelegate: AssetPickerTextDelegate(),
|
||
maxAssets: 1,
|
||
themeColor: Color(0xff7CDBC8),
|
||
),
|
||
);
|
||
|
||
if (result != null && result.isNotEmpty && mounted) {
|
||
setState(() {
|
||
_currentAsset = result.first;
|
||
});
|
||
_checkAuthStatus();
|
||
} else if (mounted) {
|
||
// 如果用户取消选择,返回到首页
|
||
Navigator.pushNamedAndRemoveUntil(
|
||
context,
|
||
'/tabs',
|
||
(route) => false,
|
||
arguments: {'initialIndex': 0},
|
||
);
|
||
}
|
||
}
|
||
|
||
// 更新选中的资源
|
||
void updateSelectedAsset(AssetEntity? asset) {
|
||
if (mounted && asset != null) {
|
||
setState(() {
|
||
_currentAsset = asset;
|
||
});
|
||
_preloadAsset(); // 预加载新资源
|
||
_checkAuthStatus(); // 检查认证状态
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
appBar: AppBar(
|
||
leading: IconButton(
|
||
icon: Image.asset('images/close.png', width: 16, height: 16),
|
||
onPressed: () => Navigator.pushNamed(context, '/tabs'),
|
||
),
|
||
),
|
||
body: SafeArea(
|
||
child: Container(
|
||
color: const Color(0xff0C0C16),
|
||
child: _isLoading
|
||
? const Center(
|
||
child: CircularProgressIndicator(
|
||
valueColor: AlwaysStoppedAnimation<Color>(Color(0xff79DDED)),
|
||
),
|
||
)
|
||
: WorkEditor(
|
||
selectedAsset: _currentAsset,
|
||
isEditMode: false,
|
||
),
|
||
),
|
||
),
|
||
resizeToAvoidBottomInset: false,
|
||
);
|
||
}
|
||
}
|