Skip to content

Commit

Permalink
chore: add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Juliotati committed Aug 14, 2024
1 parent cddb970 commit f446a99
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Import it in your Dart code:
import 'package:link_target/link_target.dart';
```

- Make sure to wrap your `home` widget with `LinkTargetRegion`
- Make sure to wrap your material `home` widget with `LinkTargetRegion`

## Code sample

Expand Down
2 changes: 1 addition & 1 deletion lib/link_target.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library link_target;

export 'src/detector.dart';
export 'src/wrapper.dart';
export 'src/region.dart';
9 changes: 9 additions & 0 deletions lib/src/detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import 'package:provider/provider.dart';

@immutable
final class LinkTargetDetector extends StatefulWidget {
/// Creates a widget that detects when the mouse hovers over the child.
///
/// The [child] argument must not be null.
/// The [target] argument must not be null.
const LinkTargetDetector({
required this.child,
required this.target,
super.key,
});

/// The widget below [LinkTargetDetector] in the tree.
///
/// This would usually be your apps' common [GestureDetector] or [InkWell].
final Widget child;

/// The target URL should by [LinkTargetRegion] on hover.
final String target;

@override
Expand Down
18 changes: 12 additions & 6 deletions lib/src/provider.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
import 'dart:developer';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// Manages and provides link target displayed by [LinkTargetRegion].
final class LinkTargetProvider extends ChangeNotifier {
String _linkTarget = '';

/// Returns true if the [linkTarget] is not empty. Meaning there is an actual
/// target "URL" that has been hovered on.
bool get hasTarget => _linkTarget.isNotEmpty;

/// The target "URL" shown on the page by [LinkTargetRegion]
String get linkTarget => _linkTarget;

/// Updates the target "URL" shown on the page by [LinkTargetRegion]
void onHover(String value) {
_linkTarget = value;
log(name: 'LinkTargetProvider', 'onHover: $_linkTarget');
_linkTarget = value.trim();
notifyListeners();
}

void onExit(String linkTarget) {
if (_linkTarget == linkTarget) {
/// Removes the target "URL" shown on the page by [LinkTargetRegion].
///
/// [linkTarget] should not be cleared if [target] happens to be from it
/// because the user has already entered a new [LinkTargetDetector].
void onExit(String target) {
if (_linkTarget == target) {
_linkTarget = '';
}
notifyListeners();
Expand Down
13 changes: 9 additions & 4 deletions lib/src/wrapper.dart → lib/src/region.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,16 @@ import 'package:provider/provider.dart';

@immutable
final class LinkTargetRegion extends StatelessWidget {
const LinkTargetRegion({
required this.child,
super.key,
});
/// Displays a target URL preview when a mouse hover is detected on web by the
/// widget wrapped in [LinkTargetDetector].
///
/// The [child] argument must not be null.
const LinkTargetRegion({required this.child, super.key});

/// The widget below [LinkTargetRegion] in the tree.
///
/// This should be directly your apps [MaterialApp] as the child of `home` or
/// `builder` if in use for the best result.
final Widget child;

@override
Expand Down
6 changes: 6 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ description: "Adds hyperlink preview behaviour on hover found on browsers."
version: 0.0.1
homepage: https://github.com/juliotati/link_target

issue_tracker: https://github.com/juliotati/link_targetissues
topics:
- web
- hyperlink
- link-preview

environment:
sdk: ^3.5.0
flutter: ">=1.17.0"
Expand Down

0 comments on commit f446a99

Please sign in to comment.