Advertisement
Guest User

bar Code preview

a guest
Jan 27th, 2022
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.90 KB | None | 0 0
  1. class CodePreviewPage extends StatefulWidget {
  2.   final Product product;
  3.   const CodePreviewPage({
  4.     Key? key,
  5.     required this.product,
  6.   }) : super(key: key);
  7.  
  8.   @override
  9.   _CodePreviewPageState createState() => _CodePreviewPageState();
  10. }
  11.  
  12. class _CodePreviewPageState extends State<CodePreviewPage> {
  13.   GlobalKey barCodeKey = new GlobalKey();
  14.   GlobalKey qrCodeKey = new GlobalKey();
  15.   @override
  16.   Widget build(BuildContext context) {
  17.     return Scaffold(
  18.       appBar: AppBar(
  19.         elevation: 1,
  20.         centerTitle: true,
  21.         backgroundColor: Colors.white,
  22.         leading: IconButton(
  23.           icon: Icon(
  24.             Icons.arrow_back_ios,
  25.             color: Colors.black,
  26.           ),
  27.           onPressed: () {
  28.             Navigator.of(context).pop();
  29.           },
  30.         ),
  31.         title: Text(
  32.           "Bar Code Preview",
  33.           style: TextStyle(
  34.             color: Colors.black,
  35.           ),
  36.         ),
  37.       ),
  38.       body: SingleChildScrollView(
  39.         child: _buildItems(),
  40.       ),
  41.       bottomNavigationBar: _buildBottomNavigationBar(),
  42.     );
  43.   }
  44.  
  45.   Widget _buildItems() {
  46.     return Container(
  47.       padding: EdgeInsets.all(20),
  48.       child: Column(
  49.         children: <Widget>[
  50.           _buildBarCodeSection(),
  51.           _buildQrCodeSection(),
  52.         ],
  53.       ),
  54.     );
  55.   }
  56.  
  57.   Widget _buildBarCodeSection() {
  58.     return Column(
  59.       crossAxisAlignment: CrossAxisAlignment.start,
  60.       children: [
  61.         Row(
  62.           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  63.           children: <Widget>[
  64.             Text(
  65.               "Bar Code",
  66.               style: TextStyle(
  67.                 fontSize: 20,
  68.                 fontWeight: FontWeight.bold,
  69.               ),
  70.             ),
  71.             TextButton(
  72.               onPressed: () {},
  73.               child: Text("Print"),
  74.             ),
  75.             TextButton(
  76.               onPressed: () {
  77.                 _saveBarCode();
  78.               },
  79.               child: Text("Save"),
  80.             ),
  81.           ],
  82.         ),
  83.         SizedBox(
  84.           height: 20,
  85.         ),
  86.         Center(
  87.           child: RepaintBoundary(
  88.             key: barCodeKey,
  89.             child: BarcodeWidget(
  90.               data: widget.product.productId!,
  91.               barcode: Barcode.code128(),
  92.               width: 200,
  93.               height: 200,
  94.               drawText: false,
  95.             ),
  96.           ),
  97.         ),
  98.       ],
  99.     );
  100.   }
  101.  
  102.   Widget _buildQrCodeSection() {
  103.     return Column(
  104.       crossAxisAlignment: CrossAxisAlignment.start,
  105.       children: [
  106.         Row(
  107.           mainAxisAlignment: MainAxisAlignment.spaceBetween,
  108.           children: <Widget>[
  109.             Text(
  110.               "QR Code",
  111.               style: TextStyle(
  112.                 fontSize: 20,
  113.                 fontWeight: FontWeight.bold,
  114.               ),
  115.             ),
  116.             TextButton(
  117.               onPressed: () {},
  118.               child: Text("Print"),
  119.             ),
  120.           ],
  121.         ),
  122.         SizedBox(
  123.           height: 20,
  124.         ),
  125.         BarcodeWidget(
  126.           data: widget.product.productId!,
  127.           barcode: Barcode.qrCode(),
  128.         ),
  129.       ],
  130.     );
  131.   }
  132.  
  133.   Widget _buildBottomNavigationBar() {
  134.     return Container(
  135.       padding: EdgeInsets.all(20),
  136.       child: BlackButton(
  137.         label: "Save",
  138.         onPressed: () {},
  139.       ),
  140.     );
  141.   }
  142.  
  143.   Future<File> _saveBarCode() async {
  144.     RenderRepaintBoundary boundary =
  145.         barCodeKey.currentContext!.findRenderObject() as RenderRepaintBoundary;
  146.     ui.Image image = await boundary.toImage();
  147.     ByteData? byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  148.     Uint8List pngBytes = byteData!.buffer.asUint8List();
  149.     final tempPath = (await getTemporaryDirectory()).path;
  150.     final path = tempPath + "qr.png";
  151.     File imgFile = File(path);
  152.     return imgFile.writeAsBytes(pngBytes);
  153.   }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement