115 lines
3.4 KiB
Dart
115 lines
3.4 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:path/path.dart' as p;
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:thermion_flutter/thermion_flutter.dart';
|
|
|
|
class Cup extends StatefulWidget {
|
|
const Cup({super.key});
|
|
|
|
@override
|
|
State<Cup> createState() => _CupState();
|
|
}
|
|
|
|
class _CupState extends State<Cup> {
|
|
bool _isLoading = true;
|
|
String? _modelPath, _modelUri;
|
|
String _statusMessage = "正在加载本地模型...";
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadLocalModel();
|
|
}
|
|
|
|
// 从应用支持目录加载模型
|
|
Future<void> _loadLocalModel() async {
|
|
try {
|
|
setState(() {
|
|
_statusMessage = "获取本地模型路径...";
|
|
});
|
|
|
|
final supportDir = await getApplicationSupportDirectory();
|
|
final modelFile = File(p.join(supportDir.path, 'cup', 'model.gltf'));
|
|
|
|
String modelPath =
|
|
File(p.join(supportDir.path, 'cup', 'model.gltf')).absolute.path;
|
|
final cupDir = Directory('${supportDir.path}/cup');
|
|
final modelUri = File('${cupDir.path}/model.gltf').uri.toString();
|
|
print('加载本地模modelPath: $modelPath');
|
|
print('加载本地模modelUri: $modelUri');
|
|
|
|
if (await modelFile.exists()) {
|
|
setState(() {
|
|
_modelPath = modelPath;
|
|
_modelUri = modelUri;
|
|
|
|
_isLoading = false;
|
|
_statusMessage = "本地模型加载完成";
|
|
});
|
|
} else {
|
|
setState(() {
|
|
_isLoading = false;
|
|
_statusMessage = "本地模型文件不存在";
|
|
});
|
|
}
|
|
} catch (e) {
|
|
print('加载本地模型失败: $e');
|
|
setState(() {
|
|
_isLoading = false;
|
|
_statusMessage = "加载失败: $e";
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('本地模型查看'),
|
|
),
|
|
body: _isLoading
|
|
? Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const CircularProgressIndicator(),
|
|
const SizedBox(height: 16),
|
|
Text(_statusMessage),
|
|
],
|
|
),
|
|
)
|
|
: _modelPath == null
|
|
? Center(
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
const Icon(Icons.error, size: 48, color: Colors.red),
|
|
const SizedBox(height: 16),
|
|
Text(_statusMessage),
|
|
const SizedBox(height: 16),
|
|
ElevatedButton(
|
|
onPressed: _loadLocalModel,
|
|
child: const Text('重试'),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: ViewerWidget(
|
|
// assetPath: "file://$_modelPath",
|
|
assetPath: _modelUri,
|
|
skyboxPath: "assets/default_env_skybox.ktx",
|
|
iblPath: "assets/default_env_ibl.ktx",
|
|
transformToUnitCube: true,
|
|
initialCameraPosition: Vector3(0, 0, 6),
|
|
background: Colors.grey[200],
|
|
manipulatorType: ManipulatorType.ORBIT,
|
|
initial: const Center(
|
|
child: CircularProgressIndicator(),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|