imagepickerv3 0.0.12 copy "imagepickerv3: ^0.0.12" to clipboard
imagepickerv3: ^0.0.12 copied to clipboard

imagepicker v3

example/lib/main.dart

import 'dart:io';
import 'dart:typed_data';

import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';

import 'package:imagepickerv3/images_picker.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

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

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  File? paththumb;
  var bi;
  
  // final _imagepickerv3Plugin = Imagepickerv3();

  @override
  void initState() {
    super.initState();
    // initPlatformState();
    Future.delayed(Duration(seconds: 1)).then((va){
      requestPermission();
    });
  }

  requestPermission() async {
    var permissssion = false;
    if (Platform.isAndroid){
      DeviceInfoPlugin plugin = DeviceInfoPlugin();
      AndroidDeviceInfo android = await plugin.androidInfo;
      if (android.version.sdkInt < 33){
        if (await Permission.storage.request().isGranted){
          permissssion = true;
        }else if (await Permission.storage.request().isPermanentlyDenied){
          await openAppSettings();
        }else if (await Permission.storage.request().isDenied){
          permissssion = false;
        }
      }else{
        permissssion = true;
      }

    }else{
      List<Permission> permissions;
      permissions = <Permission>[
        Permission.camera,
        Permission.photos,
        Permission.photosAddOnly,
        Permission.storage,
        Permission.manageExternalStorage,
        Permission.notification,
        // Permission.location
      ];
      permissions.request();
    }

    print("permission = ${permissssion}");
  }
  
  checkemulated(){
    if (paththumb != null){
      return normalfile(paththumb!);
    }else{
      return Container();
    }
  }

  Future<File?> moveFile(String sourcePath) async {
    File? newpath;
    try {
      final file = File(sourcePath);
      final directory = await getTemporaryDirectory();


      // Check if the source file exists
      if (await file.exists()) {
        // Move the file
        final newFile = await file.copy("${directory.path}temp.jpg");
        print("File moved to: ${newFile.path}");
        newpath = newFile;
      } else {
        print("Source file does not exist.");
      }
    } catch (e) {
      print("Error moving file: $e");
    }

    return newpath;
  }


  getimage(){

    ImagesPicker.pick(pickType: PickType.image).then((value){

      value?.forEach((a) async {
        if (a.thumbPath != null){
          File filethumb = File(a.thumbPath!);
          paththumb = filethumb;
        }

        // if (filethumb.existsSync()) {
        //   print('File exists: ${a.thumbPath}');
        //   if (a.thumbPath!.contains("emulated/0")){
        //    moveFile(a.thumbPath!).then((value){
        //      if (value != null){
        //        paththumb = value;
        //        setState(() {
        //
        //        });
        //      }
        //    });
        //   }
        // } else {
        //   print('File does not exist: ${a.thumbPath}');
        // }
        setState(() {

        });

  

      });
    });
  }
  
  Widget bitmat(Uint8List bytes){
    return Image.memory(bytes);
  }

  Widget normalfile(File fiel){
    return Image.file(fiel);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              GestureDetector(
                onTap: (){
                  getimage();
                },
                child: Container(
                  height: 100,
                  width: 100,
                  color: Colors.blue,
                ),
              ),
              paththumb != null ? Container(
                height: 100,
                width: 100,
                child: checkemulated(),
              ) :  Container(),
              Text('Running on: $paththumb\n'),
            ],
          ),
        ),
      ),
    );
  }
}