textspan 转连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import 'package:flutter/material.dart' ; import 'package:flutter/gestures.dart' ; import 'package:url_launcher/url_launcher.dart' ; import 'package:flutter_webview_plugin/flutter_webview_plugin.dart' ; class TestApp extends StatelessWidget { @override Widget build(BuildContext context) { / / TODO: implement build return Scaffold( appBar: AppBar( title: Text( 'test' ), ), body: Center(child:Container( child: RichText( text: TextSpan( children: [ TextSpan( text: 'just test' , style: TextStyle(color: Colors.black), recognizer: TapGestureRecognizer()..onTap = () = >Navigator.of(context).push(MaterialPageRoute(builder: (_){ return WebTest(); })), ), ], ), ), )), floatingActionButton: FloatingActionButton( onPressed: (){ Navigator.of(context).pushNamed( '/web' ); }, ), ); } } class WebTest extends StatelessWidget { / / WebTest({this.url}); final String url = 'http://www.v2ex.com' ; @override Widget build(BuildContext context) { print ( 'press here' ); return WebviewScaffold( appBar: AppBar(title: Text( 'test' ),), url: url, ); } } class _LinkTextSpan extends TextSpan { / / Beware! / / / / This class is only safe because the TapGestureRecognizer is not / / given a deadline and therefore never allocates any resources. / / / / In any other situation - - setting a deadline, using any of the less trivial / / recognizers, etc - - you would have to manage the gesture recognizer's / / lifetime and call dispose() when the TextSpan was no longer being rendered. / / / / Since TextSpan itself is @immutable, this means that you would have to / / manage the recognizer from outside the TextSpan, e.g. in the State of a / / stateful widget that then hands the recognizer to the TextSpan. / / example: / / _LinkTextSpan( / / style: linkStyle, / / url: 'https://goo.gl/iv1p4G' , / / text: 'flutter github repo' , / / ), _LinkTextSpan({ TextStyle style, String url, String text }) : super ( style: style, / / text: str ?? url, text:text, recognizer: TapGestureRecognizer()..onTap = () { print ( 'tapped' ); return WebviewScaffold( url:url, appBar: AppBar( title: Text(text, style:TextStyle(color: Colors.red)), ), ); } ); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合终身会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步