Justin McCandless 4db47db177
LinkedText (Linkify) (#125927)
New LinkedText widget and TextLinker class for easily adding hyperlinks to text.
2023-09-13 20:39:58 -07:00

93 lines
2.5 KiB
Dart

// 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';
// This example demonstrates highlighting URLs in a TextSpan tree instead of a
// flat String.
void main() {
runApp(const LinkedTextApp());
}
class LinkedTextApp extends StatelessWidget {
const LinkedTextApp({
super.key,
});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter LinkedText.spans Demo'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({
super.key,
required this.title,
});
final String title;
void _onTapUri (BuildContext context, Uri uri) {
// A package like url_launcher would be useful for actually opening the URL
// here instead of just showing a dialog.
Navigator.of(context).push(
DialogRoute<void>(
context: context,
builder: (BuildContext context) => AlertDialog(title: Text('You tapped: $uri')),
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
body: Center(
child: Builder(
builder: (BuildContext context) {
return SelectionArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
LinkedText(
onTapUri: (Uri uri) => _onTapUri(context, uri),
spans: <InlineSpan>[
TextSpan(
text: 'Check out https://www.',
style: DefaultTextStyle.of(context).style,
children: const <InlineSpan>[
TextSpan(
style: TextStyle(
fontWeight: FontWeight.w800,
),
text: 'flutter',
),
],
),
TextSpan(
text: '.dev!',
style: DefaultTextStyle.of(context).style,
),
],
),
],
),
);
},
),
),
);
}
}