class ScannerPage extends StatefulWidget {
final Function(bool) onBeepSettingChanged;
final bool beepChecked;
const ScannerPage({Key? key, required this.onBeepSettingChanged, required this.beepChecked,}) : super(key: key);
@override
State<ScannerPage> createState() => _ScannerPageState();
}
class _ScannerPageState extends State<ScannerPage> {
List<String> selectedImages = [];
String scanResult = "";
// bool scannerVibrateEnabled = false;
Future<void> _vibrate() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool scannervibrationEnabled = prefs.getBool('vibrationEnabled') ?? true;
if (scannervibrationEnabled ) {
Vibration.vibrate(duration: 500);
}
}
Future<void> scanQR() async {
try {
final scanResult = await FlutterBarcodeScanner.scanBarcode(
'#ff6666',
'Cancel',
true,
ScanMode.BARCODE,
);
if (!mounted) return;
setState(() {
this.scanResult = scanResult;
});
log('de_alam scanner $scanResult');
if(scanResult=="-1"){
return;
}
if (scanResult.isNotEmpty) {
Navigator.push(this.context ,
MaterialPageRoute(
builder: (context) => ResultPage(scanResult: scanResult ),
),
);
// widget.onBeepSettingChanged(widget.beepChecked);
}
} on PlatformException {
setState(() {
this.scanResult = "Failed to get platform version";
});
}
}
我有一个应用程序,其中有一个 setting_page 。我想在其中添加振动,我希望当我单击振动按钮时它会变为 true,然后当我打开 scanpage 时会产生振动声音扫描。当我在setting_page中检查vibration_button为真时,它会在scanner_page中扫描时产生振动。我在setting_page中使用CheckListTile。请帮助我做到这一点。
据我所知和理解,这就是您一直在寻找的东西。这是一款具有振动功能的二维码扫描仪。在下面编写的代码中,只有当
vibrationEnabled
值为 true
并且扫描二维码 successfully
并且具有 valid
值时,振动器才会振动。
import "dart:async";
import "dart:developer";
import "package:flutter/material.dart";
import "package:flutter_barcode_scanner/flutter_barcode_scanner.dart";
import "package:shared_preferences/shared_preferences.dart";
import "package:vibration/vibration.dart";
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(title: "My App", home: HomePage());
}
}
class HomePage extends StatefulWidget {
const HomePage({super.key});
@override
State<HomePage> createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
unawaited(scanQR());
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(onPressed: scanQR, child: const Text("scanQR")),
),
),
);
}
Future<void> scanQR() async {
final String result = await FlutterBarcodeScanner.scanBarcode(
"#ff6666",
"Cancel",
true,
ScanMode.BARCODE,
);
log("scanResult: $result");
if (result.isEmpty) {
// Write something if result.isEmpty...
} else if (result == "-1") {
// Write something if result == "-1"...
} else if (result.isNotEmpty) {
await vibrate();
if (mounted) {
// Write something if result.isNotEmpty. Your navigation code goes here.
} else {
return;
}
} else {
// Write something if result is unknown / undetermined...
}
return Future<void>.value();
}
Future<void> vibrate() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
(prefs.getBool("vibrationEnabled") ?? true)
? await Vibration.vibrate()
: log("Vibration is disabled");
return Future<void>.value();
}
}