From 209bb70f03c8fdb41dc9ff63310cfabd7fe68e79 Mon Sep 17 00:00:00 2001 From: Thibault Deckers Date: Tue, 2 Jun 2020 14:39:54 +0900 Subject: [PATCH] fixed filter chip hero to filter bar --- lib/widgets/album/filter_bar.dart | 2 ++ lib/widgets/common/aves_filter_chip.dart | 32 ++++++++++++++++-------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/lib/widgets/album/filter_bar.dart b/lib/widgets/album/filter_bar.dart index d64ca99a2..d2d539a23 100644 --- a/lib/widgets/album/filter_bar.dart +++ b/lib/widgets/album/filter_bar.dart @@ -33,8 +33,10 @@ class FilterBar extends StatelessWidget implements PreferredSizeWidget { final filter = filters[index]; return Center( child: AvesFilterChip( + key: ValueKey(filter), filter: filter, removable: true, + heroType: HeroType.always, onPressed: collection.removeFilter, ), ); diff --git a/lib/widgets/common/aves_filter_chip.dart b/lib/widgets/common/aves_filter_chip.dart index 7fa5ec9ca..8d002b129 100644 --- a/lib/widgets/common/aves_filter_chip.dart +++ b/lib/widgets/common/aves_filter_chip.dart @@ -4,12 +4,15 @@ import 'package:flutter/material.dart'; typedef FilterCallback = void Function(CollectionFilter filter); +enum HeroType { always, onTap, never } + class AvesFilterChip extends StatefulWidget { final CollectionFilter filter; final bool removable; final bool showGenericIcon; final Decoration decoration; final Widget details; + final HeroType heroType; final FilterCallback onPressed; static final BorderRadius borderRadius = BorderRadius.circular(32); @@ -27,6 +30,7 @@ class AvesFilterChip extends StatefulWidget { this.showGenericIcon = true, this.decoration, this.details, + this.heroType = HeroType.onTap, @required this.onPressed, }) : super(key: key); @@ -36,6 +40,7 @@ class AvesFilterChip extends StatefulWidget { class _AvesFilterChipState extends State { Future _colorFuture; + bool _tapped; CollectionFilter get filter => widget.filter; @@ -43,6 +48,7 @@ class _AvesFilterChipState extends State { void initState() { super.initState(); _initColorLoader(); + _tapped = false; } @override @@ -50,6 +56,7 @@ class _AvesFilterChipState extends State { super.didUpdateWidget(oldWidget); if (oldWidget.filter != filter) { _initColorLoader(); + _tapped = false; } } @@ -119,7 +126,7 @@ class _AvesFilterChipState extends State { final borderRadius = AvesFilterChip.borderRadius; - final chip = Container( + Widget chip = Container( constraints: const BoxConstraints( minWidth: AvesFilterChip.minChipWidth, maxWidth: AvesFilterChip.maxChipWidth, @@ -135,7 +142,12 @@ class _AvesFilterChipState extends State { borderRadius: borderRadius, ), child: InkWell( - onTap: widget.onPressed != null ? () => widget.onPressed(filter) : null, + onTap: widget.onPressed != null + ? () { + WidgetsBinding.instance.addPostFrameCallback((_) => widget.onPressed(filter)); + setState(() => _tapped = true); + } + : null, borderRadius: borderRadius, child: FutureBuilder( future: _colorFuture, @@ -162,14 +174,12 @@ class _AvesFilterChipState extends State { ), ); - // TODO TLAD only hero between `FilterBar` and chips that are tapped - return Hero( - tag: filter, - flightShuttleBuilder: (flight, animation, direction, fromHeroContext, toHeroContext) { - final toHero = toHeroContext.widget as Hero; - return Center(child: toHero.child); - }, - child: chip, - ); + if (widget.heroType == HeroType.always || widget.heroType == HeroType.onTap && _tapped) { + chip = Hero( + tag: filter, + child: chip, + ); + } + return chip; } }