flutter_barcode_listener 0.0.3 flutter_barcode_listener: ^0.0.3 copied to clipboard
Listen for any hardware barcode scanner without any manufacturer SDK
import 'package:flutter/material.dart';
import 'package:visibility_detector/visibility_detector.dart';
import 'package:flutter_barcode_listener/flutter_barcode_listener.dart';
import 'second_screen.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Barcode Scanner Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Barcode Scanner Demo'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _barcode;
bool visible;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
// Add visiblity detector to handle barcode
// values only when widget is visible
child: VisibilityDetector(
onVisibilityChanged: (VisibilityInfo info) {
visible = info.visibleFraction > 0;
},
key: Key('visible-detector-key'),
child: BarcodeKeyboardListener(
onBarcodeScanned: (barcode) {
if (!visible) return;
print(barcode);
setState(() {
_barcode = barcode;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Text(
_barcode == null ? 'SCAN BARCODE' : 'BARCODE: $_barcode',
style: Theme.of(context).textTheme.headline5,
),
Padding(
padding: const EdgeInsets.all(20.0),
child: ElevatedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
SecondScreen())),
child: Center(child: Text('Second screen'))),
)
],
),
),
),
),
);
}
}