AmolGangadhare / flutter_barcode_scanner

Barcode scanner plugin for flutter. Supports barcode scanning for Android and iOS
https://pub.dev/packages/flutter_barcode_scanner
MIT License
380 stars 499 forks source link

Override Cancel Button Event #8

Closed caktoy closed 5 years ago

caktoy commented 5 years ago

How to override 'Cancel' button action? Any suggestions?

AmolGangadhare commented 5 years ago

Cancel button listener is not available now. Is there any special requirement for a listener?

caktoy commented 5 years ago

Would you mind to add it later? Actually I didn't think that is have any special requirement for that.

AmolGangadhare commented 5 years ago

Ok. I'll figure out scenarios that would be helpful to get the listener, will update you on this later.

caktoy commented 5 years ago

Ok, thank you, I'll wait it

warriorCoder commented 5 years ago

I'd like to be able to specify the cancel event. The reason is that I want the scanner to load and start scanning when the page loads, as such I'd like for the user to be able to cancel the scanning and take them back to either a blank page where they can start the scanner again or pop them back to the page they came from.

AmolGangadhare commented 5 years ago

The cancel button is already there in barcode scanning screen, so if a user needs to stop the scanning, user can do it by clicking on cancel button.

warriorCoder commented 5 years ago

Is there also a way to programmatically cancel scanning? Sorry I haven't had a chance to look at the code.

caktoy commented 5 years ago

This is a simple code I used:

@override
void initState() {
  super.initState();

  initPlatformState();
}

// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
  String barcodeScanRes;
  // Platform messages may fail, so we use a try/catch PlatformException.
  try {
    barcodeScanRes = await FlutterBarcodeScanner.scanBarcode("#9dff00", "Cancel", true);
  } on PlatformException {
    barcodeScanRes = 'Failed to get platform version.';
  }

  // If the widget was removed from the tree while the asynchronous platform
  // message was in flight, we want to discard the reply rather than calling
  // setState to update our non-existent appearance.
  if (!mounted) return;

  Navigator.pop(_scaffoldKey.currentContext);

  if (barcodeScanRes.isNotEmpty)
    Navigator.of(_scaffoldKey.currentContext).push(MaterialPageRoute(builder: (BuildContext context) => new DetailScreen()));
}

With that code, I just close current screen that contain scanner instance first, if there is any result after scanning process, then push to another screen I want.

AmolGangadhare commented 5 years ago

Is that solved your query?