You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.1 KiB

11 months ago
import 'dart:async';
import 'dart:math';
import 'package:camera/camera.dart';
import 'package:camera_aurora/camera_aurora.dart';
import 'package:camera_aurora/camera_viewfinder.dart';
import 'package:flutter/material.dart';
import 'package:qraurora/qr_code.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
late Future<CameraController> _controller;
StreamSubscription<String?>? _cameraSearchQrSubscription;
String? code;
Future<CameraController> _createController() async {
final cameras = await availableCameras();
if (cameras.isEmpty) {
throw CameraException("not found", null);
}
final camera = cameras.where((element) {
return element.lensDirection == CameraLensDirection.back;
}).first;
final controller = CameraController(
camera,
ResolutionPreset.medium,
imageFormatGroup: ImageFormatGroup.jpeg,
);
_cameraSearchQrSubscription = cameraSearchQr?.listen((text) {
if (text.isNotEmpty) {
setState(() {
code = text;
_cameraSearchQrSubscription?.cancel();
});
}
});
await controller.initialize();
return controller;
}
@override
void initState() {
super.initState();
_controller = _createController();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
FutureBuilder(
future: _controller,
builder: (context, snapshot) {
if (snapshot.hasData) {
if (code != null) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => QrCode(text: code!)));
}
return Expanded(
child: LayoutBuilder(builder: ((context, constraints) {
return AspectRatio(
aspectRatio: max(
constraints.maxHeight, constraints.maxWidth) /
min(constraints.maxHeight, constraints.maxWidth),
child: snapshot.data!.buildPreview());
})));
}
return const CircularProgressIndicator();
},
)
],
),
),
);
}
}