修复扫描没有声音的问题

master
laiiihz 4 years ago
parent 06fd828697
commit dcddc6bc90

@ -5,6 +5,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_qr_reader/flutter_qr_reader.dart'; import 'package:flutter_qr_reader/flutter_qr_reader.dart';
import 'package:flutter_qr_reader/qrcode_reader_view.dart'; import 'package:flutter_qr_reader/qrcode_reader_view.dart';
import 'package:image_picker/image_picker.dart'; import 'package:image_picker/image_picker.dart';
import 'package:just_audio/just_audio.dart';
import 'package:recook/base/base_store_state.dart'; import 'package:recook/base/base_store_state.dart';
import 'package:recook/constants/api.dart'; import 'package:recook/constants/api.dart';
@ -20,17 +21,16 @@ import 'package:recook/utils/text_utils.dart';
// import 'package:flutter_audio_player/flutter_audio_player.dart'; // import 'package:flutter_audio_player/flutter_audio_player.dart';
class BarcodeScanPage extends StatefulWidget { class BarcodeScanPage extends StatefulWidget {
@override @override
State<BarcodeScanPage> createState() { State<BarcodeScanPage> createState() {
return _BarcodeScanPageState(); return _BarcodeScanPageState();
} }
} }
class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> { class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
String barcode = ""; String barcode = "";
GlobalKey<QrcodeReaderViewState> _key = GlobalKey(); GlobalKey<QrcodeReaderViewState> _key = GlobalKey();
final player = AudioPlayer();
@override @override
void initState() { void initState() {
super.initState(); super.initState();
@ -46,12 +46,13 @@ class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
QrcodeReaderView( QrcodeReaderView(
key: _key, key: _key,
helpWidget: Text("请将条形码置于方框中间"), helpWidget: Text("请将条形码置于方框中间"),
onEdit: (){ onEdit: () {
AppRouter.pushAndReplaced(context, RouteName.BARCODE_INPUT); AppRouter.pushAndReplaced(context, RouteName.BARCODE_INPUT);
}, },
onSelectImage: () async { onSelectImage: () async {
_key.currentState.stopScan(); _key.currentState.stopScan();
var image = await ImagePicker.pickImage(source: ImageSource.gallery); var image =
await ImagePicker.pickImage(source: ImageSource.gallery);
if (image == null) { if (image == null) {
_key.currentState.startScan(); _key.currentState.startScan();
return; return;
@ -63,11 +64,11 @@ class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
} }
File imageFile = cropFile; File imageFile = cropFile;
final rest = await FlutterQrReader.imgScan(imageFile); final rest = await FlutterQrReader.imgScan(imageFile);
if (TextUtils.isEmpty(rest)){ if (TextUtils.isEmpty(rest)) {
showError("图片识别失败...").then((v){ showError("图片识别失败...").then((v) {
_key.currentState.startScan(); _key.currentState.startScan();
}); });
}else{ } else {
onScan(rest, image: imageFile); onScan(rest, image: imageFile);
} }
}, },
@ -78,60 +79,57 @@ class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
), ),
), ),
Positioned( Positioned(
child: Center( child: Center(
child: Container( child: Container(
decoration: BoxDecoration( decoration: BoxDecoration(
color: lineColor, color: lineColor,
borderRadius: BorderRadius.circular(0.5), borderRadius: BorderRadius.circular(0.5),
boxShadow: [ boxShadow: [
BoxShadow( BoxShadow(
color: lineColor, color: lineColor,
offset: Offset(0, 1), offset: Offset(0, 1),
blurRadius: 5, blurRadius: 5,
spreadRadius: 1.0 spreadRadius: 1.0),
), BoxShadow(
BoxShadow(
color: lineColor, color: lineColor,
offset: Offset(0, -1), offset: Offset(0, -1),
blurRadius: 5, blurRadius: 5,
spreadRadius: 1.0 spreadRadius: 1.0),
), ],
],
),
width: width*0.8,
height: 1.7,
), ),
) width: width * 0.8,
), height: 1.7,
),
)),
], ],
), ),
); );
} }
Future onScan(String data, {File image}) async{ Future onScan(String data, {File image}) async {
if (!TextUtils.isEmpty(data)){ if (!TextUtils.isEmpty(data)) {
_playSound(); _playSound();
_key.currentState.stopScan(); _key.currentState.stopScan();
Future.delayed(Duration(milliseconds: 500 ), (){ Future.delayed(Duration(milliseconds: 500), () {
_getGoodsWithCode(data, (goodsId){ _getGoodsWithCode(data, (goodsId) {
AppRouter.pushAndReplaced(globalContext, RouteName.COMMODITY_PAGE, arguments: CommodityDetailPage.setArguments(int.parse(goodsId))); AppRouter.pushAndReplaced(globalContext, RouteName.COMMODITY_PAGE,
arguments: CommodityDetailPage.setArguments(int.parse(goodsId)));
return; return;
}, image: image); }, image: image);
}); });
}else{ } else {
_key.currentState.startScan(); _key.currentState.startScan();
} }
} }
_playSound() async { _playSound() async {
// AudioPlayer.addSound("assets/sound/recook_scan.mp3"); player.setAsset('assets/sound/recook_scan.mp3');
player.play();
} }
_getGoodsWithCode(String code, Function callBack, {File image}) async { _getGoodsWithCode(String code, Function callBack, {File image}) async {
ResultData resultData = await HttpManager.post(GoodsApi.goods_code_search, { ResultData resultData = await HttpManager.post(GoodsApi.goods_code_search, {
"code":code, "code": code,
}); });
if (!resultData.result) { if (!resultData.result) {
pushToFailPage(code, resultData.msg, image); pushToFailPage(code, resultData.msg, image);
@ -150,11 +148,13 @@ class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
return; return;
} }
pushToFailPage(String code, String message, File image){ pushToFailPage(String code, String message, File image) {
if (image != null) { if (image != null) {
AppRouter.pushAndReplaced(context, RouteName.BARCODE_PHOTOSFAIL, arguments: PhotosFailBarcodePage.setArguments(code, message, image)); AppRouter.pushAndReplaced(context, RouteName.BARCODE_PHOTOSFAIL,
}else{ arguments: PhotosFailBarcodePage.setArguments(code, message, image));
AppRouter.pushAndReplaced(context, RouteName.BARCODE_FAIL, arguments: FailBarcodePage.setArguments(code, message)); } else {
AppRouter.pushAndReplaced(context, RouteName.BARCODE_FAIL,
arguments: FailBarcodePage.setArguments(code, message));
} }
} }
@ -162,5 +162,4 @@ class _BarcodeScanPageState extends BaseStoreState<BarcodeScanPage> {
void dispose() { void dispose() {
super.dispose(); super.dispose();
} }
} }

File diff suppressed because it is too large Load Diff

@ -37,6 +37,7 @@ dependencies:
# soundpool: 1.0.1 # soundpool: 1.0.1
## 暂时关闭声音播放 ## 暂时关闭声音播放
#flutter_audio_player: 0.1.2 #flutter_audio_player: 0.1.2
just_audio: ^0.7.3
# 第三方分享功能 # 第三方分享功能
sharesdk_plugin: sharesdk_plugin:
git: git:

Loading…
Cancel
Save