Flutter中所有已渲染词框的列表

问题描述 投票:0回答:1

如何获得包含我渲染的所有word的框的列表,我需要它来确定用户长按哪个word?换句话说,如何找到maxRange,以便可以在下面的代码中使用getBoxesForRange

我的代码:

var pin = Offset(0, 0);
    // To create a paragraph of text, we use ParagraphBuilder.
    final ui.ParagraphBuilder builder = ui.ParagraphBuilder(
      ui.ParagraphStyle(
        textDirection: ui.TextDirection.rtl,
        textAlign: ebookTextAlign,
        fontFamily: "Asan",
        fontSize: ebookMainTextSize,
      ),
    )..pushStyle(ui.TextStyle(color: const ui.Color(0xFF000000)));

    spans.forEach((spn) {
      if (spn.localName == "p") builder.addText("\n");
      builder.addText(spn.text + " ");
    });
    builder.pop();

    paragraph = builder.build()
      ..layout(ui.ParagraphConstraints(width: size.width));

    var list = new List<ExtendedTextBox>();
    var boxes = paragraph.getBoxesForRange(0, maxRange);
flutter text render paragraph custom-painting
1个回答
0
投票

要一段文字并长按该词,请尝试一下!

String paragraph = 'Hello user ! Have a good day !',selected='';
  List<String> words = [];

  @override
  void initState() {
    super.initState();
    words = paragraph.split(' ');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            padding: EdgeInsets.fromLTRB(5.0, 50, 5.0, 10.0),

          //For plain text

            child: Column(
              mainAxisSize:MainAxisSize.min,
              children: <Widget>[
                Text('Selected word :  '+selected,
                    style: TextStyle(
                        color:Colors.black,
                        fontWeight:FontWeight.bold,
                        fontSize:16.0),),
                SizedBox(height:20.0),
                RichText(
                    text: TextSpan(
                        style: TextStyle(
                        color:Colors.black,
                        fontWeight:FontWeight.bold,
                        fontSize:18.0),
                        /*default style*/
                        children: List.generate(words.length, (ind) {
                          return TextSpan(
                              text: words[ind]+' ',
                              style: TextStyle(/*modify as your wishs*/),
                              recognizer: LongPressGestureRecognizer()
                                ..onLongPress = () {
                                  setState((){
                                    selected = words[ind];
                                  });
                                });
                        }).toList())),
              ],
            )));
  }

Screenshot

© www.soinside.com 2019 - 2024. All rights reserved.