imagepickerv3 0.0.11 copy "imagepickerv3: ^0.0.11" to clipboard
imagepickerv3: ^0.0.11 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 {
    print("hhee");
    var permissssion = false;
    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;
    }

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

    // if (paththumb != null){
    //   if (paththumb!.path.contains("emulated/0")){
    //       Uint8List abit = paththumb!.readAsBytesSync();
    //       return bitmat(abit);
    //   }else{
    //     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.video).then((value){

      value?.forEach((a) async {
        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(() {

        });

  

      });
    });
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  // Future<void> initPlatformState() async {
  //   String platformVersion;
  //   // Platform messages may fail, so we use a try/catch PlatformException.
  //   // We also handle the message potentially returning null.
  //   try {
  //     platformVersion =
  //         await _imagepickerv3Plugin.getPlatformVersion() ?? 'Unknown platform version';
  //   } on PlatformException {
  //     platformVersion = '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;
  //
  //   setState(() {
  //     _platformVersion = platformVersion;
  //   });
  // }
  
  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'),
            ],
          ),
        ),
      ),
    );
  }
}