scoped_storage 0.0.1 scoped_storage: ^0.0.1 copied to clipboard
Scoped storage for flutter (Android 30)
import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:scoped_storage/scoped_storage.dart';
void main() {
runApp(MaterialApp(home: ScopedStorageDemo()));
}
class ScopedStorageDemo extends StatefulWidget {
@override
_ScopedStorageDemoState createState() => _ScopedStorageDemoState();
}
class _ScopedStorageDemoState extends State<ScopedStorageDemo> {
BuildContext scaffoldContext;
TextEditingController directory = TextEditingController();
TextEditingController filename = TextEditingController();
double compression = 95;
CompressFormat selectedFormat = CompressFormat.PNG;
List<dynamic> images = [];
// _fetch() async {
// images = await ScopedStorage.getImages(subDirectory: directory.text);
// setState(() {
// print(images.join(','));
// });
// }
_save() async {
try {
ByteData bytes = await rootBundle.load('assets/bird.jpg');
var buffer = bytes.buffer;
final res = await ScopedStorage.saveImage(
Uint8List.view(buffer), filename.text,
subDirectory: directory.text,
compressFormat: selectedFormat,
compression: compression.toInt());
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text((res is bool && res) ? 'Success' : 'Fail'),
content: Text('Response: $res'),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'))
],
));
} catch (_) {
showDialog(
context: scaffoldContext,
builder: (context) => AlertDialog(
title: Text('Failed to save'),
content: SingleChildScrollView(child: Text('Err: $_')),
actions: [
ElevatedButton(
onPressed: () => Navigator.pop(context),
child: Text('OK'))
],
));
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Builder(builder: (context) {
this.scaffoldContext = context;
return SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text('Filename:\nDefault: System.currentTimeMillis()'),
TextField(
controller: filename,
decoration: new InputDecoration(
border: OutlineInputBorder(), hintText: "Image_01"),
),
SizedBox(height: 16),
Text('Sub directory inside /Pictures:\nDefault: /Pictures'),
TextField(
controller: directory,
decoration: new InputDecoration(
border: OutlineInputBorder(), hintText: "MyApp"),
),
SizedBox(height: 16),
Text('Compression:\nDefault: 95'),
Slider(
value: compression,
min: 0,
max: 100,
onChanged: (newValue) =>
setState(() => compression = newValue)),
SizedBox(height: 16),
Text('Compress Format:\nDefault: PNG'),
DropdownButton<CompressFormat>(
value: selectedFormat,
onChanged: (CompressFormat newValue) =>
setState(() => selectedFormat = newValue),
items:
CompressFormat.values.map((CompressFormat classType) {
return DropdownMenuItem<CompressFormat>(
value: classType,
child: Text(classType.toString()));
}).toList()),
SizedBox(height: 16),
ElevatedButton(onPressed: _save, child: Text('Save')),
// SizedBox(height: 16),
// ElevatedButton(
// onPressed: _fetch, child: Text('fetch Images')),
// Container(
// width: 100,
// height: 100,
// child: ListView.builder(
// itemCount: images.length,
// itemBuilder: (context, index) => Container(
// padding: EdgeInsets.all(2),
// child:
// Image.file(File.fromUri(Uri.parse(images[index]))),
// ),
// scrollDirection: Axis.horizontal,
// ),
// )
]),
),
);
}),
);
}
}