Skip to content

Commit

Permalink
* Updated example
Browse files Browse the repository at this point in the history
* Fixed error when no thumbnail.
  • Loading branch information
aloisdeniel committed May 4, 2020
1 parent 8a6722c commit dfb91ba
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 69 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.1.3

* Updated example
* Fixed error when no thumbnail.

## 0.1.2

* Added "All" collection.
Expand Down
118 changes: 118 additions & 0 deletions example/lib/picker/albums.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import 'package:flutter/material.dart';
import 'package:media_gallery/media_gallery.dart';

import 'thumbnail.dart';
import 'labels.dart';
import 'medias.dart';
import 'selection.dart';

class MediaAlbums extends StatelessWidget {
final List<MediaCollection> collections;

const MediaAlbums({
@required this.collections,
});

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final labels = MediaPickerLabels.of(context);
final selection = MediaPickerSelection.of(context);
final children = collections
.map<Widget>(
(x) => AlbumTile(
collection: x,
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MediaPickerSelectionProvider(
selection: selection,
child: MediaPickerLabelsProvider(
value: labels,
child: MediasPage(
collection: x,
),
),
),
),
);
if (result != null) {
Navigator.pop(context, result);
}
},
),
)
.toList();
return ListView.separated(
separatorBuilder: (context, i) => Container(
height: 1,
color: theme.textTheme.body1.color.withOpacity(0.12),
),
itemBuilder: (context, i) => children[i],
itemCount: children.length,
);
}
}

class AlbumTile extends StatelessWidget {
final GestureTapCallback onTap;
final MediaCollection collection;

const AlbumTile({
@required this.onTap,
@required this.collection,
});

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final labels = MediaPickerLabels.of(context);
return InkWell(
onTap: onTap,
child: SafeArea(
top: false,
bottom: false,
child: Padding(
padding: const EdgeInsets.all(12.0),
child: SizedBox(
height: 64,
child: Row(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(4.0),
child: Container(
width: 64,
height: 64,
color: theme.textTheme.subhead.color.withOpacity(0.1),
child:
MediaCollectionThumbnailImage(collection: collection),
),
),
SizedBox(
width: 12.0,
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
collection.name,
style: theme.textTheme.subhead,
),
Text(
'${collection.count} ${labels.items}',
style: theme.textTheme.caption,
),
],
),
)
],
),
),
),
),
);
}
}
97 changes: 52 additions & 45 deletions example/lib/picker/collections.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import 'package:flutter/material.dart';
import 'labels.dart';
import 'package:media_gallery/media_gallery.dart';
import 'package:media_gallery_example/picker/selection.dart';
import 'package:media_gallery_example/picker/validate.dart';
import 'package:permission_handler/permission_handler.dart';

import 'albums.dart';
import 'selection.dart';
import 'validate.dart';
import 'medias.dart';
import 'thumbnail.dart';

Expand All @@ -26,60 +27,66 @@ class _MediaCollectionsPageState extends State<MediaCollectionsPage> {
Future<void> initAsync() async {
final selection = MediaPickerSelection.of(context);
try {
this.collections = await MediaGallery.listMediaCollections(
collections = await MediaGallery.listMediaCollections(
mediaTypes: selection.mediaTypes,
);
this.setState(() {});
setState(() {});
} catch (e) {
print("Failed : $e");
print('Failed : $e');
}
}

@override
Widget build(BuildContext context) {
final selection = MediaPickerSelection.of(context);
return Scaffold(
appBar: AppBar(
title: Text('Select medias'),
actions: <Widget>[
PickerValidateButton(
onValidate: (selection) => Navigator.pop(context, selection),
),
],
),
body: ListView(
children: <Widget>[
...collections.map<Widget>(
(x) => Card(
child: ListTile(
leading: SizedBox(
width: 64,
child: MediaCollectionThumbnailImage(collection: x),
),
onTap: () async {
final result = await Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MediaPickerSelectionProvider(
selection: selection,
child: MediasPage(
collection: x,
),
),
),
);
if (result != null) {
Navigator.pop(context, result);
}
},
title: Text(
x.name,
final labels = MediaPickerLabels.of(context);
final allCollection = collections.firstWhere(
(c) => c.isAllCollection,
orElse: () => null,
);
return DefaultTabController(
length: selection.mediaTypes.length + 1,
child: Scaffold(
appBar: AppBar(
title: Text(labels.collectionsTitle),
actions: <Widget>[
PickerValidateButton(
onValidate: (selection) => Navigator.pop(context, selection),
),
],
bottom: TabBar(
tabs: [
...selection.mediaTypes.map(
(x) => Tab(
text: x == MediaType.video ? labels.videos : labels.images,
),
subtitle: Text("${x.count} item(s)"),
),
Tab(
text: labels.albums,
),
],
),
),
body: TabBarView(
children: [
...selection.mediaTypes.map(
(x) => allCollection == null
? SizedBox()
: MediaGrid(
key: Key(x.toString()),
collection: allCollection,
mediaType: x,
),
),
MediaAlbums(
collections: collections
.where(
(x) => !x.isAllCollection,
)
.toList(),
),
)
],
],
),
),
);
}
Expand Down
62 changes: 62 additions & 0 deletions example/lib/picker/labels.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'package:flutter/widgets.dart';
import 'package:meta/meta.dart';

@immutable
class MediaPickerLabels {
final String collectionsTitle;
final String items;
final String images;
final String albums;
final String videos;
final String mediaNotAuthorizedAccessTitle;
final String mediaNotAuthorizedAccessDescription;
final String openSettings;

const MediaPickerLabels({
@required this.collectionsTitle,
@required this.images,
@required this.videos,
@required this.albums,
@required this.items,
@required this.mediaNotAuthorizedAccessTitle,
@required this.mediaNotAuthorizedAccessDescription,
@required this.openSettings,
});

static MediaPickerLabels of(BuildContext context) {
final provider =
context.dependOnInheritedWidgetOfExactType<MediaPickerLabelsProvider>();
assert(provider != null);
return provider.value;
}

factory MediaPickerLabels.fallback() => const MediaPickerLabels(
collectionsTitle: 'Select images',
albums: 'Albums',
images: 'Photos',
videos: 'Videos',
items: 'item(s)',
mediaNotAuthorizedAccessTitle: 'Not authorized',
mediaNotAuthorizedAccessDescription:
"This app can't have access to user media gallery. You must update authorizations in app settings.",
openSettings: 'Open settings',
);
}

class MediaPickerLabelsProvider extends InheritedWidget {
final MediaPickerLabels value;

const MediaPickerLabelsProvider({
Key key,
@required Widget child,
@required this.value,
}) : super(
key: key,
child: child,
);

@override
bool updateShouldNotify(MediaPickerLabelsProvider oldWidget) {
return value != oldWidget.value;
}
}
27 changes: 17 additions & 10 deletions example/lib/picker/medias.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import 'package:flutter/material.dart';
import 'package:media_gallery/media_gallery.dart';
import 'package:media_gallery_example/picker/selection.dart';
import 'package:media_gallery_example/picker/validate.dart';

import 'labels.dart';
import 'selection.dart';
import 'validate.dart';
import 'selectable.dart';
import 'thumbnail.dart';

Expand All @@ -20,6 +21,7 @@ class _MediaImagesPageState extends State<MediasPage> {
@override
Widget build(BuildContext context) {
final selection = MediaPickerSelection.of(context);
final labels = MediaPickerLabels.of(context);
return DefaultTabController(
length: selection.mediaTypes.length,
child: Scaffold(
Expand All @@ -32,17 +34,22 @@ class _MediaImagesPageState extends State<MediasPage> {
],
bottom: selection.mediaTypes.length > 1
? TabBar(
tabs: [
Tab(text: "Photos"),
Tab(text: "Videos"),
],
tabs: selection.mediaTypes
.map(
(x) => Tab(
text: x == MediaType.video
? labels.videos
: labels.images,
),
)
.toList(),
)
: null,
),
body: TabBarView(
children: selection.mediaTypes
.map(
(x) => _MediaGrid(
(x) => MediaGrid(
key: Key(x.toString()),
collection: widget.collection,
mediaType: x,
Expand All @@ -55,10 +62,10 @@ class _MediaImagesPageState extends State<MediasPage> {
}
}

class _MediaGrid extends StatefulWidget {
class MediaGrid extends StatefulWidget {
final MediaCollection collection;
final MediaType mediaType;
_MediaGrid({
MediaGrid({
Key key,
@required this.mediaType,
@required this.collection,
Expand All @@ -68,7 +75,7 @@ class _MediaGrid extends StatefulWidget {
_MediaGridState createState() => _MediaGridState();
}

class _MediaGridState extends State<_MediaGrid>
class _MediaGridState extends State<MediaGrid>
with AutomaticKeepAliveClientMixin {
List<MediaPage> pages = [];
bool isLoading = false;
Expand Down
Loading

0 comments on commit dfb91ba

Please sign in to comment.