Use CustomScrollView

This commit is contained in:
2023-09-04 19:19:24 +08:00
parent e1233f35bd
commit 49239869ff
3 changed files with 60 additions and 20 deletions

View File

@@ -1,12 +1,15 @@
import 'package:eh_downloader_flutter/globals.dart';
import 'package:flutter/material.dart';
import '../api/file.dart';
import '../api/gallery.dart';
import 'gallery_basic_info.dart';
import 'gallery_info_desktop.dart';
import 'thumbnail_gridview.dart';
class GalleryInfo extends StatefulWidget {
const GalleryInfo(this.gData, {Key? key}) : super(key: key);
const GalleryInfo(this.gData, {Key? key, this.files}) : super(key: key);
final GalleryData gData;
final EhFiles? files;
@override
State<GalleryInfo> createState() => _GalleryInfo();
@@ -26,23 +29,29 @@ class _GalleryInfo extends State<GalleryInfo> {
@override
Widget build(BuildContext context) {
bool useMobile = MediaQuery.of(context).size.width <= 810;
return LayoutBuilder(builder: (context, constraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(minHeight: constraints.maxHeight),
child: useMobile
? Column(
children: [
GalleryBasicInfo(
widget.gData.meta, widget.gData.pages.first),
],
)
: Column(
children: [
GalleryInfoDesktop(widget.gData),
],
)));
});
final firstPage = widget.gData.pages.first;
final int? firstFileId = widget.files != null
? widget.files!.files[firstPage.token]!.first.id
: null;
return CustomScrollView(
slivers: [
useMobile
? SliverList(
delegate: SliverChildListDelegate([
GalleryBasicInfo(widget.gData.meta, firstPage,
fileId: firstFileId),
]),
)
: SliverList(
delegate: SliverChildListDelegate([
GalleryInfoDesktop(widget.gData, fileId: firstFileId),
]),
),
ThumbnailGridView(widget.gData.pages,
const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 5),
files: widget.files),
],
);
}
@override

View File

@@ -0,0 +1,25 @@
import 'package:flutter/material.dart';
import '../api/file.dart';
import '../api/gallery.dart';
import 'thumbnail.dart';
class ThumbnailGridView extends StatelessWidget {
const ThumbnailGridView(this.pages, this.gridDelegate, {Key? key, this.files})
: super(key: key);
final List<ExtendedPMeta> pages;
final EhFiles? files;
final SliverGridDelegate gridDelegate;
@override
Widget build(BuildContext context) {
return SliverGrid.builder(
gridDelegate: gridDelegate,
itemCount: pages.length,
itemBuilder: (context, index) {
final page = pages[index]!;
final fileId =
files != null ? files!.files[page.token]!.first.id : null;
return Thumbnail(page, fileId: fileId);
});
}
}

View File

@@ -1,3 +1,4 @@
import 'package:eh_downloader_flutter/api/file.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
@@ -25,6 +26,7 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
_GalleryPage();
int _gid = 0;
GalleryData? _data;
EhFiles? _files;
Object? _error;
bool _isLoading = false;
@@ -32,8 +34,12 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
try {
_isLoading = true;
final data = (await api.getGallery(_gid)).unwrap();
_data = data;
final fileData =
(await api.getFiles(data.pages.map((e) => e.token).toList()))
.unwrap();
setState(() {
_data = data;
_files = fileData;
_isLoading = false;
});
} catch (e) {
@@ -82,7 +88,7 @@ class _GalleryPage extends State<GalleryPage> with ThemeModeWidget {
body: isLoading
? const Center(child: CircularProgressIndicator())
: _data != null
? GalleryInfo(_data!)
? GalleryInfo(_data!, files: _files)
: Center(
child: Text("Error: $_error"),
));