scanner_view 1.0.1 scanner_view: ^1.0.1 copied to clipboard
Flutter plugin for QR scanner.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scanner_view/scanner_view.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState(BuildContext context,
{bool isSingle = true}) async {
// Platform messages may fail, so we use a try/catch PlatformException.
try {
ScannerView.getScanResult(context, isSingle: isSingle,
callback: (scanResult) async {
await Future.delayed(Duration(milliseconds: 2000));
});
} on PlatformException {}
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
// if (!mounted) return;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("扫码"),
),
body: Center(
child: Column(
children: [
Builder(
builder: (BuildContext ctx) {
return TextButton(
child: Text("单次扫码"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blueAccent),
foregroundColor:MaterialStateProperty.all(Colors.white),
),
onPressed: () {
ScannerView.getScanResult(ctx,
callback: (scanResult) async {
print("扫描结果:$scanResult");
});
});
},
),
Builder(
builder: (BuildContext ctx) {
return TextButton(
child: Text("连续扫码"),
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blueAccent),
foregroundColor:MaterialStateProperty.all(Colors.white),
),
onPressed: () {
ScannerView.getScanResult(ctx, isSingle: false,
callback: (scanResult) async {
await Future.delayed(Duration(milliseconds: 2000));
});
});
},
),
],
))),
);
}
}