2020-08-14 18:07:27 +08:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:photo_gallery/photo_gallery.dart';
|
|
|
|
import 'package:transparent_image/transparent_image.dart';
|
2020-08-22 15:32:52 +08:00
|
|
|
import 'package:video_player/video_player.dart';
|
2020-08-14 18:07:27 +08:00
|
|
|
|
|
|
|
void main() {
|
|
|
|
runApp(MyApp());
|
|
|
|
}
|
|
|
|
|
|
|
|
class MyApp extends StatefulWidget {
|
|
|
|
@override
|
|
|
|
_MyAppState createState() => _MyAppState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _MyAppState extends State<MyApp> {
|
2021-03-30 22:30:21 +08:00
|
|
|
List<Album>? _albums;
|
2020-08-14 18:07:27 +08:00
|
|
|
bool _loading = false;
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
_loading = true;
|
|
|
|
initAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initAsync() async {
|
|
|
|
if (await _promptPermissionSetting()) {
|
2023-05-11 21:56:25 +08:00
|
|
|
List<Album> albums = await PhotoGallery.listAlbums();
|
2020-08-14 18:07:27 +08:00
|
|
|
setState(() {
|
2020-09-20 16:04:55 +08:00
|
|
|
_albums = albums;
|
2020-08-14 18:07:27 +08:00
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
setState(() {
|
|
|
|
_loading = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _promptPermissionSetting() async {
|
2023-05-14 02:23:49 +08:00
|
|
|
if (Platform.isIOS) {
|
2023-07-13 19:37:47 +03:00
|
|
|
if (await Permission.photos.request().isGranted) {
|
2023-05-14 02:23:49 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Platform.isAndroid) {
|
|
|
|
if (await Permission.storage.request().isGranted ||
|
|
|
|
await Permission.photos.request().isGranted &&
|
|
|
|
await Permission.videos.request().isGranted) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
title: const Text('Photo gallery example'),
|
|
|
|
),
|
|
|
|
body: _loading
|
|
|
|
? Center(
|
|
|
|
child: CircularProgressIndicator(),
|
|
|
|
)
|
|
|
|
: LayoutBuilder(
|
|
|
|
builder: (context, constraints) {
|
|
|
|
double gridWidth = (constraints.maxWidth - 20) / 3;
|
|
|
|
double gridHeight = gridWidth + 33;
|
|
|
|
double ratio = gridWidth / gridHeight;
|
|
|
|
return Container(
|
|
|
|
padding: EdgeInsets.all(5),
|
|
|
|
child: GridView.count(
|
|
|
|
childAspectRatio: ratio,
|
|
|
|
crossAxisCount: 3,
|
|
|
|
mainAxisSpacing: 5.0,
|
|
|
|
crossAxisSpacing: 5.0,
|
|
|
|
children: <Widget>[
|
2020-09-20 16:04:55 +08:00
|
|
|
...?_albums?.map(
|
|
|
|
(album) => GestureDetector(
|
2020-08-14 18:07:27 +08:00
|
|
|
onTap: () => Navigator.of(context).push(
|
2023-05-14 02:23:53 +08:00
|
|
|
MaterialPageRoute(builder: (context) => AlbumPage(album)),
|
|
|
|
),
|
2020-08-14 18:07:27 +08:00
|
|
|
child: Column(
|
|
|
|
children: <Widget>[
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(5.0),
|
|
|
|
child: Container(
|
|
|
|
color: Colors.grey[300],
|
|
|
|
height: gridWidth,
|
|
|
|
width: gridWidth,
|
|
|
|
child: FadeInImage(
|
|
|
|
fit: BoxFit.cover,
|
2023-05-14 02:23:53 +08:00
|
|
|
placeholder: MemoryImage(kTransparentImage),
|
2020-08-14 18:07:27 +08:00
|
|
|
image: AlbumThumbnailProvider(
|
2023-05-13 15:49:58 +08:00
|
|
|
album: album,
|
2020-08-14 18:07:27 +08:00
|
|
|
highQuality: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
padding: EdgeInsets.only(left: 2.0),
|
|
|
|
child: Text(
|
2021-10-20 21:27:32 +08:00
|
|
|
album.name ?? "Unnamed Album",
|
2020-08-14 18:07:27 +08:00
|
|
|
maxLines: 1,
|
|
|
|
textAlign: TextAlign.start,
|
|
|
|
style: TextStyle(
|
|
|
|
height: 1.2,
|
|
|
|
fontSize: 16,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Container(
|
|
|
|
alignment: Alignment.topLeft,
|
|
|
|
padding: EdgeInsets.only(left: 2.0),
|
|
|
|
child: Text(
|
2020-09-20 16:04:55 +08:00
|
|
|
album.count.toString(),
|
2020-08-14 18:07:27 +08:00
|
|
|
textAlign: TextAlign.start,
|
|
|
|
style: TextStyle(
|
|
|
|
height: 1.2,
|
|
|
|
fontSize: 12,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-20 16:04:55 +08:00
|
|
|
class AlbumPage extends StatefulWidget {
|
|
|
|
final Album album;
|
2020-08-14 18:07:27 +08:00
|
|
|
|
2020-09-20 16:04:55 +08:00
|
|
|
AlbumPage(Album album) : album = album;
|
2020-08-14 18:07:27 +08:00
|
|
|
|
|
|
|
@override
|
2020-09-20 16:04:55 +08:00
|
|
|
State<StatefulWidget> createState() => AlbumPageState();
|
2020-08-14 18:07:27 +08:00
|
|
|
}
|
|
|
|
|
2020-09-20 16:04:55 +08:00
|
|
|
class AlbumPageState extends State<AlbumPage> {
|
2021-03-30 22:30:21 +08:00
|
|
|
List<Medium>? _media;
|
2020-08-14 18:07:27 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
|
|
|
initAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
void initAsync() async {
|
2020-09-20 16:04:55 +08:00
|
|
|
MediaPage mediaPage = await widget.album.listMedia();
|
2020-08-14 18:07:27 +08:00
|
|
|
setState(() {
|
|
|
|
_media = mediaPage.items;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: IconButton(
|
|
|
|
icon: Icon(Icons.arrow_back_ios),
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
),
|
2021-10-20 21:27:32 +08:00
|
|
|
title: Text(widget.album.name ?? "Unnamed Album"),
|
2020-08-14 18:07:27 +08:00
|
|
|
),
|
|
|
|
body: GridView.count(
|
|
|
|
crossAxisCount: 3,
|
|
|
|
mainAxisSpacing: 1.0,
|
|
|
|
crossAxisSpacing: 1.0,
|
|
|
|
children: <Widget>[
|
|
|
|
...?_media?.map(
|
2020-08-22 14:12:54 +08:00
|
|
|
(medium) => GestureDetector(
|
2023-05-14 02:23:53 +08:00
|
|
|
onTap: () => Navigator.of(context).push(
|
|
|
|
MaterialPageRoute(builder: (context) => ViewerPage(medium)),
|
|
|
|
),
|
2020-08-14 18:07:27 +08:00
|
|
|
child: Container(
|
|
|
|
color: Colors.grey[300],
|
|
|
|
child: FadeInImage(
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
placeholder: MemoryImage(kTransparentImage),
|
|
|
|
image: ThumbnailProvider(
|
2020-08-22 14:12:54 +08:00
|
|
|
mediumId: medium.id,
|
|
|
|
mediumType: medium.mediumType,
|
2020-08-14 18:07:27 +08:00
|
|
|
highQuality: true,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ViewerPage extends StatelessWidget {
|
2020-08-22 14:17:58 +08:00
|
|
|
final Medium medium;
|
2020-08-14 18:07:27 +08:00
|
|
|
|
2020-08-22 14:17:58 +08:00
|
|
|
ViewerPage(Medium medium) : medium = medium;
|
2020-08-14 18:07:27 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-03-30 22:30:21 +08:00
|
|
|
DateTime? date = medium.creationDate ?? medium.modifiedDate;
|
2020-08-14 18:07:27 +08:00
|
|
|
return MaterialApp(
|
|
|
|
home: Scaffold(
|
|
|
|
appBar: AppBar(
|
|
|
|
leading: IconButton(
|
|
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
|
|
icon: Icon(Icons.arrow_back_ios),
|
|
|
|
),
|
2021-03-30 22:30:21 +08:00
|
|
|
title: date != null ? Text(date.toLocal().toString()) : null,
|
2020-08-14 18:07:27 +08:00
|
|
|
),
|
|
|
|
body: Container(
|
|
|
|
alignment: Alignment.center,
|
2020-08-22 15:32:52 +08:00
|
|
|
child: medium.mediumType == MediumType.image
|
|
|
|
? FadeInImage(
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
placeholder: MemoryImage(kTransparentImage),
|
|
|
|
image: PhotoProvider(mediumId: medium.id),
|
|
|
|
)
|
|
|
|
: VideoProvider(
|
|
|
|
mediumId: medium.id,
|
|
|
|
),
|
2020-08-14 18:07:27 +08:00
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-22 15:32:52 +08:00
|
|
|
|
|
|
|
class VideoProvider extends StatefulWidget {
|
|
|
|
final String mediumId;
|
|
|
|
|
|
|
|
const VideoProvider({
|
2021-03-30 22:30:21 +08:00
|
|
|
required this.mediumId,
|
2020-08-22 15:32:52 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
@override
|
|
|
|
_VideoProviderState createState() => _VideoProviderState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _VideoProviderState extends State<VideoProvider> {
|
2021-03-30 22:30:21 +08:00
|
|
|
VideoPlayerController? _controller;
|
|
|
|
File? _file;
|
2020-08-22 15:32:52 +08:00
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2023-05-02 23:05:24 +08:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
2020-08-22 15:32:52 +08:00
|
|
|
initAsync();
|
|
|
|
});
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> initAsync() async {
|
|
|
|
try {
|
|
|
|
_file = await PhotoGallery.getFile(mediumId: widget.mediumId);
|
2021-03-30 22:30:21 +08:00
|
|
|
_controller = VideoPlayerController.file(_file!);
|
|
|
|
_controller?.initialize().then((_) {
|
2020-08-22 15:32:52 +08:00
|
|
|
// Ensure the first frame is shown after the video is initialized, even before the play button has been pressed.
|
|
|
|
setState(() {});
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
print("Failed : $e");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2021-03-30 22:30:21 +08:00
|
|
|
return _controller == null || !_controller!.value.isInitialized
|
2020-08-22 15:32:52 +08:00
|
|
|
? Container()
|
|
|
|
: Column(
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
children: <Widget>[
|
|
|
|
AspectRatio(
|
2021-03-30 22:30:21 +08:00
|
|
|
aspectRatio: _controller!.value.aspectRatio,
|
|
|
|
child: VideoPlayer(_controller!),
|
2020-08-22 15:32:52 +08:00
|
|
|
),
|
2023-05-02 23:05:24 +08:00
|
|
|
TextButton(
|
2020-08-22 15:32:52 +08:00
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
2023-05-14 02:23:53 +08:00
|
|
|
_controller!.value.isPlaying ? _controller!.pause() : _controller!.play();
|
2020-08-22 15:32:52 +08:00
|
|
|
});
|
|
|
|
},
|
|
|
|
child: Icon(
|
2021-03-30 22:30:21 +08:00
|
|
|
_controller!.value.isPlaying ? Icons.pause : Icons.play_arrow,
|
2020-08-22 15:32:52 +08:00
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|