gpt4 book ai didi

Flutter:生成具有多个键值对的二维码

转载 作者:行者123 更新时间:2023-12-02 19:05:49 24 4
gpt4 key购买 nike

我正在尝试在flutter中生成qrcode。我需要在其中添加三个键值。所以,当我扫描它时,我可以获得相关信息。

这是我第一次从事二维码扫描工作。所以我对此一无所知。

这是代码。

import 'package:DTG/utils/d_app_drwaer.dart';
import 'package:DTG/utils/d_appbar.dart';
import 'package:auto_route/auto_route.dart';
import 'package:device_info/device_info.dart';
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'dart:io';
import 'package:flutter/rendering.dart';
import 'package:path_provider/path_provider.dart';

class BookingQRGenerate extends StatefulWidget {
final int abid;
final int uid;
final int txnid;

const BookingQRGenerate({
@required this.abid,
@required this.uid,
@required this.txnid,
});
@override
State<StatefulWidget> createState() => BookingQRGenerateState();
}

class BookingQRGenerateState extends State<BookingQRGenerate> {
static const double _topSectionTopPadding = 20.0;
static const double _topSectionBottomPadding = 20.0;
static const double _topSectionHeight = 20.0;

GlobalKey globalKey = new GlobalKey();
String _dataString = "Hello from this QR";
bool physicaldevice;
var deviceid;

int dtguid;

int abid;
int txnid;
int uid;

@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
deviceInfo();
}

Future<void> deviceInfo() async {
abid = widget.abid;
txnid = widget.txnid;
uid = widget.uid;

DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;

}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: myAppBar(),
endDrawer: myDrawer(),
body: _contentWidget(),
);
}

_contentWidget() {
final bodyHeight = MediaQuery.of(context).size.height -
MediaQuery.of(context).viewInsets.bottom;
return Container(
color: const Color(0xFFFFFFFF),
child: Column(
children: <Widget>[
Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(13.0)),
color: Colors.blueGrey[300],
child: ListTile(
title: Text(
'QRCode',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
),
Padding(
padding: const EdgeInsets.only(
top: _topSectionTopPadding,
left: 20.0,
right: 10.0,
bottom: _topSectionBottomPadding,
),
child: Container(
height: _topSectionHeight,
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[],
),
),
),
Expanded(
child: Container(
color: Colors.black26,
child: Center(
child: RepaintBoundary(
key: globalKey,
child: QrImage(
data: _dataString,
// data:abid, uid, txnid,
version: QrVersions.auto,
size: 320,
gapless: false,
embeddedImage: AssetImage('assets/img/logo.png'),
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size(80, 80),
),
),
),
),
),
),
],
),
);
}
}

我需要在二维码中传递abid:value, txnid:value, uid: value。因此,当我扫描它时,我可以获取该值并相应地使用它。

最佳答案

您可以复制粘贴下面的 run 2 完整代码
您可以使用JSON
第 1 步:使用 JSON 生成 QR 图像格式
您可以在下面看到第 1 步生成二维码图像的完整代码

home: BookingQRGenerate(
abid: 1,
uid: 2,
txnid: 3,
),
...
Future<void> deviceInfo() async {
abid = widget.abid;
txnid = widget.txnid;
uid = widget.uid;
_dataString = '''
{
"abid": ${widget.abid},
"uid":${widget.uid},
"txnid":${widget.txnid}
}
''';

第 2 步:检查 QR 图像生成正确的 JSON 字符串

enter image description here

第3步:扫描此二维码图像,您将得到JSON字符串,您可以使用 payloadFromJson 进行解析,查看完整的解析代码 JSON下面

Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));
...
Payload payload = payloadFromJson(jsonString);
print("abid ${payload.abid}");
print("uid ${payload.uid}");
print("txnid ${payload.txnid}");

第 1 步的工作演示

enter image description here

第1步生成QR图像的完整代码

import 'package:flutter/material.dart';
import 'package:device_info/device_info.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flutter/services.dart';
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'dart:io';
import 'package:flutter/rendering.dart';
//import 'package:path_provider/path_provider.dart';

class BookingQRGenerate extends StatefulWidget {
final int abid;
final int uid;
final int txnid;

const BookingQRGenerate({
@required this.abid,
@required this.uid,
@required this.txnid,
});
@override
State<StatefulWidget> createState() => BookingQRGenerateState();
}

class BookingQRGenerateState extends State<BookingQRGenerate> {
static const double _topSectionTopPadding = 20.0;
static const double _topSectionBottomPadding = 20.0;
static const double _topSectionHeight = 20.0;

GlobalKey globalKey = new GlobalKey();
String _dataString = "Hello from this QR";
bool physicaldevice;
var deviceid;

int dtguid;

int abid;
int txnid;
int uid;

@override
void initState() {
super.initState();
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
deviceInfo();
}

Future<void> deviceInfo() async {
abid = widget.abid;
txnid = widget.txnid;
uid = widget.uid;
_dataString = '''
{
"abid": ${widget.abid},
"uid":${widget.uid},
"txnid":${widget.txnid}
}
''';

print("_dataString $_dataString");
DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
}

@override
Widget build(BuildContext context) {
return Scaffold(
//appBar: myAppBar(),
//endDrawer: myDrawer(),
body: _contentWidget(),
);
}

_contentWidget() {
final bodyHeight = MediaQuery.of(context).size.height -
MediaQuery.of(context).viewInsets.bottom;
return Container(
color: const Color(0xFFFFFFFF),
child: Column(
children: <Widget>[
Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(13.0)),
color: Colors.blueGrey[300],
child: ListTile(
title: Text(
'QRCode',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.w500,
),
),
),
),
Padding(
padding: const EdgeInsets.only(
top: _topSectionTopPadding,
left: 20.0,
right: 10.0,
bottom: _topSectionBottomPadding,
),
child: Container(
height: _topSectionHeight,
child: Row(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[],
),
),
),
Expanded(
child: Container(
color: Colors.black26,
child: Center(
child: RepaintBoundary(
key: globalKey,
child: QrImage(
data: _dataString,
// data:abid, uid, txnid,
version: QrVersions.auto,
size: 320,
gapless: false,
embeddedImage: AssetImage('assets/img/logo.png'),
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size(80, 80),
),
),
),
),
),
),
],
),
);
}
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: BookingQRGenerate(
abid: 1,
uid: 2,
txnid: 3,
),
);
}
}

要解析的完整代码 JSON

import 'package:flutter/material.dart';
import 'dart:convert';

Payload payloadFromJson(String str) => Payload.fromJson(json.decode(str));

String payloadToJson(Payload data) => json.encode(data.toJson());

class Payload {
Payload({
this.abid,
this.txnid,
this.uid,
});

int abid;
int txnid;
int uid;

factory Payload.fromJson(Map<String, dynamic> json) => Payload(
abid: json["abid"],
txnid: json["txnid"],
uid: json["uid"],
);

Map<String, dynamic> toJson() => {
"abid": abid,
"txnid": txnid,
"uid": uid,
};
}

void main() {
runApp(MyApp());
}

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}

class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
_MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;

void _incrementCounter() {
String jsonString = '''
{
"abid": 1,
"uid":2,
"txnid":3
}
''';

Payload payload = payloadFromJson(jsonString);
print("abid ${payload.abid}");
print("uid ${payload.uid}");
print("txnid ${payload.txnid}");
setState(() {
_counter++;
});
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}

解析输出JSON

I/flutter ( 6945): abid 1
I/flutter ( 6945): uid 2
I/flutter ( 6945): txnid 3

关于Flutter:生成具有多个键值对的二维码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65069392/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com