lecle_native_camera 0.0.3+2
lecle_native_camera: ^0.0.3+2 copied to clipboard
A Flutter project to open device's camera to capture image and video and get the file path on Android and iOS platforms
example/lib/main.dart
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:lecle_native_camera/constants/enums/enums.dart';
import 'package:lecle_native_camera/lecle_native_camera.dart';
import 'package:lecle_native_camera/models/file_data_model.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:video_player/video_player.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
File? imageFile;
File? videoFile;
VideoPlayerController? _controller;
bool _isPlaying = false;
@override
void initState() {
super.initState();
_requestPermissions();
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
void _requestPermissions() async {
await [Permission.camera, Permission.microphone, Permission.storage]
.request();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: SizedBox(
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisSize: MainAxisSize.max,
children: [
Visibility(
visible: imageFile != null,
child: imageFile != null
? Image.file(imageFile!)
: const SizedBox.shrink(),
),
Visibility(
visible: videoFile != null && _controller != null,
child: videoFile != null && _controller != null
? AspectRatio(
child: VideoPlayer(_controller!),
aspectRatio: _controller!.value.aspectRatio,
)
: const SizedBox.shrink(),
),
const SizedBox(height: 32.0),
ElevatedButton(
onPressed: () async {
if (videoFile == null) {
return;
}
_controller?.value.isPlaying ?? false
? await _controller?.pause()
: await _controller?.play();
if (_controller?.value.isPlaying ?? false) {
setState(() {
_isPlaying = true;
});
} else {
setState(() {
_isPlaying = false;
});
}
},
child: Icon(
_isPlaying ? Icons.pause : Icons.play_arrow,
),
),
ElevatedButton(
onPressed: () {
LecleNativeCamera.openNativeCamera(
fileProviderPath:
'vn.lecle.native_camera.fileprovider', // This must be the same with the custom path that you defined in the AndroidManifest file
imageExtension: ImageExtension.jpeg,
videoQuality: VideoQuality.typeHigh,
videoExtension: VideoExtension.mov,
imageQuality: 100,
imageWidth: 2160,
imageHeight: 3804,
androidOptionSheetTitle: 'Choose an option',
canPickFile: true,
fileName: 'your_custom_file_name',
customSaveFolder: 'Test Folder',
).then(
(value) async {
if (value != null) {
print('$value');
if (value.fileType == FileType.image &&
value.filePath != null) {
setState(() {
imageFile = File(value.filePath!);
imageFile!.length().then((value) {
print('File size ::: $value');
});
videoFile = null;
_controller = null;
});
} else if (value.fileType == FileType.video &&
value.filePath != null) {
setState(() {
videoFile = File(value.filePath!);
videoFile!.length().then((value) {
print('File size ::: $value');
});
imageFile = null;
_controller =
VideoPlayerController.file(videoFile!);
_controller!.initialize().then(
(value) {
setState(() {});
_controller!.addListener(() {
if (_controller?.value.position ==
_controller?.value.duration) {
setState(() {
_isPlaying = false;
});
}
});
},
);
});
}
}
},
);
},
child: const Text('Open native camera'),
),
],
),
),
),
);
}
}