在项目进行中,遇到需要根据页面动态生成二维码的情况。
在pub.dev/查找了下,可以使用组件 qr_flutter 来实现
一、在yaml中引入包,目前最新版本是3.2.0
qr_flutter: ^3.2.0二、在使用的动态生成二维码文件中引入包
import 'package:qr_flutter/qr_flutter.dart';三、使用
1、生成二维码:
String imgUrl = 'https://c-ssl.duitang.com/uploads/item/202005/06/20200506170346_rkjrm.thumb.300_0.jpg';QrImage( data: '$imgUrl', version: QrVersions.auto, size: 320, gapless: false, embeddedImageStyle: QrEmbeddedImageStyle( size: Size(80, 80), ),)2、生成带图片的二维码:
String imgUrl = 'https://c-ssl.duitang.com/uploads/item/202005/06/20200506170346_rkjrm.thumb.300_0.jpg';
QrImage( data: '$imgUrl', size: 320, gapless: false, version: QrVersions.auto, embeddedImage: AssetImage("icon.png"), embeddedImageStyle: QrEmbeddedImageStyle( size: Size(15, 15), ),),3、参数介绍
| Property | Type | Description |
|---|---|---|
version | int | QrVersions.auto or a value between 1 and 40. See www.qrcode.com/en/about/ve… for limitations and details.自动或1到40之间的值。 二维码的符号版本从版本1到版本40不等。 每个版本都有不同的模块配置或模块数。 (该模块指的是组成 QR 码的黑白点。) “模块配置”是指包含在一个符号中的模块数量,从版本1(2121个模块)到版本40(177177个模块)。 每个更高的版本号包含4个额外的模块。 根据数据量、字符类型和错误纠正等级,每个 QR 码符号版本都有最大的数据容量。 换句话说,随着数据量的增加,需要更多的模块组成 QR 码,从而产生更大的 QR 码符号。 |
errorCorrectionLevel | int | A value defined on QrErrorCorrectLevel. e.g.: QrErrorCorrectLevel.L. |
size | double | The (square) size of the image. If not given, will auto size using shortest size constraint. 生成二维码图片的尺寸 |
padding | EdgeInsets | Padding surrounding the QR code data. |
backgroundColor | Color | The background color (default is none). |
foregroundColor | Color | The foreground color (default is black). |
gapless | bool | Adds an extra pixel in size to prevent gaps (default is true). 添加一个额外的像素大小,以防止空白,默认是true |
errorStateBuilder | QrErrorBuilder | Allows you to show an error state Widget in the event there is an error rendering the QR code (e.g.: version is too low, input is too long, etc).允许您显示一个错误状态小部件在事件中有一个错误的二维码呈现(例如:版本太低,输入太长,等等)。 |
constrainErrorBounds | bool | If true, the error Widget will be constrained to the square that the QR code was going to be drawn in. If false, the error state Widget will grow/shrink to whatever size it needs.如果为真,则错误小部件将被限制在将要绘制二维码的正方形内。如果为false, error state小部件将增长/收缩到所需的大小。 |
embeddedImage | ImageProvider | An ImageProvider that defines an image to be overlaid in the center of the QR code.一个ImageProvider,它定义了要覆盖在二维码中心的图像。 |
embeddedImageStyle | QrEmbeddedImageStyle | Properties to style the embedded image. 属性设置嵌入图像的样式。 |
embeddedImageEmitsError | bool | If true, any failure to load the embedded image will trigger the errorStateBuilder or render an empty Container. If false, the QR code will be rendered and the embedded image will be ignored.如果为真,加载嵌入式映像的任何失败都将触发errorStateBuilder或呈现空容器。如果为false,则会呈现二维码,而嵌入的图像将被忽略。 |