131 lines
3.7 KiB
Dart
Executable File
131 lines
3.7 KiB
Dart
Executable File
import 'package:camera/camera.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:translator_lux/config/config.dart';
|
|
import 'package:translator_lux/global/global_state.dart';
|
|
|
|
class CameraPage extends StatefulWidget {
|
|
const CameraPage({super.key});
|
|
|
|
@override
|
|
State<CameraPage> createState() => _CameraPageState();
|
|
}
|
|
|
|
class _CameraPageState extends State<CameraPage> {
|
|
late CameraController controller;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
controller =
|
|
CameraController(AppConfig.cameraList[0], ResolutionPreset.high);
|
|
controller.initialize().then((_) {
|
|
if (!mounted) {
|
|
return;
|
|
}
|
|
setState(() {});
|
|
}).catchError((Object e) {
|
|
if (e is CameraException) {
|
|
switch (e.code) {
|
|
case 'CameraAccessDenied':
|
|
// Handle access errors here.
|
|
break;
|
|
default:
|
|
// Handle other errors here.
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
super.dispose();
|
|
controller.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
if (!controller.value.isInitialized) {
|
|
return Container();
|
|
}
|
|
// 设备尺寸
|
|
final Size size = MediaQuery.of(context).size;
|
|
return Scaffold(
|
|
body: Stack(children: [
|
|
_cameraView(size),
|
|
SafeArea(
|
|
child: Padding(
|
|
padding: const EdgeInsets.symmetric(horizontal: 20),
|
|
child: Column(
|
|
children: [
|
|
Row(
|
|
children: [
|
|
GestureDetector(
|
|
onTap: () {
|
|
Get.back();
|
|
},
|
|
child: Container(
|
|
alignment: Alignment.center,
|
|
margin: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black,
|
|
borderRadius: BorderRadius.circular(30)),
|
|
child: const Padding(
|
|
padding: EdgeInsets.all(8.0),
|
|
child: Icon(
|
|
Icons.arrow_back_ios,
|
|
color: Colors.white,
|
|
size: 18,
|
|
),
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
alignment: Alignment.center,
|
|
margin: const EdgeInsets.all(10),
|
|
decoration: BoxDecoration(
|
|
color: Colors.black,
|
|
borderRadius: BorderRadius.circular(30)),
|
|
child: Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: SizedBox(
|
|
width: 100,
|
|
child: Row(
|
|
children: [
|
|
Text(
|
|
GlobalState.fromCountryName.value,
|
|
style: const TextStyle(
|
|
fontSize: 14, color: Colors.white),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
)
|
|
],
|
|
)
|
|
],
|
|
),
|
|
)),
|
|
]),
|
|
);
|
|
}
|
|
|
|
Widget _cameraView(Size size) {
|
|
// 设备像素比
|
|
final double deviceRatio = size.width / size.height;
|
|
// 相机纵横比
|
|
final double aspectRatio = controller.value.aspectRatio;
|
|
return Center(
|
|
child: Transform.scale(
|
|
scale: aspectRatio / deviceRatio,
|
|
child: AspectRatio(
|
|
aspectRatio: aspectRatio,
|
|
child: Center(child: CameraPreview(controller)),
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|