selectImage method

dynamic selectImage(
  1. BuildContext context,
  2. String positiveText,
  3. String negativeText,
  4. String message,
  5. dynamic onSelect(
    1. File
    )
)

Opens dialog to choose image from gallery or camera

Implementation

selectImage(BuildContext context, String positiveText, String negativeText,
    String message, Function(File) onSelect) async {
  _positiveText = positiveText;
  _negativeText = negativeText;
  _message = message;
  await showDialog(
    context: context,
    builder: (context) => StreamBuilder(
      initialData: false,
      stream: _controller.stream,
      builder: (context, snapshot){
        return WillPopScope(
          onWillPop: () async {
            _controller.close();
            return true;},
          child: CustomLoaderWidget(
            isTrue: snapshot.hasData && snapshot.data,
            child: new AlertDialog(
              title: Text(message),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Divider(
                    color: Colors.grey[400],
                  ),
                  ListTile(
                    onTap: () async {
                      File selectedImage =
                      await _getImage(context, ImageSource.camera);
                      if (selectedImage != null) {
                        {
                          var result = await _showSelectedImageDialog(
                              selectedImage, context);
                          if (result) {
                            _controller.sink.add(true);
                            await onSelect(selectedImage);
                            Navigator.pop(context);
                          } else
                            Navigator.pop(context);
                        }
                      }
                    },
                    title: Text("Take Photo"),
                  ),
                  ListTile(
                    onTap: () async {
                      File selectedImage =
                      await _getImage(context, ImageSource.gallery);
                      if (selectedImage != null) {
                        var result = await _showSelectedImageDialog(
                            selectedImage, context);
                        if (result) {
                          _controller.sink.add(true);
                          await onSelect(selectedImage);
                          Navigator.pop(context);
                        } else
                          Navigator.pop(context);
                      }
                    },
                    title: Text("Select from Gallery"),
                  ),
                ],
              ),
              actions: <Widget>[
                new FlatButton(
                    onPressed: () => Navigator.pop(context),
                    child: new Text(negativeText))
              ],
            ),
          ),
        );
      }

    ),
  );
}