Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo committed Nov 10, 2022
0 parents commit 48b03a3
Show file tree
Hide file tree
Showing 20 changed files with 1,043 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Miscellaneous
*.class
*.log
*.pyc
*.swp
.DS_Store
.atom/
.buildlog/
.history
.svn/
migrate_working_dir/

# IntelliJ related
*.iml
*.ipr
*.iws
.idea/

# The .vscode folder contains launch configuration and tasks you configure in
# VS Code which you may wish to be included in version control, so this line
# is commented out by default.
#.vscode/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
/pubspec.lock
**/doc/api/
.dart_tool/
.packages
build/
10 changes: 10 additions & 0 deletions .metadata
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# This file tracks properties of this Flutter project.
# Used by Flutter tool to assess capabilities and perform upgrades etc.
#
# This file should be version controlled and should not be manually edited.

version:
revision: e99c9c7cd9f6c0b2f8ae6e3ebfd585239f5568f4
channel: stable

project_type: package
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 1.0.0

* Added: el_tooltip first release.
23 changes: 23 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
el_tooltip

MIT License

Copyright (c) 2019 Takashi Kawasaki

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<p align="center"><img src="https://raw.githubusercontent.com/marcelogil/el_tooltip/develop/images/header.png" width="700"/></p>
<h1 align="center">
ElTooltip - a smart positioned tooltip
</h1>


## Why el_tooltip?

- 📦 Add widget elements to your tooltip
- ↔️ Chose the prefered position for the tooltip to appear relative to the button
- ↩️ The position changes automagically if the desired one doesn't fit the screen
- ✅ No external dependencies
- ❤️ Customizable layout
- 🛡️ Null safety

## Getting Started

### 🍭 Installation

Add to your `pubspec.yaml`:

```yaml
dependencies:
el_tooltip: <last_version>
```
Import the library and call the Widget ElTooltip() with the required fields `trigger` and `content`:


```dart
import 'package:flutter/material.dart';
import 'package:el_tooltip/el_tooltip.dart';
void main() {
runApp(MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SafeArea(
child: Center(
child: ElTooltip(
trigger: Icon(Icons.info_outline),
content: Text('This is a tooltip'),
),
),
),
));
}
```

[**Full example**](https://github.com/marcelogil/el_tooltip/master/example/lib/main.dart)

### 🏷️ El Tooltip widget properties

| Properties | Required | Default | Description |
| ----------------------- | -------- | ------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| content | true | | What will appear inside the tooltip. |
| trigger | true | | Widget that represent the button to activate the tooltip (no click method required). |
| color | false | `Colors.white` | Background color of the bubble and the arrow. |
| distance | false | `10.0` | Space between the trigger button and the tooltip. |
| padding | false | `14.0` | Tooltip padding around the content widget. |
| position | false | `topCenter` | Desired position based on the Enum `ElTooltipPosition`. Can be `topStart`, `topCenter`, `topEnd`, `rightStart`, `rightCenter`,` rightEnd`, `bottomStart`, `bottomCenter`, `bottomEnd`, `leftStart`, `leftCenter`, `leftEnd`, |
| radius | false | `8.0` | Border radius of the tooltip. |
| showModal | false | `true` | Displays a fullscreen dark layer behind the tooltip. |
| timeout | false | `0` (only disappears on click) | How many seconds to wait for the tooltip to disappear. |

<p align="center"><img src="https://raw.githubusercontent.com/marcelogil/el_tooltip/develop/images/placement.png" width="700"/></p>
4 changes: 4 additions & 0 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
include: package:flutter_lints/flutter.yaml

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
Binary file added develop/images/header.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added develop/images/placement.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
213 changes: 213 additions & 0 deletions lib/el_tooltip.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
library el_tooltip;

import 'package:flutter/material.dart';
import 'el_tooltip_assets/arrow.dart';
import 'el_tooltip_assets/bubble.dart';
import 'el_tooltip_assets/element_box.dart';
import 'el_tooltip_assets/enum/el_tooltip_position.dart';
import 'el_tooltip_assets/modal.dart';
import 'el_tooltip_assets/position_manager.dart';
import 'el_tooltip_assets/tooltip_elements_display.dart';

export 'el_tooltip_assets/enum/el_tooltip_position.dart';

/// Widget that displays a tooltip
/// It takes a widget as the trigger and a widget as the content
class ElTooltip extends StatefulWidget {
final Widget content;
final Widget trigger;
final Color color;
final double distance;
final double padding;
final ElTooltipPosition position;
final double radius;
final bool showModal;
final int timeout;

const ElTooltip({
required this.content,
required this.trigger,
this.color = Colors.white,
this.distance = 10.0,
this.padding = 14.0,
this.position = ElTooltipPosition.topCenter,
this.radius = 8.0,
this.showModal = true,
this.timeout = 0,
super.key,
});
@override
State<ElTooltip> createState() => _ElTooltipState();
}

class _ElTooltipState extends State<ElTooltip> with WidgetsBindingObserver {
final GlobalKey _widgetKey = GlobalKey();
OverlayEntry? _overlayEntryHidden;
OverlayEntry? _overlayEntry;
ElementBox get _screenSize => _getScreenSize();
ElementBox get _triggerBox => _getTriggerSize();
ElementBox _overlayBox = ElementBox(h: 0.0, w: 0.0);
final ElementBox _arrowBox = ElementBox(h: 10.0, w: 16.0);

@override
void initState() {
super.initState();
WidgetsBinding.instance
.addPostFrameCallback((_) => _loadHiddenOverlay(context));
WidgetsBinding.instance.addObserver(this);
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeMetrics() {
_hideOverlay();
}

/// Measures the hidden tooltip after it's loaded with _loadHiddenOverlay(_)
void _getHiddenOverlaySize(context) {
RenderBox box = _widgetKey.currentContext?.findRenderObject() as RenderBox;
setState(() {
_overlayBox = ElementBox(
w: box.size.width,
h: box.size.height,
);
_overlayEntryHidden?.remove();
});
}

/// Loads the tooltip without opacity to measure the rendered size
void _loadHiddenOverlay(_) {
OverlayState? overlayStateHidden = Overlay.of(context);
_overlayEntryHidden = OverlayEntry(
builder: (context) {
WidgetsBinding.instance
.addPostFrameCallback((_) => _getHiddenOverlaySize(context));
return Opacity(
opacity: 0,
child: Center(
child: Bubble(
key: _widgetKey,
triggerBox: _triggerBox,
padding: widget.padding,
child: widget.content,
),
),
);
},
);
overlayStateHidden?.insert(_overlayEntryHidden!);
}

/// Measures the size of the trigger widget
ElementBox _getTriggerSize() {
final renderBox = context.findRenderObject() as RenderBox;
final offset = renderBox.localToGlobal(Offset.zero);
return ElementBox(
w: renderBox.size.width,
h: renderBox.size.height,
x: offset.dx,
y: offset.dy,
);
}

/// Measures the size of the screen to calculate possible overflow
ElementBox _getScreenSize() {
return ElementBox(
w: MediaQuery.of(context).size.width,
h: MediaQuery.of(context).size.height,
);
}

/// Loads the tooltip into view
void _showOverlay(BuildContext context) async {
OverlayState? overlayState = Overlay.of(context);

ToolTipElementsDisplay toolTipElementsDisplay = PositionManager(
arrowBox: _arrowBox,
overlayBox: _overlayBox,
triggerBox: _triggerBox,
screenSize: _screenSize,
distance: widget.distance,
radius: widget.radius,
).load(preferredPosition: widget.position);

_overlayEntry = OverlayEntry(
builder: (context) {
return Stack(
children: [
Modal(
color: Colors.black87,
opacity: 0.7,
visible: widget.showModal,
onTap: () {
_hideOverlay();
},
),
Positioned(
top: toolTipElementsDisplay.bubble.y,
left: toolTipElementsDisplay.bubble.x,
child: Bubble(
triggerBox: _triggerBox,
padding: widget.padding,
radius: toolTipElementsDisplay.radius,
color: widget.color,
child: widget.content,
),
),
Positioned(
top: toolTipElementsDisplay.arrow.y,
left: toolTipElementsDisplay.arrow.x,
child: Arrow(
color: widget.color,
position: toolTipElementsDisplay.position,
width: _arrowBox.w,
height: _arrowBox.h,
),
),
Positioned(
top: _triggerBox.y,
left: _triggerBox.x,
child: GestureDetector(
onTap: () {
_overlayEntry != null
? _hideOverlay()
: _showOverlay(context);
},
child: widget.trigger,
),
),
],
);
},
);

overlayState?.insert(_overlayEntry!);

if (widget.timeout > 0) {
await Future.delayed(Duration(seconds: widget.timeout))
.whenComplete(() => _hideOverlay());
}
}

void _hideOverlay() {
if (_overlayEntry != null) {
_overlayEntry?.remove();
_overlayEntry = null;
}
}

@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
_overlayEntry != null ? _hideOverlay() : _showOverlay(context);
},
child: widget.trigger,
);
}
}
Loading

0 comments on commit 48b03a3

Please sign in to comment.