Skip to content

Commit

Permalink
fix(scope_chip): šŸ› show tooltip only when isInteractive (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
albertms10 authored Feb 2, 2024
1 parent a59089b commit 81517bb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
59 changes: 59 additions & 0 deletions lib/src/widgets/conditional_widget_wrap.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
39 changes: 23 additions & 16 deletions lib/src/widgets/scope_chip.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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,
),
),
),
Expand Down

0 comments on commit 81517bb

Please sign in to comment.