在我的Flutter应用程序中,我想扫描一些条形码。扫描条形码后,我想显示一个小吃吧。该应用应该在Android设备上运行。
不幸的是,小吃店从未露面。 两种功能(条形码扫描和显示小吃店)都可以单独使用。
在调试过程中,会发生以下情况:
条形码扫描由版本 0.0.3 中的flutter插件 barcode_scan
处理。
以下是匹配代码:
final GlobalKey<ScaffoldState> keyScaffold = new GlobalKey<ScaffoldState>();
[...]
@override
Widget build(BuildContext context) {
return new Scaffold(
key: keyScaffold,
[...]
}
Future scan() async {
String snackBarMessage = "No error";
try {
String barcode = await BarcodeScanner.scan();
setState(() => _barcodeController.text = barcode);
snackBarMessage = barcode;
} on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
snackBarMessage = 'The user did not grant the camera permission!';
} else {
snackBarMessage = 'Unknown error: $e';
}
} on FormatException {
snackBarMessage =
'null (User returned using the "back"-button before scanning anything. Result)';
} catch (e) {
snackBarMessage = 'Unknown error: $e';
}
keyScaffold.currentState.showSnackBar(new SnackBar(
content: new Text(snackBarMessage),
duration: new Duration(seconds: 5),
));
}
答案 0 :(得分:0)
在我的原型flutter应用程序(flutter版本0.5.1 beta)上,我同时使用了快餐栏和barcode_scan插件(版本0.0.4),并且快餐栏确实显示了(扫描结果为字符串) )。所有积分都归于Wahib Ul Haq以便更轻松地编辑快餐栏:
就我而言,有两点值得注意。
我根本不使用keyScaffold。我将BuildContext声明为一个变量(我称其为scaffoldContext)以容纳该支架的主体。
我声明并初始化一个新的脚手架。对于它的主体,我编写了一个Builder。在构建器内部,我将scaffoldContext初始化为构建器的上下文。
这是我的代码,可让您更容易理解:
import 'package:flutter/material.dart';
import 'package:barcode_scan/barcode_scan.dart';
import 'package:flutter/services.dart';
[...]
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
[...],
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
//This is what I meant in note #1.
BuildContext scaffoldContext;
String barcode;
@override
initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('MyApp'),
),
//This is what I meant in note #2.
body: Builder(builder: (BuildContext context) {
scaffoldContext = context;
return Center(
child: Column(children: <Widget>[
ListTile(
[...]
),
ListTile(
[...]
),
[...]
]
));}),
floatingActionButton: FloatingActionButton(
mini: true,
child: Icon(Icons.crop_free), onPressed: fabPressed),
)};
void createSnackBar(String message) {
final snackBar = new SnackBar(content: new Text(message),
backgroundColor: Colors.black
);
Scaffold.of(scaffoldContext).showSnackBar(snackBar);
}
void fabPressed() async {
barcode = "";
try {
barcode = await BarcodeScanner.scan();
setState(() => this.barcode = barcode);
createSnackBar(barcode);
}
on PlatformException catch (e) {
if (e.code == BarcodeScanner.CameraAccessDenied) {
setState(() {
this.barcode = 'The user did not grant the camera permission!';
});
} else {
setState(() => this.barcode = 'Unknown error: $e');
}
}
on FormatException {
setState(() =>
this.barcode = 'null (User returned using the "back"-button before scanning anything. Result)');
}
catch (e) {
setState(() => this.barcode = 'Unknown error: $e');
}
}
}