fmk
3 years ago
17 changed files with 499 additions and 52 deletions
@ -0,0 +1,153 @@
|
||||
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(); |
||||
} |
||||
} |
@ -0,0 +1,202 @@
|
||||
import 'dart:convert'; |
||||
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:flutter/rendering.dart'; |
||||
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; |
||||
import 'package:huixiang/qr/qr_share_image.dart'; |
||||
import 'package:huixiang/retrofit/data/user_info.dart'; |
||||
import 'package:huixiang/utils/font_weight.dart'; |
||||
import 'package:huixiang/view_widget/my_appbar.dart'; |
||||
import 'package:flutter_screenutil/flutter_screenutil.dart'; |
||||
import 'package:qr_flutter/qr_flutter.dart'; |
||||
import 'package:shared_preferences/shared_preferences.dart'; |
||||
|
||||
class QrSharePage extends StatefulWidget { |
||||
@override |
||||
State<StatefulWidget> createState() { |
||||
return _QrSharePage(); |
||||
} |
||||
} |
||||
|
||||
class _QrSharePage extends State<QrSharePage> { |
||||
String phone = ""; |
||||
|
||||
@override |
||||
void initState() { |
||||
super.initState(); |
||||
|
||||
SharedPreferences.getInstance().then((value) { |
||||
if (value.getString('user') != null && value.getString('user') != "") { |
||||
phone = UserInfo.fromJson(jsonDecode(value.getString('user'))).phone; |
||||
setState(() {}); |
||||
} |
||||
}); |
||||
|
||||
buildImageInfo(); |
||||
} |
||||
|
||||
@override |
||||
void dispose() { |
||||
super.dispose(); |
||||
SmartDialog.dismiss(); |
||||
} |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
if (height == null || height == 0) |
||||
height = MediaQuery.of(context).size.height; |
||||
return Scaffold( |
||||
appBar: MyAppBar( |
||||
title: "", |
||||
titleColor: Colors.black, |
||||
background: Color(0xFFF7F7F7), |
||||
leadingColor: Colors.black, |
||||
action: Container( |
||||
alignment: Alignment.center, |
||||
margin: EdgeInsets.only(right: 16.w), |
||||
child: InkWell( |
||||
onTap: () { |
||||
SmartDialog.show( |
||||
widget: QrShareImagePage("15827991714"),); |
||||
}, |
||||
child: Text( |
||||
"分享", |
||||
style: TextStyle( |
||||
color: Colors.black, |
||||
fontSize: 18.sp, |
||||
fontWeight: FontWeight.bold, |
||||
), |
||||
), |
||||
), |
||||
), |
||||
), |
||||
body: SingleChildScrollView( |
||||
child: Container( |
||||
height: height, |
||||
child: Stack( |
||||
children: [ |
||||
// if (image != null) |
||||
Positioned( |
||||
child: Image.asset( |
||||
"assets/image/qr_share_bg.png", |
||||
fit: BoxFit.fill, |
||||
), |
||||
top: 0, |
||||
left: 0, |
||||
bottom: 0, |
||||
right: 0, |
||||
), |
||||
Positioned( |
||||
child: Container( |
||||
width: MediaQuery.of(context).size.width, |
||||
height: height * 0.635, |
||||
padding: |
||||
EdgeInsets.symmetric(vertical: 28.h, horizontal: 19.w), |
||||
child: Stack( |
||||
children: [ |
||||
Positioned( |
||||
child: Image.asset( |
||||
"assets/image/qr_share_info_bg.png", |
||||
fit: BoxFit.fill, |
||||
), |
||||
top: 0, |
||||
left: 0, |
||||
bottom: 0, |
||||
right: 0, |
||||
), |
||||
Column( |
||||
mainAxisAlignment: MainAxisAlignment.spaceEvenly, |
||||
crossAxisAlignment: CrossAxisAlignment.center, |
||||
children: [ |
||||
shareTypeTitle("方式一"), |
||||
Container( |
||||
margin: EdgeInsets.only(left: 16.w, right: 24.w), |
||||
child: Text( |
||||
"截屏保存下方二维码,邀请他人微信扫一扫识别,长按关注后,完成小游戏,领取优惠券后,即邀请成功哦!", |
||||
textAlign: TextAlign.center, |
||||
style: TextStyle( |
||||
color: Color(0xFF2E3552), |
||||
fontSize: 16.sp, |
||||
fontWeight: MyFontWeight.regular, |
||||
), |
||||
), |
||||
), |
||||
Container( |
||||
width: 152.w, |
||||
height: 152.w, |
||||
decoration: BoxDecoration( |
||||
border: Border.all( |
||||
color: Color(0xFF2E3552), width: 1), |
||||
), |
||||
child: QrImage( |
||||
data: phone ?? "", |
||||
version: QrVersions.auto, |
||||
size: 200.w, |
||||
gapless: true, |
||||
), |
||||
), |
||||
shareTypeTitle("方式二"), |
||||
Container( |
||||
margin: EdgeInsets.only(left: 16.w, right: 24.w), |
||||
child: Text( |
||||
"也可以直接点击右上方的分享给到你想要邀请的人。", |
||||
textAlign: TextAlign.center, |
||||
style: TextStyle( |
||||
color: Color(0xFF2E3552), |
||||
fontSize: 16.sp, |
||||
fontWeight: MyFontWeight.regular, |
||||
), |
||||
), |
||||
), |
||||
SizedBox( |
||||
height: 1.h, |
||||
), |
||||
], |
||||
), |
||||
], |
||||
), |
||||
), |
||||
left: 0, |
||||
bottom: 0, |
||||
right: 0, |
||||
), |
||||
], |
||||
), |
||||
), |
||||
), |
||||
); |
||||
} |
||||
|
||||
Widget shareTypeTitle(typeText) { |
||||
return Container( |
||||
width: 90.w, |
||||
height: 30.h, |
||||
child: Text( |
||||
typeText, |
||||
style: TextStyle( |
||||
fontWeight: MyFontWeight.semi_bold, |
||||
fontSize: 18.sp, |
||||
color: Color(0xFF2E3552), |
||||
), |
||||
), |
||||
alignment: Alignment.center, |
||||
decoration: BoxDecoration( |
||||
color: Color(0xFFFFC93B), |
||||
border: Border.all(color: Color(0xFF2E3552), width: 1), |
||||
), |
||||
); |
||||
} |
||||
|
||||
Image image; |
||||
double height; |
||||
|
||||
buildImageInfo() async { |
||||
image = Image.asset("assets/image/qr_share_bg.png"); |
||||
image.image |
||||
.resolve(ImageConfiguration()) |
||||
.addListener(ImageStreamListener((ImageInfo info, bool _) { |
||||
height = info.image.height.toDouble(); |
||||
setState(() {}); |
||||
})); |
||||
} |
||||
} |
@ -0,0 +1,45 @@
|
||||
|
||||
import 'package:flutter/material.dart'; |
||||
import 'package:flutter/rendering.dart'; |
||||
import 'package:flutter/services.dart'; |
||||
import 'dart:ui' as ui; |
||||
|
||||
class QrShareImagePage extends StatefulWidget { |
||||
|
||||
String mobile; |
||||
|
||||
QrShareImagePage(this.mobile); |
||||
|
||||
@override |
||||
State<StatefulWidget> createState() { |
||||
return _QrShareImagePage(); |
||||
} |
||||
|
||||
} |
||||
|
||||
class _QrShareImagePage extends State<QrShareImagePage> { |
||||
|
||||
@override |
||||
Widget build(BuildContext context) { |
||||
widgetToImage(); |
||||
return Container( |
||||
width: 200, |
||||
height: 300, |
||||
color: Colors.white.withAlpha(123), |
||||
child: Text(widget.mobile), |
||||
); |
||||
} |
||||
|
||||
GlobalKey globalKey = GlobalKey(); |
||||
|
||||
widgetToImage() async { |
||||
BuildContext buildContext = globalKey.currentContext; |
||||
if (null != buildContext) { |
||||
RenderRepaintBoundary boundary = buildContext.findRenderObject(); |
||||
ui.Image image = await boundary.toImage(); |
||||
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png); |
||||
// byteData.buffer.asUint16List() |
||||
} |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue