From f091ad19559f655256c52363cc0daadcb008e747 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Tue, 23 Jul 2019 23:54:49 +0900 Subject: [PATCH] improved widget constructors and main future builder --- lib/common/outlined_text.dart | 5 +- lib/image_fullscreen_overlay.dart | 2 +- lib/image_fullscreen_page.dart | 6 +- lib/main.dart | 27 ++++----- lib/model/image_fetcher.dart | 4 +- lib/thumbnail.dart | 2 +- lib/thumbnail_collection.dart | 94 ++++++++++++++++++++----------- 7 files changed, 86 insertions(+), 54 deletions(-) diff --git a/lib/common/outlined_text.dart b/lib/common/outlined_text.dart index 7bd9e3eb3..de3719a63 100644 --- a/lib/common/outlined_text.dart +++ b/lib/common/outlined_text.dart @@ -6,12 +6,13 @@ class OutlinedText extends StatelessWidget { final double outlineWidth; final Color outlineColor; - OutlinedText( + const OutlinedText( this.data, { + Key key, this.style, @required this.outlineWidth, @required this.outlineColor, - }); + }) : super(key: key); @override Widget build(BuildContext context) { diff --git a/lib/image_fullscreen_overlay.dart b/lib/image_fullscreen_overlay.dart index f486fd15d..94fb751f0 100644 --- a/lib/image_fullscreen_overlay.dart +++ b/lib/image_fullscreen_overlay.dart @@ -7,7 +7,7 @@ class FullscreenOverlay extends StatefulWidget { final List entries; final int index; - FullscreenOverlay({this.entries, this.index}); + const FullscreenOverlay({Key key, this.entries, this.index}) : super(key: key); @override State createState() => _FullscreenOverlayState(); diff --git a/lib/image_fullscreen_page.dart b/lib/image_fullscreen_page.dart index 2ebed5f95..d341a8e89 100644 --- a/lib/image_fullscreen_page.dart +++ b/lib/image_fullscreen_page.dart @@ -10,7 +10,11 @@ class ImageFullscreenPage extends StatefulWidget { final List entries; final String initialUri; - ImageFullscreenPage({this.entries, this.initialUri}); + const ImageFullscreenPage({ + Key key, + this.entries, + this.initialUri, + }) : super(key: key); @override ImageFullscreenPageState createState() => ImageFullscreenPageState(); diff --git a/lib/main.dart b/lib/main.dart index 8d3972c4c..e5d2e6e84 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -16,7 +16,7 @@ class MyApp extends StatelessWidget { theme: ThemeData( brightness: Brightness.dark, accentColor: Colors.amberAccent, - scaffoldBackgroundColor: Colors.grey[900] + scaffoldBackgroundColor: Colors.grey[900], ), home: HomePage(), ); @@ -29,18 +29,13 @@ class HomePage extends StatefulWidget { } class _HomePageState extends State { - List imageEntryList; + Future> _entryListLoader; @override void initState() { super.initState(); imageCache.maximumSizeBytes = 100 * 1024 * 1024; - getImageEntries(); - } - - getImageEntries() async { - imageEntryList = await ImageFetcher.getImageEntries(); - setState(() {}); + _entryListLoader = ImageFetcher.getImageEntries(); } @override @@ -49,11 +44,17 @@ class _HomePageState extends State { // fake app bar so that content is safe from status bar, even though we use a SliverAppBar appBar: FakeAppBar(), body: Container( - child: imageEntryList == null - ? Center( - child: CircularProgressIndicator(), - ) - : ThumbnailCollection(imageEntryList), + child: FutureBuilder( + future: _entryListLoader, + builder: (futureContext, AsyncSnapshot> snapshot) { + if (snapshot.connectionState == ConnectionState.done && !snapshot.hasError) { + return ThumbnailCollection(entries: snapshot.data); + } + return Center( + child: CircularProgressIndicator(), + ); + }, + ), ), resizeToAvoidBottomInset: false, ); diff --git a/lib/model/image_fetcher.dart b/lib/model/image_fetcher.dart index 0f05e90be..0cb0cf20d 100644 --- a/lib/model/image_fetcher.dart +++ b/lib/model/image_fetcher.dart @@ -41,7 +41,7 @@ class ImageFetcher { } // return map with: 'aperture' 'exposureTime' 'focalLength' 'iso' - static Future getOverlayMetadata (String path) async { + static Future getOverlayMetadata(String path) async { try { final result = await platform.invokeMethod('getOverlayMetadata', { 'path': path, @@ -52,4 +52,4 @@ class ImageFetcher { } return Map(); } -} \ No newline at end of file +} diff --git a/lib/thumbnail.dart b/lib/thumbnail.dart index e42b375db..66417424f 100644 --- a/lib/thumbnail.dart +++ b/lib/thumbnail.dart @@ -11,7 +11,7 @@ class Thumbnail extends StatefulWidget { final double extent; final double devicePixelRatio; - Thumbnail({ + const Thumbnail({ Key key, @required this.entry, @required this.extent, diff --git a/lib/thumbnail_collection.dart b/lib/thumbnail_collection.dart index c7be8a0cb..ce9b85501 100644 --- a/lib/thumbnail_collection.dart +++ b/lib/thumbnail_collection.dart @@ -14,13 +14,12 @@ class ThumbnailCollection extends StatelessWidget { final Map> sections; final ScrollController scrollController = ScrollController(); - ThumbnailCollection(this.entries) : sections = groupBy(entries, ImageEntry.getDayTaken); + ThumbnailCollection({Key key, this.entries}) + : sections = groupBy(entries, ImageEntry.getDayTaken), + super(key: key); @override Widget build(BuildContext context) { - var columnCount = 4; - var mediaQuery = MediaQuery.of(context); - return DraggableScrollbar.arrows( labelTextBuilder: (double offset) => Text( "${offset ~/ 1}", @@ -34,34 +33,59 @@ class ThumbnailCollection extends StatelessWidget { title: Text('Aves - All'), floating: true, ), - ...sections.keys.map((sectionKey) => SliverStickyHeader( - header: DaySectionHeader(sectionKey), - sliver: SliverGrid( - delegate: SliverChildBuilderDelegate( - (context, index) { - var sectionEntries = sections[sectionKey]; - if (index >= sectionEntries.length) return null; - var entry = sectionEntries[index]; - return GestureDetector( - onTap: () => _showFullscreen(context, entry), - child: Thumbnail( - entry: entry, - extent: mediaQuery.size.width / columnCount, - devicePixelRatio: mediaQuery.devicePixelRatio, - ), - ); - }, - childCount: sections[sectionKey].length, - ), - gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( - crossAxisCount: columnCount, - ), - ), - )) + ...sections.keys.map((sectionKey) => SectionSliver( + entries: entries, + sections: sections, + sectionKey: sectionKey, + )), ], ), ); } +} + +class SectionSliver extends StatelessWidget { + final List entries; + final Map> sections; + final DateTime sectionKey; + + const SectionSliver({ + Key key, + this.entries, + this.sections, + this.sectionKey, + }) : super(key: key); + + @override + Widget build(BuildContext context) { + var columnCount = 4; + var mediaQuery = MediaQuery.of(context); + + return SliverStickyHeader( + header: DaySectionHeader(date: sectionKey), + sliver: SliverGrid( + delegate: SliverChildBuilderDelegate( + (context, index) { + var sectionEntries = sections[sectionKey]; + if (index >= sectionEntries.length) return null; + var entry = sectionEntries[index]; + return GestureDetector( + onTap: () => _showFullscreen(context, entry), + child: Thumbnail( + entry: entry, + extent: mediaQuery.size.width / columnCount, + devicePixelRatio: mediaQuery.devicePixelRatio, + ), + ); + }, + childCount: sections[sectionKey].length, + ), + gridDelegate: SliverGridDelegateWithFixedCrossAxisCount( + crossAxisCount: columnCount, + ), + ), + ); + } Future _showFullscreen(BuildContext context, Map entry) { return Navigator.push( @@ -79,6 +103,10 @@ class ThumbnailCollection extends StatelessWidget { class DaySectionHeader extends StatelessWidget { final String text; + DaySectionHeader({Key key, DateTime date}) + : text = formatDate(date), + super(key: key); + static DateFormat md = DateFormat.MMMMd(); static DateFormat ymd = DateFormat.yMMMMd(); @@ -88,25 +116,23 @@ class DaySectionHeader extends StatelessWidget { return ymd.format(date); } - DaySectionHeader(DateTime date) : text = formatDate(date); - @override Widget build(BuildContext context) { - return SectionHeader(text); + return SectionHeader(text: text); } } class SectionHeader extends StatelessWidget { - final String primaryText; + final String text; - SectionHeader(this.primaryText); + const SectionHeader({Key key, this.text}) : super(key: key); @override Widget build(BuildContext context) { return Container( padding: EdgeInsets.all(16), child: OutlinedText( - primaryText, + text, style: TextStyle( color: Colors.grey[200], fontSize: 20,