link_button.dart
2.14 KB
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
import 'package:flutter/material.dart';
import 'markdown_config.dart';
/// A custom button widget that displays a link with customizable colors and styles.
///
/// The [LinkButton] widget is used to create a button that displays a link in the UI.
/// It takes a [text] parameter which is the text of the link,
/// a [config] parameter which is the configuration for the link,
/// a [color] parameter to set the color of the link,
class LinkButton extends StatefulWidget {
/// The text of the link.
final String text;
/// The callback function to be called when the link is pressed.
final VoidCallback? onPressed;
/// The style of the text.
final TextStyle? textStyle;
/// The URL of the link.
final String? url;
/// The configuration for the link.
final GptMarkdownConfig config;
/// The color of the link.
final Color color;
/// The color of the link when hovering.
final Color hoverColor;
const LinkButton({
super.key,
required this.text,
required this.config,
required this.color,
required this.hoverColor,
this.onPressed,
this.textStyle,
this.url,
});
@override
State<LinkButton> createState() => _LinkButtonState();
}
class _LinkButtonState extends State<LinkButton> {
bool _isHovering = false;
@override
Widget build(BuildContext context) {
var style = (widget.config.style ?? const TextStyle()).copyWith(
color: _isHovering ? widget.hoverColor : widget.color,
decoration: TextDecoration.underline,
decorationColor: _isHovering ? widget.hoverColor : widget.color,
);
return MouseRegion(
cursor: SystemMouseCursors.click,
onEnter: (_) => _handleHover(true),
onExit: (_) => _handleHover(false),
child: GestureDetector(
onTapDown: (_) => _handlePress(true),
onTapUp: (_) => _handlePress(false),
onTapCancel: () => _handlePress(false),
onTap: widget.onPressed,
child: widget.config.getRich(TextSpan(text: widget.text, style: style)),
),
);
}
void _handleHover(bool hover) {
setState(() {
_isHovering = hover;
});
}
void _handlePress(bool pressed) {
setState(() {});
}
}