Skip to content

How to run this plugin in Flutter

Tal Jacobson edited this page Dec 9, 2018 · 2 revisions

With @Günter Zöchbauer help, I finally made it. My small contribution to Stack Overflow members, Let's Flutter Members and the this plugin members.

TODO: 1 - LOAD EVERYTHING

Future _loadEverything() async {
    await _requestAppDocumentsDirectory();   // TODO: 2 - GET APP DOCUMENTS DIRECTORY
    _dekontExist = await makeReceiptImage(); // TODO: 3 - MAKE A RECEIPT

    // Show the writen image
    if (_dekontExist == true) {
      setState(() {
        newDekontImage = _appDocumentsDirectory + "/" + widget._currentUserReceiptNo + ".jpg";
        imageOkay = true; // FOR - 4 - MAIN WIDGET BUILD
      });
    }
  }

TODO: 2 - GET APP DOCUMENTS DIRECTORY

Future _requestAppDocumentsDirectory() async {
  // I choose temp dir because I don’t want to write twice same Receipt
  // Also when user close the app it will destroys the images
  final _directory =
      await getTemporaryDirectory(); //getApplicationDocumentsDirectory();
  setState(() {
    _appDocumentsDirectory = _directory.path;
  });
}

TODO: 3 - MAKE A RECEIPT

Future<bool> makeReceiptImage() async {

  // I use this as a template:
  // 'packages/mayApp/assets/images/CapitalReceipt.jpg'

  ByteData imageData = await rootBundle.load('packages/mayApp/assets/images/CapitalReceipt.jpg');
  List<int> bytes = Uint8List.view(imageData.buffer);
  Image _receiptImage = decodeImage(bytes);

  // TODO: WRITE ON TO THE IMAGE
  drawString(_receiptImage, arial_48, 440, 30, “Customer Receipt - Customer Name”, color: 0xFF000000);


  // Write it to disk as a different jpeg
    var new_jpeg = await encodeJpg(_receiptImage);
    String newDekontImage = _appDocumentsDirectory + "/" + "${_currentUserReceiptNo}" + ".jpg";
    await File(newDekontImage).writeAsBytesSync(new_jpeg);

  return true;
}

TODO: 4 - MAIN WIDGET BUILD

  @override
  Widget build(BuildContext context) {
    capitalHeight = MediaQuery.of(context).size.height;
    capitalWidth = MediaQuery.of(context).size.width;

    return Scaffold(
      resizeToAvoidBottomPadding: true,
      appBar: AppBar(
        title: Text("Customer Receipt"),
        backgroundColor: const Color(0xFF7CB342),
        elevation: 0.0,
        actions: <Widget>[
          Container(
            padding: EdgeInsets.only(right: 10.0),
            child: Icon(Icons.mail),
          )
        ],
      ),
      body: Center(
        child: Container(
          margin: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
          width: capitalWidth,
          height: capitalHeight / 2.3,
          color: Colors.black54, 
          child: imageOkay == true
              ? Container(
                  width: capitalWidth - 20.0,
                  height: capitalHeight / 2.3,
                  child: Image.file(File('$newDekontImage')),
                )
              : CircularProgressIndicator(),
        ),
      ),
    );
  }