Flutter plugin for scanning and generating QR codes using the ZXing library, supporting Android, iOS, and desktop platforms
flutterbarcode-generatorbarcode-scannergeneratorqrqrcodeqrcode-generatorqrcode-scannerscannerzxingbarcodezxscanner
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
115 lines
3.0 KiB
115 lines
3.0 KiB
3 years ago
|
import 'dart:io';
|
||
|
import 'dart:typed_data';
|
||
|
|
||
|
import 'package:flutter/material.dart';
|
||
|
import 'package:flutter_zxing/flutter_zxing.dart';
|
||
3 years ago
|
import 'package:zxscanner/configs/constants.dart';
|
||
|
import 'package:zxscanner/models/encode.dart';
|
||
|
import 'package:zxscanner/utils/db_service.dart';
|
||
|
import 'package:zxscanner/utils/extensions.dart';
|
||
|
import 'package:zxscanner/widgets/common_widgets.dart';
|
||
3 years ago
|
import 'package:path_provider/path_provider.dart';
|
||
|
import 'package:share_plus/share_plus.dart';
|
||
|
|
||
|
late Directory tempDir;
|
||
|
String get tempPath => '${tempDir.path}/zxing.jpg';
|
||
|
|
||
3 years ago
|
class CreatorPage extends StatefulWidget {
|
||
|
const CreatorPage({
|
||
3 years ago
|
Key? key,
|
||
|
}) : super(key: key);
|
||
|
|
||
|
@override
|
||
3 years ago
|
State<CreatorPage> createState() => _CreatorPageState();
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
class _CreatorPageState extends State<CreatorPage> {
|
||
3 years ago
|
bool isAndroid() => Theme.of(context).platform == TargetPlatform.android;
|
||
|
|
||
|
// Write result
|
||
3 years ago
|
Encode? encode;
|
||
3 years ago
|
|
||
|
@override
|
||
|
void initState() {
|
||
|
super.initState();
|
||
|
|
||
|
initStateAsync();
|
||
|
}
|
||
|
|
||
|
void initStateAsync() async {
|
||
|
getTemporaryDirectory().then((value) {
|
||
|
tempDir = value;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
@override
|
||
|
Widget build(BuildContext context) {
|
||
|
return Scaffold(
|
||
|
appBar: AppBar(
|
||
3 years ago
|
title: const Text('Creator'),
|
||
3 years ago
|
),
|
||
3 years ago
|
body: SingleChildScrollView(
|
||
3 years ago
|
child: ContainerX(
|
||
|
child: Column(
|
||
|
children: [
|
||
3 years ago
|
WriterWidget(
|
||
3 years ago
|
onSuccess: (result, bytes) {
|
||
|
setState(() {
|
||
|
encode = Encode.fromEncodeResult(result, bytes);
|
||
|
});
|
||
|
},
|
||
|
onError: (error) {
|
||
|
setState(() {
|
||
|
encode = null;
|
||
|
});
|
||
3 years ago
|
context.showToast(error);
|
||
3 years ago
|
},
|
||
|
),
|
||
|
if (encode != null) buildWriteResult(),
|
||
|
],
|
||
|
),
|
||
3 years ago
|
),
|
||
3 years ago
|
),
|
||
|
);
|
||
|
}
|
||
|
|
||
|
Column buildWriteResult() {
|
||
|
return Column(
|
||
|
children: [
|
||
|
// Barcode image
|
||
3 years ago
|
Image.memory(encode?.data ?? Uint8List(0)),
|
||
3 years ago
|
const SizedBox(height: spaceLarge),
|
||
3 years ago
|
// Share button
|
||
3 years ago
|
Row(
|
||
|
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
|
||
|
crossAxisAlignment: CrossAxisAlignment.center,
|
||
|
children: [
|
||
|
ElevatedButton(
|
||
|
onPressed: () {
|
||
|
// Save image to device
|
||
|
final file = File(tempPath);
|
||
|
file.writeAsBytesSync(encode?.data ?? Uint8List(0));
|
||
|
final path = file.path;
|
||
|
// Share image
|
||
|
Share.shareFiles([path]);
|
||
|
},
|
||
|
child: const Text('Share'),
|
||
|
),
|
||
|
ElevatedButton(
|
||
|
onPressed: () async {
|
||
|
if (encode != null) {
|
||
|
await DbService.instance.addEncode(encode!);
|
||
3 years ago
|
if (!mounted) return;
|
||
3 years ago
|
Navigator.of(context).pop();
|
||
|
}
|
||
|
},
|
||
|
child: const Text('Save'),
|
||
|
),
|
||
|
],
|
||
3 years ago
|
),
|
||
3 years ago
|
const SizedBox(height: spaceLarge),
|
||
3 years ago
|
],
|
||
|
);
|
||
|
}
|
||
|
}
|