textspan 转连接

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)),
          ),
        );
      }
  );
}

  

posted @ 2019-04-08 11:04  CrossPython  阅读(336)  评论(0编辑  收藏  举报