scanbot_image_picker 1.0.1-alpha1 copy "scanbot_image_picker: ^1.0.1-alpha1" to clipboard
scanbot_image_picker: ^1.0.1-alpha1 copied to clipboard

Simple Image Picker with single and multiple selection capability.

example/lib/main.dart

import 'dart:io';

import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:scanbot_image_picker/scanbot_image_picker_flutter.dart';

void main() {
  runApp(const MaterialApp(home: MyApp()));
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool resetView = true;
  List<String> _pickedImages = List.empty(growable: true);

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

  /// Platform messages are asynchronous, so we initialize in an async method.
  Future<String> initPlatformState() async {
    String platformVersion;
    try {
      platformVersion = await ScanbotImagePickerFlutter.platformVersion ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    if (!mounted) return platformVersion;
    return platformVersion;
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Image Picker'),
          actions: [
            IconButton(
                onPressed: () {
                  resetAll();
                },
                icon: const Icon(Icons.refresh, size: 25))
          ],
        ),
        body: getWidget(),
      ),
    );
  }

  /// get the widget according to the view state
  Widget getWidget() {
    Widget widget;
    if (resetView) {
      widget = getOptionsWidget();
    } else {
      // get image list widget
      widget = getImageListWidget();
    }
    return widget;
  }

  /// get the initial widget which shows options to open image picker for single/multiple images.
  Widget getOptionsWidget() {
    return Center(
        child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [
      TextButton(
        child: const Text("Pick Image"),
        style: ButtonStyle(
          foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
        ),
        onPressed: () {
          pickImageFromPhotoGallery();
        },
      ),
      TextButton(
        child: const Text("Pick Images"),
        style: ButtonStyle(
          foregroundColor: MaterialStateProperty.all<Color>(Colors.blue),
        ),
        onPressed: () {
          pickImagesFromPhotoGallery();
        },
      )
    ]));
  }

  /// Widget to display picked images list.
  Widget getImageListWidget() {
    return GridView.builder(
        padding: const EdgeInsets.all(10),
        gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
            crossAxisCount: 3, crossAxisSpacing: 10, mainAxisSpacing: 10),
        itemCount: _pickedImages.length,
        itemBuilder: (context, index) {
          return Container(
              decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(7),
                  image: DecorationImage(
                      image:  FileImage(File(_pickedImages[index])),scale: 0.5,
                    fit: BoxFit.cover
                  )
              )
          );
        }
    );
  }

  /// resets the view to initial state, clears the list and allows user to select more images.
  void resetAll() {
    setState(() {
      resetView = true;
      _pickedImages.clear();
    });
  }

  /// picks multiple images from photo gallery and gets the output of selected image uris.
  Future pickImagesFromPhotoGallery() async {
    var response = await ScanbotImagePickerFlutter.pickImagesAsync();
    // display images
    setState(() {
      resetView = false;
      _pickedImages = response.getNonEmptyUris();
      if (_pickedImages.isEmpty) {
        resetAll();
      }
    });

    if (response.message?.isNotEmpty == true) {
      // show error message
      showAlert(response.message ?? "");
    }
  }

  /// picks single image from photo gallery and gets the output of selected image uris.
  Future pickImageFromPhotoGallery() async {
    var response = await ScanbotImagePickerFlutter.pickImageAsync();
    // display image
    setState(() {
      resetView = false;
      if (response.uri?.isNotEmpty == true) {
        _pickedImages.add(response.uri ?? "");
      } else {
        resetAll();
      }
    });

    if (response.message?.isNotEmpty == true) {
      // show error message
      showAlert(response.message ?? "");
    }
  }

  /// Show alert message.
  void showAlert(String message) {
    var alertBuilder = AlertDialog(
      title: const Text("Alert"),
      content: Text(message),
      actions: [
        TextButton(
            onPressed: () {
              Navigator.of(context).pop();
            },
            child: const Text("Ok"))
      ],
    );

    showDialog(
        context: context, builder: (BuildContext context) => alertBuilder);
  }
}
3
likes
0
pub points
80%
popularity

Publisher

verified publisherscanbot.io

Simple Image Picker with single and multiple selection capability.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, image_picker

More

Packages that depend on scanbot_image_picker