Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add tests and docs #1

Merged
merged 9 commits into from
Aug 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@Juliotati
47 changes: 47 additions & 0 deletions .github/workflows/ci_tests_pipeline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
name: Tests Pipeline

on:
pull_request:
types:
- opened
- synchronize
branches:
- main

jobs:
analyze-and-test:
runs-on: macos-latest
steps:
- name: setup-repo
uses: actions/checkout@v4

- name: setup-jdk
uses: actions/setup-java@v2
with:
java-version: '12.x'
distribution: 'zulu'

- uses: subosito/flutter-action@v2
with:
cache: true
channel: 'stable'
cache-key: 'flutter-macos-stable-3.24.0-x64'
cache-path: '${{ runner.tool_cache }}/flutter-macos-stable-3.24.0-x64-hash'
pub-cache-key: 'flutter-pub-macos-stable-3.24.0-x64-hash'
pub-cache-path: '${{ runner.tool_cache }}/flutter/stable-3.24.0-x64'
flutter-version: '3.24.0'

- name: load-dependencies
run: dart pub get

- name: analyse-link_target
run: dart analyze --fatal-warnings

- name: analyse-example
run: dart analyze example --fatal-warnings

- name: validate-format
run: dart format --line-length 80 --set-exit-if-changed .

- name: run-tests
run: flutter test
6 changes: 1 addition & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ migrate_working_dir/
*.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/
coverage/

# Flutter/Dart/Pub related
# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock.
Expand Down
7 changes: 7 additions & 0 deletions .run/RUN_WEB_AUTO.run.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="RUN_WEB_AUTO" type="FlutterRunConfigurationType" factoryName="Flutter">
<option name="additionalArgs" value="-d chrome --web-renderer auto" />
<option name="filePath" value="$PROJECT_DIR$/example/lib/main.dart" />
<method v="2" />
</configuration>
</component>
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
## 0.0.1

* feat: Target link preview for web.
* feat: Target link glance for web.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ and the Flutter guide for

# Link Target

[![Tests Pipeline](https://github.com/Juliotati/link_target/actions/workflows/ci_tests_pipeline.yml/badge.svg)](https://github.com/Juliotati/link_target/actions/workflows/ci_tests_pipeline.yml)
[![pub package](https://img.shields.io/pub/v/link_target.svg)](https://pub.dev/packages/link_target)
![Libraries.io dependency status for latest release](https://img.shields.io/librariesio/release/pub/link_target)
![License](https://img.shields.io/github/license/juliotati/link_target)
![License](https://img.shields.io/github/license/Juliotati/link_target)

Adds hyperlink preview behaviour on hover found on browsers.
Adds hyperlink glance behaviour on hover found on the bottom left of the browser page.

## Preview

https://github.com/user-attachments/assets/b58f76fb-0e4e-4af5-98ba-2c6d130ade30
https://github.com/user-attachments/assets/b4d9b2e4-33da-4501-b71d-c402b7d9d9d9

## Getting started

Expand Down Expand Up @@ -50,10 +50,7 @@ class MyAppInkwellOrGestureDetector extends StatelessWidget {
return LinkTargetDetector(
target: 'https://dart.dev/',
child: GestureDetector(
child: Text(
'Preview dart.dev',
style: Theme.of(context).textTheme.titleMedium,
),
child: Text('Glance dart.dev\'s link'),
),
);
}
Expand Down
73 changes: 40 additions & 33 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,54 @@ class LinkTargetExample extends StatelessWidget {

@override
Widget build(BuildContext context) {
final titleMedium = Theme.of(context).textTheme.titleMedium;
return MaterialApp(
title: 'Link Target Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: LinkTargetRegion(
child: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
child: Stack(
children: [
LinkTargetDetector(
target: 'https://github.com/flutter/flutter',
child: Text(
'Preview flutter repo',
style: Theme.of(context).textTheme.titleMedium,
),
),
LinkTargetDetector(
target: 'https://flutter.dev/development',
child: Text(
'Preview flutter.dev',
style: Theme.of(context).textTheme.titleMedium,
Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
LinkTargetDetector(
target: 'https://github.com/flutter/flutter',
child: Text(
'Glance flutter\'s repo link',
style: titleMedium,
),
),
LinkTargetDetector(
target: 'https://flutter.dev/development',
child: Text(
'Glance flutter.dev\'s link',
style: titleMedium,
),
),
LinkTargetDetector(
target: 'https://dart.dev/',
child: Text(
'Glance dart.dev\'s link',
style: titleMedium,
),
),
LinkTargetDetector(
target: 'https://www.youtube.com/@flutterdev',
child: Text(
'Glance Flutter YouTube\'s link',
style: titleMedium,
),
),
],
),
),
LinkTargetDetector(
target: 'https://dart.dev/',
child: Text(
'Preview dart.dev',
style: Theme.of(context).textTheme.titleMedium,
),
),
LinkTargetDetector(
target: 'https://www.youtube.com/@flutterdev',
child: Text(
'Preview Flutter YouTube',
style: Theme.of(context).textTheme.titleMedium,
),
const Positioned(
left: 10.0,
bottom: 10.0,
child: Icon(Icons.arrow_downward, size: 150.0),
),
],
),
Expand Down
9 changes: 9 additions & 0 deletions lib/src/detector.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:link_target/src/provider.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -31,6 +34,12 @@ class _LinkTargetDetectorState extends State<LinkTargetDetector> {

@override
Widget build(BuildContext context) {
if (!kIsWeb) {
if (!Platform.environment.containsKey('FLUTTER_TEST')) {
return widget.child;
}
}

return MouseRegion(
onHover: (_) {
if (_isHovered) return;
Expand Down
1 change: 0 additions & 1 deletion lib/src/provider.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

/// Manages and provides link target displayed by [LinkTargetRegion].
final class LinkTargetProvider extends ChangeNotifier {
Expand Down
14 changes: 11 additions & 3 deletions lib/src/region.dart
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:link_target/src/provider.dart';
import 'package:provider/provider.dart';

@immutable
final class LinkTargetRegion extends StatelessWidget {
/// Displays a target URL preview when a mouse hover is detected on web by the
/// widget wrapped in [LinkTargetDetector].
/// Displays the full URL 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});
Expand All @@ -19,7 +21,11 @@ final class LinkTargetRegion extends StatelessWidget {

@override
Widget build(BuildContext context) {
if (!kIsWeb) return child;
if (!kIsWeb) {
if (!Platform.environment.containsKey('FLUTTER_TEST')) {
return child;
}
}

return ListenableProvider(
create: (_) => LinkTargetProvider(),
Expand Down Expand Up @@ -53,6 +59,8 @@ final class LinkTargetRegion extends StatelessWidget {
),
child: Text(
provider.linkTarget,
maxLines: 1,
softWrap: false,
style: const TextStyle(
fontSize: 12,
color: Color.fromRGBO(240, 240, 240, 1.0),
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: link_target
description: "Adds hyperlink preview behaviour on hover found on browsers."
description: "Adds hyperlink glance behaviour on hover found on the bottom left of the browser page."
version: 0.0.1
homepage: https://github.com/juliotati/link_target

Expand All @@ -8,6 +8,7 @@ topics:
- web
- hyperlink
- link-preview
- link-glance

environment:
sdk: ^3.5.0
Expand Down
23 changes: 23 additions & 0 deletions test/src/detector_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import 'package:flutter/material.dart' show MouseRegion, Text, TextDirection;
import 'package:flutter_test/flutter_test.dart';
import 'package:link_target/src/detector.dart';

void main() {
testWidgets(
'LinkTargetDetector must layout MouseRegion and child',
(tester) async {
await tester.pumpWidget(
const LinkTargetDetector(
target: 'https://example.com',
child: Text(
'Flutter web rocks',
textDirection: TextDirection.ltr,
),
),
);

expect(find.text('Flutter web rocks'), findsOneWidget);
expect(find.byType(MouseRegion), findsOneWidget);
},
);
}
Loading
Loading