lecle_flutter_absolute_path 0.0.1+1 lecle_flutter_absolute_path: ^0.0.1+1 copied to clipboard
A Flutter project to get the absolute path of a file on Android or iOS device.
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:lecle_flutter_absolute_path/lecle_flutter_absolute_path.dart';
void main() {
runApp(
const MaterialApp(
home: LecleFlutterAbsolutePathDemo(),
),
);
}
class LecleFlutterAbsolutePathDemo extends StatefulWidget {
const LecleFlutterAbsolutePathDemo({Key? key}) : super(key: key);
@override
State<LecleFlutterAbsolutePathDemo> createState() =>
_LecleFlutterAbsolutePathDemoState();
}
class _LecleFlutterAbsolutePathDemoState
extends State<LecleFlutterAbsolutePathDemo> {
List<String> _absolutePaths = [];
// Get images messages are asynchronous, so we initialize in an async method.
Future<void> getImages() async {
List<String> absolutePaths = [];
// Get image messages may fail, so we use if to check if assets data is available.
// We also handle the message potentially returning null.
List<XFile>? assets = await selectImagesFromGallery();
if (assets != null) {
for (XFile asset in assets) {
final filePath =
await LecleFlutterAbsolutePath.getAbsolutePath(asset.path);
if (filePath != null) {
absolutePaths.add(filePath);
}
}
}
// 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(() {
_absolutePaths = List.from(absolutePaths);
});
// Use with wechat_assets_picker package
// AssetPicker.pickAssets(context).then((assets) async {
// if (assets != null && assets.isNotEmpty) {
// for (var asset in assets) {
// final file = await asset.file;
// if (file != null) {
// final filePath = await LecleFlutterAbsolutePath.getAbsolutePath(file.path);
// if (filePath != null) {
// absolutePaths.add(filePath);
// }
// }
// }
// }
// });
}
Future<List<XFile>?> selectImagesFromGallery() async {
ImagePicker picker = ImagePicker();
return await picker.pickMultiImage();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
floatingActionButton: FloatingActionButton(
child: const Icon(Icons.image),
onPressed: () async {
getImages();
},
),
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
const Padding(
child: Text('Absolute paths'),
padding: EdgeInsets.only(bottom: 16.0),
),
...List.generate(
_absolutePaths.length,
(index) => Padding(
padding: const EdgeInsets.only(bottom: 16.0),
child:
Text(_absolutePaths[index], textAlign: TextAlign.center),
),
),
],
),
),
),
);
}
}