Skip to content

Instantly share code, notes, and snippets.

@ha-yi
Last active June 9, 2020 13:46
Show Gist options
  • Save ha-yi/14b110feaa175ef450f4de163b7fd84e to your computer and use it in GitHub Desktop.
Save ha-yi/14b110feaa175ef450f4de163b7fd84e to your computer and use it in GitHub Desktop.
import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: MyWidget(
text: "Halo ini adalah #sample text yang hash tag kayak #gini bisa di click. #style nya juga diganti dengan #bold dan #color_blue.",
),
),
),
);
}
}
class MyWidget extends StatefulWidget {
final String text;
const MyWidget({Key key, this.text}) : super(key: key);
@override
_MyWidgetState createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> {
List<String> texts = ["Hello", "#welcome", "to awesome", "#app", "."];
initState() {
if(widget.text != null) {
texts = widget.text.split(" ");
}
super.initState();
}
TapGestureRecognizer createClicker(String data) {
if (data.startsWith("#")) {
TapGestureRecognizer recognizer = TapGestureRecognizer();
recognizer.onTap = () {
print("clicked $data");
};
return recognizer;
}
return null;
}
InlineSpan buildSpan(String data) {
return TextSpan(
text: data + " ",
style: TextStyle(
color: data.startsWith("#") ? Colors.blue : Colors.white,
fontSize: 16,
fontWeight: data.startsWith("#") ? FontWeight.bold : FontWeight.normal,
),
recognizer: createClicker(data),
);
}
@override
Widget build(BuildContext context) {
return RichText(
text: TextSpan(
text: "",
style: TextStyle(color: Colors.white, fontSize: 16),
children: texts.map((e) => buildSpan(e)).toList(),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment