kaboc/flutter_custom_text

onTap and hoverStyle does not work in rare conditions

kaboc opened this issue · 1 comments

kaboc commented

This appears to be an edge case that only occurs in the following conditions:

  • A single matcher matches the entire text.
  • Neither style nor matchStyle is set.

Reproducible code

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: CustomText(
            'abc@example.com',
            definitions: [
              TextDefinition(
                matcher: const EmailMatcher(),
                hoverStyle: TextStyle(color: Colors.blue),
                onTap: (_) => print('Tapped'),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Expected behaviour

  • The email address is shown in blue while the mouse pointer hovers over it.
  • "Tapped" is printed in the console when the email address is tapped.

Actual behaviour

  • The text only sits on the screen like the normal Text widget, with no colour change and no output to the console.
kaboc commented

return Text.rich(
TextSpan(
children: elements.isEmpty
? [TextSpan(text: widget.text)]
: elements.asMap().entries.map((entry) {

When parsing is done and a single matcher has matched the entire text, the inner TextSpan is replaced but still with the same text. It seems that the TextSpan is considered to be unchanged because of the same text and therefore the added parameters are not used.

While the text is still being parsed:

Text.rich(
  TextSpan(
    children: [
      TextSpan(
        text: widget.text,
      ),
    ] ,
  ),
)

After the parsing is done:

Text.rich(
  TextSpan(
    children: [
      TextSpan(
        text: widget.text,
        hoverStyle: xxxxxx,
        onTap: xxxxxx,
      ),
    ] ,
  ),
)

Although I'm not sure exactly why just setting style or matchStyle prevents the issue, probably it somehow makes Flutter/Dart aware that the TextSpan has been replaced and causes hoverStyle and onTap to become active.