fixed section layout when there is no header

This commit is contained in:
Thibault Deckers 2020-04-12 09:11:34 +09:00
parent b32a7747f6
commit 1478f1add3
2 changed files with 31 additions and 32 deletions

View file

@ -2,7 +2,6 @@ import 'dart:math';
import 'package:aves/labs/sliver_known_extent_list.dart';
import 'package:aves/model/collection_lens.dart';
import 'package:aves/utils/constants.dart';
import 'package:aves/widgets/album/collection_section.dart';
import 'package:aves/widgets/album/grid/header_generic.dart';
import 'package:flutter/material.dart';
@ -31,21 +30,12 @@ class CollectionListSliver extends StatelessWidget {
Widget build(BuildContext context) {
final sectionLayouts = <SectionLayout>[];
final sectionKeys = collection.sections.keys.toList();
final headerPadding = TitleSectionHeader.padding;
var currentIndex = 0, currentOffset = 0.0;
sectionKeys.forEach((sectionKey) {
final sectionEntryCount = collection.sections[sectionKey].length;
final sectionChildCount = 1 + (sectionEntryCount / columnCount).ceil();
var headerExtent = 0.0;
if (showHeader) {
// only compute height for album headers, as they're the only likely ones to split on multiple lines
if (sectionKey is String) {
final text = collection.source.getUniqueAlbumName(sectionKey);
headerExtent = SectionLayout.computeHeaderExtent(text, scrollableWidth - headerPadding.horizontal);
}
headerExtent = max(headerExtent, TitleSectionHeader.leadingDimension) + headerPadding.vertical;
}
final headerExtent = showHeader ? SectionHeader.computeHeaderExtent(collection, sectionKey, scrollableWidth) : 0.0;
final sectionFirstIndex = currentIndex;
currentIndex += sectionChildCount;
@ -66,16 +56,14 @@ class CollectionListSliver extends StatelessWidget {
tileExtent: tileExtent,
builder: (context, listIndex) {
listIndex -= sectionFirstIndex;
if (showHeader) {
if (listIndex == 0) {
return SectionHeader(
collection: collection,
sections: collection.sections,
sectionKey: sectionKey,
);
}
listIndex--;
if (listIndex == 0) {
return SectionHeader(
collection: collection,
sections: collection.sections,
sectionKey: sectionKey,
);
}
listIndex--;
final section = collection.sections[sectionKey];
final minEntryIndex = listIndex * columnCount;
@ -153,16 +141,4 @@ class SectionLayout {
scrollOffset -= minOffset + headerExtent;
return firstIndex + (scrollOffset < 0 ? 0 : (scrollOffset / tileExtent).ceil() - 1);
}
// TODO TLAD cache header extent computation?
static double computeHeaderExtent(String text, double scrollableWidth) {
final para = RenderParagraph(
TextSpan(
text: text,
style: Constants.titleTextStyle,
),
textDirection: TextDirection.ltr,
)..layout(BoxConstraints(maxWidth: scrollableWidth), parentUsesSize: true);
return para.getMaxIntrinsicHeight(scrollableWidth);
}
}

View file

@ -1,3 +1,5 @@
import 'dart:math';
import 'package:aves/model/collection_lens.dart';
import 'package:aves/model/image_entry.dart';
import 'package:aves/utils/constants.dart';
@ -6,6 +8,7 @@ import 'package:aves/widgets/album/grid/header_date.dart';
import 'package:aves/widgets/common/fx/outlined_text.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
class SectionHeader extends StatelessWidget {
final CollectionLens collection;
@ -59,6 +62,26 @@ class SectionHeader extends StatelessWidget {
albumName: collection.source.getUniqueAlbumName(folderPath),
);
}
// TODO TLAD cache header extent computation?
static double computeHeaderExtent(CollectionLens collection, dynamic sectionKey, double scrollableWidth) {
var headerExtent = 0.0;
if (sectionKey is String) {
// only compute height for album headers, as they're the only likely ones to split on multiple lines
final text = collection.source.getUniqueAlbumName(sectionKey);
final maxWidth = scrollableWidth - TitleSectionHeader.padding.horizontal;
final para = RenderParagraph(
TextSpan(
text: text,
style: Constants.titleTextStyle,
),
textDirection: TextDirection.ltr,
)..layout(BoxConstraints(maxWidth: maxWidth), parentUsesSize: true);
headerExtent = para.getMaxIntrinsicHeight(maxWidth);
}
headerExtent = max(headerExtent, TitleSectionHeader.leadingDimension) + TitleSectionHeader.padding.vertical;
return headerExtent;
}
}
class TitleSectionHeader extends StatelessWidget {