-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
rich-text-construction-in-flutter.dart
150 lines (137 loc) Β· 3.44 KB
/
rich-text-construction-in-flutter.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// π¦ Twitter https://twitter.com/vandadnp
// π΅ LinkedIn https://linkedin.com/in/vandadnp
// π₯ YouTube https://youtube.com/c/vandadnp
// π Free Flutter Course https://linktr.ee/vandadnp
// π¦ 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// πΆ 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// π¦ 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
// π€ Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'dart:developer' as devtools show log;
void main() {
runApp(
const App(),
);
}
extension Log on Object {
void log() => devtools.log(toString());
}
class App extends StatelessWidget {
const App({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
themeMode: ThemeMode.dark,
darkTheme: ThemeData.dark(),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
@immutable
class BaseText {
final String text;
final TextStyle? style;
const BaseText({
required this.text,
this.style,
});
factory BaseText.plain({
required String text,
TextStyle? style = const TextStyle(),
}) =>
BaseText(
text: text,
style: style,
);
factory BaseText.link({
required String text,
required VoidCallback onTapped,
TextStyle? style = const TextStyle(
color: Colors.blue,
decoration: TextDecoration.underline,
),
}) =>
LinkText(
text: text,
onTapped: onTapped,
style: style,
);
}
@immutable
class LinkText extends BaseText {
final VoidCallback onTapped;
const LinkText({
required super.text,
required this.onTapped,
super.style,
});
}
class RichTextWidget extends StatelessWidget {
final TextStyle? styleForAll;
final Iterable<BaseText> texts;
const RichTextWidget({
super.key,
required this.texts,
this.styleForAll,
});
@override
Widget build(BuildContext context) {
return RichText(
text: TextSpan(
children: texts.map(
(baseText) {
if (baseText is LinkText) {
return TextSpan(
text: baseText.text,
style: styleForAll?.merge(baseText.style),
recognizer: TapGestureRecognizer()
..onTap = () {
baseText.onTapped();
},
);
} else {
return TextSpan(
text: baseText.text,
style: styleForAll?.merge(baseText.style),
);
}
},
).toList()),
);
}
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: RichTextWidget(
styleForAll: Theme.of(context).textTheme.headline2,
texts: [
BaseText.plain(
text: 'Welcome to my blog at ',
),
BaseText.link(
text: 'https://vandad.sh',
onTapped: () {
'Tapped'.log();
},
),
],
),
),
);
}
}