Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CodePreviewPage extends StatefulWidget {
- final Product product;
- const CodePreviewPage({
- Key? key,
- required this.product,
- }) : super(key: key);
- @override
- _CodePreviewPageState createState() => _CodePreviewPageState();
- }
- class _CodePreviewPageState extends State<CodePreviewPage> {
- GlobalKey barCodeKey = new GlobalKey();
- GlobalKey qrCodeKey = new GlobalKey();
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- elevation: 1,
- centerTitle: true,
- backgroundColor: Colors.white,
- leading: IconButton(
- icon: Icon(
- Icons.arrow_back_ios,
- color: Colors.black,
- ),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- title: Text(
- "Bar Code Preview",
- style: TextStyle(
- color: Colors.black,
- ),
- ),
- ),
- body: SingleChildScrollView(
- child: _buildItems(),
- ),
- bottomNavigationBar: _buildBottomNavigationBar(),
- );
- }
- Widget _buildItems() {
- return Container(
- padding: EdgeInsets.all(20),
- child: Column(
- children: <Widget>[
- _buildBarCodeSection(),
- _buildQrCodeSection(),
- ],
- ),
- );
- }
- Widget _buildBarCodeSection() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Text(
- "Bar Code",
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.bold,
- ),
- ),
- TextButton(
- onPressed: () {},
- child: Text("Print"),
- ),
- TextButton(
- onPressed: () {
- _saveBarCode();
- },
- child: Text("Save"),
- ),
- ],
- ),
- SizedBox(
- height: 20,
- ),
- Center(
- child: RepaintBoundary(
- key: barCodeKey,
- child: BarcodeWidget(
- data: widget.product.productId!,
- barcode: Barcode.code128(),
- width: 200,
- height: 200,
- drawText: false,
- ),
- ),
- ),
- ],
- );
- }
- Widget _buildQrCodeSection() {
- return Column(
- crossAxisAlignment: CrossAxisAlignment.start,
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- children: <Widget>[
- Text(
- "QR Code",
- style: TextStyle(
- fontSize: 20,
- fontWeight: FontWeight.bold,
- ),
- ),
- TextButton(
- onPressed: () {},
- child: Text("Print"),
- ),
- ],
- ),
- SizedBox(
- height: 20,
- ),
- BarcodeWidget(
- data: widget.product.productId!,
- barcode: Barcode.qrCode(),
- ),
- ],
- );
- }
- Widget _buildBottomNavigationBar() {
- return Container(
- padding: EdgeInsets.all(20),
- child: BlackButton(
- label: "Save",
- onPressed: () {},
- ),
- );
- }
- Future<File> _saveBarCode() async {
- RenderRepaintBoundary boundary =
- barCodeKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
- ui.Image image = await boundary.toImage();
- ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
- Uint8List pngBytes = byteData!.buffer.asUint8List();
- final tempPath = (await getTemporaryDirectory()).path;
- final path = tempPath + "qr.png";
- File imgFile = File(path);
- return imgFile.writeAsBytes(pngBytes);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement