From 81517bbcb0bbcf512ff361b6980e718635c11e19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Ma=C3=B1osa?= Date: Sat, 3 Feb 2024 00:32:49 +0100 Subject: [PATCH] =?UTF-8?q?fix(scope=5Fchip):=20=F0=9F=90=9B=20show=20tool?= =?UTF-8?q?tip=20only=20when=20`isInteractive`=20(#45)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/src/widgets/conditional_widget_wrap.dart | 59 ++++++++++++++++++++ lib/src/widgets/scope_chip.dart | 39 +++++++------ 2 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 lib/src/widgets/conditional_widget_wrap.dart diff --git a/lib/src/widgets/conditional_widget_wrap.dart b/lib/src/widgets/conditional_widget_wrap.dart new file mode 100644 index 0000000..4bfe414 --- /dev/null +++ b/lib/src/widgets/conditional_widget_wrap.dart @@ -0,0 +1,59 @@ +import 'package:flutter/widgets.dart'; + +/// Conditionally wrap a subtree with a parent widget without breaking +/// the code tree. +/// +/// See [source Gist](https://gist.github.com/ltOgt/3771c824fc1c8811f5ec1a81a9a4937b). +/// +/// ___________ +/// Use: +/// +/// ```dart +/// return ConditionalParentWidget( +/// condition: shouldIncludeParent, +/// child: Widget1( +/// child: Widget2( +/// child: Widget3(), +/// ), +/// ), +/// conditionalBuilder: (Widget child) => SomeParentWidget(child: child), +///); +/// ``` +/// +/// ___________ +/// Instead of: +/// +/// ```dart +/// Widget child = Widget1( +/// child: Widget2( +/// child: Widget3(), +/// ), +/// ); +/// +/// return shouldIncludeParent ? SomeParentWidget(child: child) : child; +/// ``` +class ConditionalWidgetWrap extends StatelessWidget { + /// The subtree that should always be build. + final Widget child; + + /// The condition depending on which the subtree [child] + /// is wrapped with the parent. + final bool condition; + + /// Builds the parent with the subtree [child]. + final Widget Function(Widget) conditionalBuilder; + + const ConditionalWidgetWrap({ + required this.child, + required this.condition, + required this.conditionalBuilder, + super.key, + }); + + @override + Widget build(BuildContext context) { + if (condition) return conditionalBuilder(child); + + return child; + } +} diff --git a/lib/src/widgets/scope_chip.dart b/lib/src/widgets/scope_chip.dart index 42c171c..e1b0e3c 100644 --- a/lib/src/widgets/scope_chip.dart +++ b/lib/src/widgets/scope_chip.dart @@ -1,4 +1,5 @@ import 'package:el_meu_diec/model.dart'; +import 'package:el_meu_diec/src/widgets/conditional_widget_wrap.dart'; import 'package:flutter/material.dart'; class ScopeChip extends StatelessWidget { @@ -26,23 +27,29 @@ class ScopeChip extends StatelessWidget { borderRadius: BorderRadius.all(Radius.circular(4)), side: BorderSide(color: Color(0xFFE0E0E0)), ), - child: Tooltip( - message: 'Mostra l’abreviatura', - child: InkWell( - onTap: isInteractive ? () => _onTap(context) : null, - child: Padding( - padding: const EdgeInsetsDirectional.only( - start: 6, - end: 6, - top: 3, - bottom: 4, + child: ConditionalWidgetWrap( + condition: isInteractive, + conditionalBuilder: (child) { + return Tooltip( + message: 'Mostra l’abreviatura', + child: InkWell( + onTap: () => _onTap(context), + child: child, ), - child: Text( - scope.name, - style: const TextStyle( - color: Colors.black, - fontSize: 12, - ), + ); + }, + child: Padding( + padding: const EdgeInsetsDirectional.only( + start: 6, + end: 6, + top: 3, + bottom: 4, + ), + child: Text( + scope.name, + style: const TextStyle( + color: Colors.black, + fontSize: 12, ), ), ),