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.
154 lines
4.2 KiB
154 lines
4.2 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();
|
||
|
|
||
|
readerHeight = MediaQuery.of(context).size.height;
|
||
|
Future.delayed(Duration(seconds: 1), () {
|
||
|
buildReaderView();
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
body: Container(
|
||
|
child: Stack(
|
||
|
children: [
|
||
|
Positioned(
|
||
|
child: qrReaderView != null
|
||
|
? Column(
|
||
|
children: [
|
||
|
Expanded(child: qrReaderView, flex: readerHeight.toInt(),),
|
||
|
// Expanded(child: qrReaderView, flex: readerHeight,),
|
||
|
|
||
|
],
|
||
|
)
|
||
|
: 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;
|
||
|
double readerHeight ;
|
||
|
|
||
|
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();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
startCameraResult(String data, List<Offset> offset) async {
|
||
|
showData(data);
|
||
|
this.viewController.stopCamera();
|
||
|
}
|
||
|
|
||
|
Future onScan(QrReaderViewController viewController) async {
|
||
|
this.viewController = viewController;
|
||
|
await this.viewController.startCamera(startCameraResult);
|
||
|
}
|
||
|
|
||
|
showData(String data) {
|
||
|
SmartDialog.show(
|
||
|
widget: Tips(
|
||
|
() {},
|
||
|
text: "$data",
|
||
|
),
|
||
|
onDismiss: () {
|
||
|
Future.delayed(Duration(seconds: 1), () {
|
||
|
this.viewController.startCamera(startCameraResult);
|
||
|
});
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
void dispose() {
|
||
|
if (this.viewController != null) {
|
||
|
this.viewController.stopCamera();
|
||
|
}
|
||
|
super.dispose();
|
||
|
}
|
||
|
}
|