-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.dart
139 lines (115 loc) · 3.9 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_photokit/flutter_photokit.dart';
import 'package:path_provider/path_provider.dart';
enum _SaveStatus { NONE, SAVING, SAVED }
void main() => runApp(const MyApp());
class MyApp extends StatefulWidget {
const MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
_SaveStatus _cameraRollStatus = _SaveStatus.NONE;
_SaveStatus _albumStatus = _SaveStatus.NONE;
TextEditingController _albumTextController;
static HttpClient _httpClient = HttpClient();
static String _sampleImageUrl =
'https://cdn-images-1.medium.com/max/1200/1*5-aoK8IBmXve5whBQM90GA.png';
// Use this link to test with a video.
// static String _sampleVideoUrl = 'https://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4';
@override
void initState() {
super.initState();
_albumTextController = TextEditingController()
..addListener(() => setState(() {}));
}
Future<File> _downloadFile(String url) async {
Uri uri = Uri.parse(url);
HttpClientRequest req = await _httpClient.getUrl(uri);
HttpClientResponse res = await req.close();
List<int> bytes = await consolidateHttpClientResponseBytes(res);
String tempDir = (await getTemporaryDirectory()).path;
File outputFile = File('$tempDir/${uri.pathSegments.last}');
await outputFile.writeAsBytes(bytes);
return outputFile;
}
void _saveToCameraRoll() async {
setState(() => _cameraRollStatus = _SaveStatus.SAVING);
File file = await _downloadFile(_sampleImageUrl);
await FlutterPhotokit.saveToCameraRoll(filePath: file.path);
setState(() => _cameraRollStatus = _SaveStatus.SAVED);
}
void _saveToAlbum() async {
setState(() => _albumStatus = _SaveStatus.SAVING);
File file = await _downloadFile(_sampleImageUrl);
await FlutterPhotokit.saveToAlbum(
filePath: file.path, albumName: _albumTextController.value.text);
setState(() => _albumStatus = _SaveStatus.SAVED);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Flutter PhotoKit Example'),
),
body: _appBody()),
);
}
Widget _appBody() {
if (Platform.isIOS) {
return SingleChildScrollView(
child: new Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
Image.network(_sampleImageUrl),
SizedBox(height: 16.0),
_saveToCameraRollRow(),
SizedBox(height: 16.0),
_saveToAlbumRow()
],
));
} else {
return Center(
child: Text('The PhotoKit plugin is only available for iOS!'));
}
}
Widget _saveToCameraRollRow() {
switch (_cameraRollStatus) {
case _SaveStatus.NONE:
return RaisedButton(
onPressed: _saveToCameraRoll, child: Text('Save to Camera Roll'));
case _SaveStatus.SAVING:
return CircularProgressIndicator();
case _SaveStatus.SAVED:
return Icon(Icons.check);
}
}
Widget _saveToAlbumRow() {
switch (_albumStatus) {
case _SaveStatus.NONE:
return Row(
children: <Widget>[
Expanded(
child: TextField(
controller: _albumTextController,
decoration: InputDecoration(labelText: 'Album name')),
),
RaisedButton(
onPressed: _albumTextController.value.text.trim().isEmpty
? null
: _saveToAlbum,
child: Text('Save to album'),
)
],
);
case _SaveStatus.SAVING:
return CircularProgressIndicator();
case _SaveStatus.SAVED:
return Icon(Icons.check);
}
}
}