Want to make a TextSpan ripple when I tap it? You can use the following code to have the ripple constrained to a specific section of the text:
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 | import 'package:flutter/material.dart'; import 'package:flutter/rendering.dart'; import 'dart:ui' show TextBox; import 'dart:math'; void main() { runApp(new MaterialApp( home: new Material( child: new Center( child: new Demo(), ), ), )); } class Demo extends StatelessWidget { final TextSelection textSelection = const TextSelection(baseOffset: 17, extentOffset: 21); final GlobalKey _textKey = new GlobalKey(); @override Widget build(context) => new Stack( children: <Widget>[ new RichText( key: _textKey, text: new TextSpan( text: 'HELLO THIS IS MY ', style: DefaultTextStyle.of(context).style, children: <TextSpan>[ new TextSpan( text: 'LONG', style: new TextStyle(fontWeight: FontWeight.bold)), new TextSpan(text: ' SENTENCE'), ], ), ), new Positioned.fill( child: new LayoutBuilder( builder: (context, _) => new Stack( children: <Widget>[ new Positioned.fromRect( rect: _getSelectionRect(), child: new InkWell( onTap: () => {}, // needed to show the ripple ), ), ], ), ), ), ], ); Rect _getSelectionRect() => (_textKey.currentContext.findRenderObject() as RenderParagraph) .getBoxesForSelection(textSelection) .fold( null, (Rect previous, TextBox textBox) => new Rect.fromLTRB( min(previous?.left ?? textBox.left, textBox.left), min(previous?.top ?? textBox.top, textBox.top), max(previous?.right ?? textBox.right, textBox.right), max(previous?.bottom ?? textBox.bottom, textBox.bottom), ), ) ?? Rect.zero; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.