link_button.dart
1.53 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
import 'package:flutter/material.dart';
import 'markdown_config.dart';
class LinkButton extends StatefulWidget {
final String text;
final VoidCallback? onPressed;
final TextStyle? textStyle;
final String? url;
final GptMarkdownConfig config;
final Color color;
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(() {});
}
}