storage_permission_handler 1.0.0
storage_permission_handler: ^1.0.0 copied to clipboard
A Android Storage Permission Handler for Multiple Versions
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:storage_permission_handler/storage_permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Storage Permission Example'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
title: Text(widget.title),
),
body: Center(
child: ElevatedButton(
style: ElevatedButton.styleFrom(
backgroundColor: Colors.deepPurple,
padding: EdgeInsets.symmetric(horizontal: 30, vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
shadowColor: Colors.black.withOpacity(0.2),
elevation: 5,
side: BorderSide(width: 2, color: Colors.deepPurple),
).copyWith(
elevation: MaterialStateProperty.all(10),
),
onPressed: () => _checkStoragePermission(),
child: Text(
'Request Storage Permission',
style: TextStyle(color: Colors.white),
))),
);
}
void _checkStoragePermission() async {
final hasStoragePermission = await StoragePermissionHandler.hasStoragePermission();
if (hasStoragePermission) {
// call method for saving files or other
} else {
final status = await StoragePermissionHandler.requestStoragePermission();
if (status == true) {
// call method for saving files or other
} else {
debugPrint("Please grant storage permission");
}
}
}
}