selection: toggle section selection by tapping header

This commit is contained in:
Thibault Deckers 2020-07-26 01:35:13 +09:00
parent bcc571fa84
commit 2ed7851d7d

View file

@ -129,50 +129,67 @@ class TitleSectionHeader extends StatelessWidget {
alignment: AlignmentDirectional.centerStart, alignment: AlignmentDirectional.centerStart,
padding: padding, padding: padding,
constraints: const BoxConstraints(minHeight: leadingDimension), constraints: const BoxConstraints(minHeight: leadingDimension),
child: Text.rich( child: GestureDetector(
TextSpan( onTap: () => _toggleSectionSelection(context),
children: [ child: Text.rich(
WidgetSpan( TextSpan(
alignment: widgetSpanAlignment, children: [
child: SectionSelectableLeading(
sectionKey: sectionKey,
browsingBuilder: leading != null
? (context) => Container(
padding: leadingPadding,
width: leadingDimension,
height: leadingDimension,
child: leading,
)
: null,
),
),
TextSpan(
text: title,
style: Constants.titleTextStyle,
),
if (trailing != null)
WidgetSpan( WidgetSpan(
alignment: widgetSpanAlignment, alignment: widgetSpanAlignment,
child: Container( child: SectionSelectableLeading(
padding: trailingPadding, sectionKey: sectionKey,
child: trailing, browsingBuilder: leading != null
? (context) => Container(
padding: leadingPadding,
width: leadingDimension,
height: leadingDimension,
child: leading,
)
: null,
onPressed: () => _toggleSectionSelection(context),
), ),
), ),
], TextSpan(
text: title,
style: Constants.titleTextStyle,
),
if (trailing != null)
WidgetSpan(
alignment: widgetSpanAlignment,
child: Container(
padding: trailingPadding,
child: trailing,
),
),
],
),
), ),
), ),
); );
} }
void _toggleSectionSelection(BuildContext context) {
final collection = Provider.of<CollectionLens>(context, listen: false);
final sectionEntries = collection.sections[sectionKey];
final selected = collection.isSelected(sectionEntries);
if (selected) {
collection.removeFromSelection(sectionEntries);
} else {
collection.addToSelection(sectionEntries);
}
}
} }
class SectionSelectableLeading extends StatelessWidget { class SectionSelectableLeading extends StatelessWidget {
final dynamic sectionKey; final dynamic sectionKey;
final WidgetBuilder browsingBuilder; final WidgetBuilder browsingBuilder;
final VoidCallback onPressed;
const SectionSelectableLeading({ const SectionSelectableLeading({
Key key, Key key,
@required this.sectionKey, @required this.sectionKey,
@required this.browsingBuilder, @required this.browsingBuilder,
@required this.onPressed,
}) : super(key: key); }) : super(key: key);
static const leadingDimension = TitleSectionHeader.leadingDimension; static const leadingDimension = TitleSectionHeader.leadingDimension;
@ -199,13 +216,7 @@ class SectionSelectableLeading extends StatelessWidget {
padding: const EdgeInsets.only(top: 1), padding: const EdgeInsets.only(top: 1),
alignment: Alignment.topLeft, alignment: Alignment.topLeft,
icon: Icon(selected ? AIcons.selected : AIcons.unselected), icon: Icon(selected ? AIcons.selected : AIcons.unselected),
onPressed: () { onPressed: onPressed,
if (selected) {
collection.removeFromSelection(sectionEntries);
} else {
collection.addToSelection(sectionEntries);
}
},
tooltip: selected ? 'Deselect section' : 'Select section', tooltip: selected ? 'Deselect section' : 'Select section',
constraints: const BoxConstraints( constraints: const BoxConstraints(
minHeight: leadingDimension, minHeight: leadingDimension,