【问题标题】:Flutter: Clickable Selectable Text, control over cursorFlutter:可点击的可选择文本,控制光标
【发布时间】:2021-10-02 15:04:49
【问题描述】:

我想要一个可点击的可选择文本,在这种情况下,我使用 Flutter Web 和 html 库,我有一个可点击的电话号码,它允许用户从浏览器中选择一个应用程序来打电话。

我的问题是,当悬停在此文本上时,光标是可选文本中的“文本”,但我希望光标变为“指针”/“链接选择”。换句话说,我希望它能够像默认的“真实”HTML 那样工作。


class Example extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
            SelectableText(
              'Phone us:',
              style: TextStyle(fontSize: 24),
            ),
        SelectableText(
            'Tel: +123 1231 231',
            style: TextStyle(fontSize: 20),
            onTap: ()=>{html.window.location.href = "tel:+1231231231"},
          ),
      ],
    );
  }
}

我尝试将可选文本包装在 MouseRegion() 中,但这也不起作用。

class Example extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
            SelectableText(
              'Phone us:',
              style: TextStyle(fontSize: 24),
            ),

        MouseRegion(
          cursor: SystemMouseCursors.click,
          child: SelectableText(
            'Tel: +123 1231 231',
            style: TextStyle(fontSize: 20),
            onTap: ()=>{html.window.location.href = "tel:+1231231231"},
          ),
        ),

      ],
    );
  }
}

【问题讨论】:

    标签: html flutter mouse-cursor


    【解决方案1】:

    试试SelectableText.richurl_launcher

    示例代码

    import 'package:flutter/gestures.dart';
    import 'package:flutter/material.dart';
    import 'package:url_launcher/url_launcher.dart';
    
    class ClickAbleText extends StatelessWidget {
      const ClickAbleText({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: SelectableText.rich(
              TextSpan(
                text: 'Phone us:',
                style: TextStyle(
                  fontSize: 24,
                ),
                children: [
                  TextSpan(
                    text: '+123 1231 231',
                    style: TextStyle(fontSize: 20),
                    mouseCursor: SystemMouseCursors.click,
                    recognizer: TapGestureRecognizer()
                      ..onTap = () async {
                        final _url = "tel:+1 555 010 999";
                        await canLaunch(_url)
                            ? await launch(_url)
                            : throw 'Could not launch $_url';
                      },
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    
    
    

    【讨论】:

    • 您好 Yeasin,感谢您的回复。那种作品!问题在于,一旦文本被“点击”,并且您尝试再次将鼠标悬停在文本上,它就会“卡住”为“文本”光标,您必须选择其他一些文本才能使其恢复为指针悬停时。也不需要 url_launcher,dart HTML 包可以正常工作:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 2021-10-12
    • 2012-01-24
    • 2016-04-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多