LinkedText (Linkify) (#125927)

New LinkedText widget and TextLinker class for easily adding hyperlinks to text.
This commit is contained in:
Justin McCandless
2023-09-13 20:39:58 -07:00
committed by GitHub
parent ee9aef0130
commit 4db47db177
18 changed files with 2520 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/linked_text/linked_text.0.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('tapping a link shows a dialog with the tapped uri', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LinkedTextApp(),
);
final Finder textFinder = find.descendant(
of: find.byType(LinkedText),
matching: find.byType(RichText),
);
expect(textFinder, findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await tester.tapAt(tester.getCenter(textFinder));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('You tapped: https://www.flutter.dev'), findsOneWidget);
});
}

View File

@@ -0,0 +1,27 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/linked_text/linked_text.1.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('tapping a Twitter handle shows a dialog with the uri of the user', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LinkedTextApp(),
);
final Finder textFinder = find.descendant(
of: find.byType(LinkedText),
matching: find.byType(RichText),
);
expect(textFinder, findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await tester.tapAt(tester.getCenter(textFinder));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('You tapped: https://www.twitter.com/FlutterDev'), findsOneWidget);
});
}

View File

@@ -0,0 +1,27 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/linked_text/linked_text.2.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('can tap links generated from TextSpans', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LinkedTextApp(),
);
final Finder textFinder = find.descendant(
of: find.byType(LinkedText),
matching: find.byType(RichText),
);
expect(textFinder, findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await tester.tapAt(tester.getCenter(textFinder));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('You tapped: https://www.flutter.dev'), findsOneWidget);
});
}

View File

@@ -0,0 +1,36 @@
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/material.dart';
import 'package:flutter_api_samples/widgets/linked_text/linked_text.3.dart' as example;
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('can tap different link types with different results', (WidgetTester tester) async {
await tester.pumpWidget(
const example.LinkedTextApp(),
);
final Finder textFinder = find.descendant(
of: find.byType(SelectionArea),
matching: find.byType(Text),
);
expect(textFinder, findsOneWidget);
expect(find.byType(AlertDialog), findsNothing);
await tester.tapAt(tester.getTopLeft(textFinder));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('You tapped: https://www.twitter.com/FlutterDev'), findsOneWidget);
await tester.tapAt(tester.getTopLeft(find.byType(Scaffold)));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsNothing);
await tester.tapAt(tester.getCenter(textFinder));
await tester.pumpAndSettle();
expect(find.byType(AlertDialog), findsOneWidget);
expect(find.text('You tapped: www.flutter.dev'), findsOneWidget);
});
}