How I add a hyperlink in the flutter Checkbox

Asked

Viewed 58 times

0

Good morning, all right, guys? I would like to add a hyperlink in the "privacy policy" section, and I’m not getting it. Is it possible to do this? If anyone can help me, I’d appreciate it.

import 'package:flutter/material.dart';

class GetCheckValue extends StatefulWidget {
  @override
  GetCheckValueState createState() {
    return new GetCheckValueState();
  }
}

class GetCheckValueState extends State<GetCheckValue> {
  bool _isChecked = false;
  String _currText = '';

  List<String> text = [
    "Declaro que li e aceito os termos da Política de Privacidade."

  ];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: Column(
            children: text
                .map((t) => CheckboxListTile(
                      title: Text(
                        t,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 17.0,
                        ),
                      ),
                      value: _isChecked,
                      onChanged: (val) {
                        setState(() {
                          _isChecked = val;
                          if (val == true) {
                            _currText = t;
                          }
                        });
                      },
                      activeColor: Colors.lightBlueAccent,
                      checkColor: Colors.white,
                    ))
                .toList(),
          ),
        ),
      ],
    );
  }
}

1 answer

2


You can create a hyperlink in a text using RichText and TextSpan, for example:

  @override
  Widget build(BuildContext context) {
    return RichText(
      text: TextSpan(
        text: 'Concordo com os ',
        children: <InlineSpan>[
          _buildLink(
            context: context,
            title: 'Termos de uso',
            onTap: () {
              print('Tap termos de uso');
            }
          ),
          const TextSpan(text: ' e a '),
          _buildLink(
            context: context,
            title: 'Política de Privacidade',
            onTap: () {
              print('Tap política de privacidade');
            }
          ),
        ],
      ),
    );
  }
  
  TextSpan _buildLink({
    BuildContext context,
    String title,
    VoidCallback onTap,
  }) {
    return TextSpan(
      text: title,
      style: Theme.of(context).textTheme.bodyText1.copyWith(
            fontSize: 14.0,
            color: Colors.pink,
          ),
      recognizer: TapGestureRecognizer()..onTap = onTap,
    );
  }

And within a TextSpan use the parameter recognizer to create the hyperlink, leaving a part of the text as a link.

See on practice at Dartpad.

inserir a descrição da imagem aqui

With this, adapt to your case. No need for a CheckboxListTile and yes only one Row with a Checkbox and the RichText.

  • Thank you very much! That way I managed to solve. Some things in the flutter are still obscure. It helped a lot!

  • Nothing, if it helped, could mark the answer as correct. : D

  • 1

    @Secchiz marks the answer as correct there buddy! Help the reputation of those who help you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.