2

How to Add divider onEnter TextSpan and why backrgroundColor not full?

Widget richText() {
      return Positioned(
        top: h / 1.9,
        right: w / 4.9,
        child: Material(
          color: Colors.transparent,
          child: Container(
            padding: const EdgeInsets.only(left: 8, right: 0),
            margin: const EdgeInsets.all(0),
            color: Colors.transparent,
            width: wA / 1.365,
            height: hA / 2.3,
            child: RichText(
              softWrap: true,
              textScaleFactor: 1,
              textAlign: TextAlign.justify,
              textDirection: TextDirection.rtl,
              text: TextSpan(
                style: GoogleFonts.notoNaskhArabic(
                  fontWeight: FontWeight.bold,
                  wordSpacing: 0.05,
                  letterSpacing: 0.05,
                  height: 2.2,
                  fontSize: 24,
                  color: Colors.black,
                ),
                children: [
                  for (int u = 0; u < _items.length; u++)
                    (u) > 0
                        ? TextSpan(
                            style: TextStyle(
                                backgroundColor: (indexBackground) == u
                                    ? Colors.amber
                                    : Colors.transparent),
                            children: [
                              TextSpan(
                                style: TextStyle(
                                    backgroundColor: (indexBackground) == u
                                        ? Colors.amber
                                        : Colors.transparent),
                                text: _items[u]['text'],
                                recognizer: LongPressGestureRecognizer()
                                  ..onLongPress = () {
                                    indexBackground = u;
                                    setState(() {});
                                  },
                              ),
                              const WidgetSpan(
                                alignment: PlaceholderAlignment.middle,
                                child: SizedBox(width: 3),
                              ),
                              WidgetSpan(
                                alignment: PlaceholderAlignment.middle,
                                child: waqaf(u + 1),
                              ),
                              const WidgetSpan(
                                alignment: PlaceholderAlignment.middle,
                                child: SizedBox(
                                  width: 8,
                                ),
                              ),
                            ],
                          )
                        : const TextSpan(),
                ],
              ),
            ),
          ),
        ),
      );
    }

How to solve this?

I have set the outer border of the Container and padding to 0, but it doesn't work like the code below. I've also tried using widgetSpan to put text in, but the text just gets messy. Why not full colorBackground in TextSpan? Because, my code not worked. This photo is the result.

Sunderam Dubey
  • 1
  • 11
  • 20
  • 40

1 Answers1

1

Adding an extra space will help text: "${_items[u]['text']} ",

TextSpan(
  text:"${_items[u]['text']} ",
  style: TextStyle(
      backgroundColor: (indexBackground) == u
          ? Colors.amber
          : Colors.transparent),
  recognizer: LongPressGestureRecognizer()

Seems like text doesn't have enough padding.

If you like more, use WidgetSpan with a container padding and bg color.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56
  • I've tried using WidgetSpan to put the text, but it's a mess. It doesn't fit what I want, I want it like this photo. BackroundColor TextSpan full from start to finish (I marked the red box). However, in the photo above, it looks like there is padding so the backgroundColor is not full [I want this backround color full](https://i.ibb.co/tQ4J6RP/Text-Span-2.jpg) – Rian Pratama Aug 13 '22 at 18:03
  • you need to handle only left and right padding, also you can just add a space at end as I mentioned – Md. Yeasin Sheikh Aug 13 '22 at 18:06
  • I just updated my code, I've set all padding to 0 – Rian Pratama Aug 13 '22 at 18:13
  • Thanks, solved. I'm adding space at end, and i remove widgetSpan>SizeBox – Rian Pratama Aug 13 '22 at 18:21