|
|
|
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_pickers/image_pickers.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 = 812;
|
|
|
|
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) {
|
|
|
|
List<Media> medias = await ImagePickers.pickerPaths(
|
|
|
|
galleryMode: GalleryMode.image,
|
|
|
|
selectCount: 1,
|
|
|
|
showGif: true,
|
|
|
|
showCamera: false,
|
|
|
|
compressSize: 500,
|
|
|
|
uiConfig: UIConfig(
|
|
|
|
uiThemeColor: Color(0xFFFFFFFF),
|
|
|
|
),
|
|
|
|
cropConfig: CropConfig(
|
|
|
|
enableCrop: true,
|
|
|
|
width: 200,
|
|
|
|
height: 200,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
if (medias == null || medias.length == 0) return;
|
|
|
|
String filePath = medias[0].path;
|
|
|
|
final String data = await FlutterQrReader.imgScan(filePath);
|
|
|
|
showData(data);
|
|
|
|
} else {
|
|
|
|
await Permission.storage.request();
|
|
|
|
openStorage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void _startCameraResult(String data, List<Offset> offset) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|