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

86 lines
2.3 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 and linking Twitter handles.
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: MyHomePage(title: 'Flutter Link Twitter Handle Demo'),
);
}
}
class MyHomePage extends StatelessWidget {
MyHomePage({
super.key,
required this.title
});
final String title;
static const String _text = 'Please check out @FlutterDev on Twitter for the latest.';
void _handleTapTwitterHandle(BuildContext context, String linkText) {
final String handleWithoutAt = linkText.substring(1);
final String twitterUriString = 'https://www.twitter.com/$handleWithoutAt';
final Uri? uri = Uri.tryParse(twitterUriString);
if (uri == null) {
throw Exception('Failed to parse $twitterUriString.');
}
// 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')),
),
);
}
final RegExp _twitterHandleRegExp = RegExp(r'@[a-zA-Z0-9]{4,15}');
@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.regExp(
text: _text,
regExp: _twitterHandleRegExp,
onTap: (String twitterHandleString) => _handleTapTwitterHandle(context, twitterHandleString),
),
],
),
);
},
),
),
);
}
}