在文本小部件中向导航器添加链接

时间:2019-07-30 10:37:10

标签: flutter dart

我想在文本小部件中显示指向单独视图的链接

我尝试在其中添加两个文本小部件和一个扁平按钮,但未按预期显示

我希望它看起来如何

<p>This is a <a href="#">sample</a> text</p>

这是我尝试过的

Column(
  children: [
    Text('This is a '),
    FlatButton(
      onPress: (){
        Navigator.push(context, MaterialPageRoute(builder: (context)=>View()))
      }
      child: Text('sample')
    ),
    Text(' text')
  ]
)

1 个答案:

答案 0 :(得分:1)

TapGestureRecognizer _recognizer;

@override
void initState() {
  super.initState();
  _recognizer = TapGestureRecognizer()..onTap = (){
    print("tapped");
  };
}

@override
Widget build(BuildContext context) {
  return Scaffold(
    appBar: AppBar(),
    body: Center(
      child: RichText(
        text: TextSpan(
          children: <TextSpan>[
            TextSpan(text: 'This is a '),
            TextSpan(text: 'sample', style: TextStyle(fontWeight: FontWeight.bold), recognizer: _recognizer),
            TextSpan(text: ' text'),
          ],
        ),
      ),
    ),
  );
}