From 404d1d7cf74e3b0457cad3b39aa52f1cb0ca85f8 Mon Sep 17 00:00:00 2001 From: lukas-heiligenbrunner Date: Thu, 25 Aug 2022 22:20:25 +0200 Subject: [PATCH] use previewgrid also on shufflescreen --- android/build.gradle | 2 +- lib/preview_grid.dart | 70 +++++++++++++++++++++++-------------- lib/preview_tile.dart | 2 +- lib/shufflescreen.dart | 79 +++++++++++------------------------------- 4 files changed, 67 insertions(+), 86 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 10ab8e5..9e8dea7 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.4.32' + ext.kotlin_version = '1.6.0' repositories { google() mavenCentral() diff --git a/lib/preview_grid.dart b/lib/preview_grid.dart index 2e1169a..722b2e2 100644 --- a/lib/preview_grid.dart +++ b/lib/preview_grid.dart @@ -6,9 +6,11 @@ import 'package:openmediacentermobile/platform.dart'; import 'package:openmediacentermobile/preview_tile.dart'; class PreviewGrid extends StatefulWidget { - const PreviewGrid({Key? key, required this.videoLoader}) : super(key: key); + const PreviewGrid({Key? key, required this.videoLoader, this.headerBuilder, this.footerBuilder}) : super(key: key); final Future> Function() videoLoader; + final Widget Function(_PreviewGridState state)? footerBuilder; + final Widget Function(_PreviewGridState state)? headerBuilder; @override State createState() => _PreviewGridState(); @@ -21,7 +23,13 @@ class _PreviewGridState extends State { @override void initState() { super.initState(); - _data = widget.videoLoader(); + loadData(); + } + + void loadData() { + setState(() { + _data = widget.videoLoader(); + }); } @override @@ -36,29 +44,37 @@ class _PreviewGridState extends State { } else if (snapshot.hasData) { return Stack( children: [ - MasonryGridView.count( - // every tile should be at max 330 pixels long... - crossAxisCount: isTV() ? width ~/ 200 : width ~/ 275, - // crossAxisCount: isTV() ? width ~/ 200 : width ~/ 330, - - mainAxisSpacing: 4, - crossAxisSpacing: 4, - padding: EdgeInsets.all(5), - itemBuilder: (context, index) { - return PreviewTile( - dta: snapshot.data![index], - onLongPress: (img) { - setState(() { - _previewImage = img; - }); - }, - onLongPressEnd: () { - setState(() { - _previewImage = null; - }); - }, - ); - }, + Column( + children: [ + if (widget.headerBuilder != null) widget.headerBuilder!(this), + Expanded( + child: MasonryGridView.count( + // every tile should be at max 330 pixels long... + crossAxisCount: isTV() ? width ~/ 200 : width ~/ 275, + // crossAxisCount: isTV() ? width ~/ 200 : width ~/ 330, + itemCount: snapshot.data!.length, + mainAxisSpacing: 4, + crossAxisSpacing: 4, + padding: EdgeInsets.all(5), + itemBuilder: (context, index) { + return PreviewTile( + dta: snapshot.data![index], + onLongPress: (img) { + setState(() { + _previewImage = img; + }); + }, + onLongPressEnd: () { + setState(() { + _previewImage = null; + }); + }, + ); + }, + ), + ), + if (widget.footerBuilder != null) widget.footerBuilder!(this), + ], ), if (_previewImage != null) ...[ BackdropFilter( @@ -72,7 +88,9 @@ class _PreviewGridState extends State { ), Container( child: Center( - child: Padding(padding: EdgeInsets.symmetric(horizontal: 50),child: ClipRRect(borderRadius: BorderRadius.circular(10.0), child: _previewImage!)), + child: Padding( + padding: EdgeInsets.symmetric(horizontal: 50), + child: ClipRRect(borderRadius: BorderRadius.circular(10.0), child: _previewImage!)), ), ), ], diff --git a/lib/preview_tile.dart b/lib/preview_tile.dart index d2f07a4..d3b6c47 100644 --- a/lib/preview_tile.dart +++ b/lib/preview_tile.dart @@ -91,7 +91,7 @@ class _PreviewTileState extends State { snapshot.data! ], ), - color: Color(0xFF6CE56F), + color: Color(0x6a94a6ff), ), Positioned.fill( child: Material( diff --git a/lib/shufflescreen.dart b/lib/shufflescreen.dart index 60816ba..7995a5d 100644 --- a/lib/shufflescreen.dart +++ b/lib/shufflescreen.dart @@ -2,7 +2,7 @@ import 'dart:convert'; import 'dart:math'; import 'package:flutter/material.dart'; -import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart'; +import 'package:openmediacentermobile/preview_grid.dart'; import 'preview_tile.dart'; import 'api/api.dart'; @@ -16,8 +16,6 @@ class ShuffleScreen extends StatefulWidget { } class _ShuffleScreenState extends State { - late Future> _data; - Future> loadData(int nr) async { final data = await API.query("video", "getRandomMovies", {'Number': nr, 'Seed': Random().nextInt(0x7fffffff)}); @@ -28,65 +26,30 @@ class _ShuffleScreenState extends State { return dta; } - @override - void initState() { - super.initState(); - - _data = Future.delayed(Duration.zero).then((_) { - double width = MediaQuery.of(context).size.width; - return loadData((isTV() ? width ~/ 200 : width ~/ 330) * 2); - }); - - } - @override Widget build(BuildContext context) { double width = MediaQuery.of(context).size.width; - return FutureBuilder>( - future: _data, // a previously-obtained Future or null - builder: (BuildContext context, AsyncSnapshot> snapshot) { - if (snapshot.hasError) { - return Text("Error"); - } else if (snapshot.hasData) { - return Column(children: [ - MasonryGridView.count( - shrinkWrap: true, - // every tile should be at max 330 pixels long... - crossAxisCount: isTV() ? width ~/ 200 : width ~/ 330, - mainAxisSpacing: 4, - crossAxisSpacing: 4, - padding: EdgeInsets.all(5), - itemCount: snapshot.data!.length, - itemBuilder: (context, index) { - return PreviewTile(dta: snapshot.data![index]); - }, - ), - const SizedBox( - height: 25, - ), - TextButton.icon( - onPressed: () { - setState(() { - _data = loadData((isTV() ? width ~/ 200 : width ~/ 330) * 2); - }); - }, - icon: const Icon(Icons.update), - label: const Text("Shuffle")) - ]); - } else { - return Column(children: const [ - SizedBox( - width: 60, - height: 60, - child: CircularProgressIndicator(), - ), - Padding( - padding: EdgeInsets.only(top: 16), - child: Text('Awaiting result...'), - ) - ]); - } + return PreviewGrid( + videoLoader: () { + return loadData((isTV() ? width ~/ 200 : width ~/ 330) * 2); }, + footerBuilder: (state) => Column( + children: [ + const SizedBox( + height: 25, + ), + TextButton.icon( + onPressed: () { + state.loadData(); + }, + icon: const Icon(Icons.update), + label: const Text("Shuffle"), + ), + const SizedBox( + height: 25, + ), + ], + ), ); } }