mirror of
https://github.com/flutter/flutter.git
synced 2026-02-20 02:29:02 +08:00
86 lines
2.3 KiB
Dart
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),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|