diff --git a/README.md b/README.md index 651dd81..23a541d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/link_target.dart b/lib/link_target.dart index 95238ca..a52fcd8 100644 --- a/lib/link_target.dart +++ b/lib/link_target.dart @@ -1,4 +1,4 @@ library link_target; export 'src/detector.dart'; -export 'src/wrapper.dart'; +export 'src/region.dart'; diff --git a/lib/src/detector.dart b/lib/src/detector.dart index 279bf71..3dbb72d 100644 --- a/lib/src/detector.dart +++ b/lib/src/detector.dart @@ -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 diff --git a/lib/src/provider.dart b/lib/src/provider.dart index cfc0862..75b871a 100644 --- a/lib/src/provider.dart +++ b/lib/src/provider.dart @@ -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(); diff --git a/lib/src/wrapper.dart b/lib/src/region.dart similarity index 83% rename from lib/src/wrapper.dart rename to lib/src/region.dart index 3c34c2a..4bf7dc6 100644 --- a/lib/src/wrapper.dart +++ b/lib/src/region.dart @@ -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 diff --git a/pubspec.yaml b/pubspec.yaml index 8dac6f2..c8edbba 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -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"