flutter_barcode_listener 0.1.4 flutter_barcode_listener: ^0.1.4 copied to clipboard
Listen for any hardware barcode scanner without any manufacturer SDK
import 'dart:io';
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, required this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String? _barcode;
late 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(
bufferDuration: Duration(milliseconds: 200),
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.headlineSmall,
),
Padding(
padding: const EdgeInsets.all(20.0),
child: ElevatedButton(
onPressed: () => Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) =>
SecondScreen())),
child: Center(child: Text('Second screen'))),
)
],
),
useKeyDownEvent: Platform.isWindows,
),
),
),
);
}
}