Сделайте определенные части текста кликабельными во флаттере - PullRequest
0 голосов
/ 28 сентября 2019

Я хочу сделать часть текста доступной для записи, чтобы я мог вызвать функцию для него.Также я хочу контролировать стиль надписываемого текста.В лучшем случае я также могу увеличить размер области, к которой можно прикоснуться, например, до 42 пикселей.

enter image description here

Я уже изучил flutter_linkify и linkify, но этоне то, что я хочу.Мне любопытно, есть ли уже пакет или даже встроенный в библиотеку флаттера.

Ответы [ 2 ]

1 голос
/ 28 сентября 2019

Используйте RichText с TextSpan и GestureRecognizer .С помощью GestureRecognizer вы можете обнаружить нажатие , двойное нажатие , длительное нажатие и т. Д.

Widget build(BuildContext context) {
    TextStyle defaultStyle = TextStyle(color: Colors.grey, fontSize: 20.0);
    TextStyle linkStyle = TextStyle(color: Colors.blue);
    return RichText(
      text: TextSpan(
        style: defaultStyle,
        children: <TextSpan>[
          TextSpan(text: 'By clicking Sign Up, you agree to our '),
          TextSpan(
              text: 'Terms of Service',
              style: linkStyle,
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print('Terms of Service"');
                }),
          TextSpan(text: ' and that you have read our '),
          TextSpan(
              text: 'Privacy Policy',
              style: linkStyle,
              recognizer: TapGestureRecognizer()
                ..onTap = () {
                  print('Privacy Policy"');
                }),
        ],
      ),
    );
  }

enter image description here

1 голос
/ 28 сентября 2019

Вы можете использовать RichText, чтобы объединить список TextSpan в один текст.

    return RichText(
      text: TextSpan(
        text: 'Hello ',
        style: DefaultTextStyle.of(context).style,
        children: <TextSpan>[
          TextSpan(
              text: 'world!',
              style: TextStyle(fontWeight: FontWeight.bold)),
          TextSpan(
              text: ' click here!',
              recognizer: TapGestureRecognizer()
                ..onTap = () => print('click')),
        ],
      ),
    );
...