/
main.dart
118 lines (110 loc) · 3.47 KB
/
main.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:linkfy_text/linkfy_text.dart';
void main() {
runApp(MyApp());
}
final k = GlobalKey<ScaffoldMessengerState>();
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'linkfy_text Demo',
scaffoldMessengerKey: k,
debugShowCheckedModeBanner: false,
home: App(),
);
}
}
class App extends StatefulWidget {
@override
_AppState createState() => _AppState();
}
class _AppState extends State<App> {
final textStyle =
GoogleFonts.spaceGrotesk(fontSize: 16, fontWeight: FontWeight.w400);
final List<Map<String, dynamic>> texts = [
{
"text": "O1. This text contains a url: https://flutter.dev",
"types": null
},
{
"text": "O2. This text contains an email address: example@app.com",
"types": [LinkType.email]
},
{
"text": "O3. This text contains an #hashtag",
"types": [LinkType.hashTag]
},
{
"text": "O4. This text contains a @user tag",
"types": [LinkType.userTag]
},
{
"text": "O5. This text contains a phone number: (555) 444 2223",
"types": [LinkType.phone]
},
{
"text":
"O6. My website url: https://hello.com/GOOGLE search using: www.google.com, social media is facebook.com, additional link https://example.com/method?param=fullstackoverflow.dev, hashtag #trending & mention @dev.user +18009999999",
"types": [
LinkType.phone,
LinkType.email,
LinkType.url,
LinkType.hashTag,
LinkType.userTag,
]
},
];
void showSnackbar(String msg) {
k.currentState!.removeCurrentSnackBar();
k.currentState!.showSnackBar(SnackBar(
content: Text("$msg", style: textStyle),
behavior: SnackBarBehavior.floating,
));
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Padding(
padding: const EdgeInsets.all(27.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Center(
child: Text(
"LinkfyText Example",
style: textStyle.copyWith(
fontSize: 24,
),
),
),
for (var i = 0; i < texts.length; i++)
Padding(
padding: EdgeInsets.only(top: 14),
child: LinkifyText(
texts[i]['text'],
textAlign: TextAlign.left,
linkTypes: texts[i]['types'],
textStyle: textStyle,
customLinkStyles: {
LinkType.email: TextStyle(color: Colors.blue),
LinkType.hashTag: TextStyle(color: Colors.green),
LinkType.userTag: TextStyle(color: Colors.deepPurple),
LinkType.url: TextStyle(color: Colors.pink),
LinkType.phone: TextStyle(color: Colors.deepOrange),
},
linkStyle: textStyle.copyWith(
color: Colors.blue,
fontWeight: FontWeight.bold,
),
onTap: (link) => showSnackbar(
"link pressed: ${link.value!}. Type: ${link.type}"),
)),
],
),
),
);
}
}