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.

157 lines
4.3 KiB

3 years ago
import 'package:flutter/material.dart';
import 'package:flutter_qr_reader/flutter_qr_reader.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:huixiang/generated/l10n.dart';
import 'package:huixiang/view_widget/my_appbar.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:huixiang/view_widget/tips_dialog.dart';
import 'package:image_picker_gallery_camera/image_picker_gallery_camera.dart';
import 'package:permission_handler/permission_handler.dart';
class QrCodeScanPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _QrCodeScanPage();
}
}
class _QrCodeScanPage extends State<QrCodeScanPage> {
GlobalKey<_QrCodeScanPage> qrViewKey = GlobalKey();
QrReaderViewController viewController;
@override
void initState() {
super.initState();
3 years ago
readerHeight = 812;
3 years ago
Future.delayed(Duration(seconds: 1), () {
buildReaderView();
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: Stack(
children: [
Positioned(
child: qrReaderView != null
? Column(
3 years ago
children: [
Expanded(
child: qrReaderView,
flex: readerHeight.toInt(),
),
// Expanded(child: qrReaderView, flex: readerHeight,),
],
)
3 years ago
: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
),
top: 0,
bottom: 0,
left: 0,
right: 0,
),
Positioned(
child: MyAppBar(
title: "扫码",
titleColor: Colors.white,
leadingColor: Colors.white,
brightness: Brightness.dark,
background: Colors.transparent,
action: Container(
alignment: Alignment.center,
margin: EdgeInsets.only(right: 16.w),
child: GestureDetector(
onTap: () {
openStorage();
},
child: Text(
S.of(context).xiangce,
style: TextStyle(
color: Colors.white,
fontSize: 18.sp,
fontWeight: FontWeight.bold,
),
),
),
),
),
top: 0,
left: 0,
right: 0,
),
],
),
),
);
}
QrReaderView qrReaderView;
3 years ago
double readerHeight;
3 years ago
buildReaderView() async {
double screenWidth = MediaQuery.of(context).size.width;
readerHeight = 1920.0 / 1080.0 * screenWidth;
print("object: $readerHeight");
qrReaderView = await QrReaderView(
key: qrViewKey,
callback: onScan,
width: MediaQuery.of(context).size.width,
height: readerHeight,
);
setState(() {});
}
///打开相册
openStorage() async {
if (await Permission.storage.isGranted) {
var image = await ImagePickerGC.pickImage(
context: context, source: ImgSource.Gallery);
String filePath = image.path;
final String data = await FlutterQrReader.imgScan(filePath);
showData(data);
} else {
await Permission.storage.request();
openStorage();
}
}
3 years ago
void _startCameraResult(String data, List<Offset> offset) {
3 years ago
showData(data);
this.viewController.stopCamera();
}
Future onScan(QrReaderViewController viewController) async {
this.viewController = viewController;
3 years ago
await this.viewController.startCamera(_startCameraResult);
3 years ago
}
showData(String data) {
SmartDialog.show(
3 years ago
widget: Tips(
() {},
text: "$data",
),
onDismiss: () {
Future.delayed(Duration(seconds: 1), () {
this.viewController.startCamera(_startCameraResult);
3 years ago
});
3 years ago
},
);
3 years ago
}
@override
void dispose() {
if (this.viewController != null) {
this.viewController.stopCamera();
}
super.dispose();
}
}