Home > Blockchain >  How to control svg width using the space occupied by the text in the column flutter
How to control svg width using the space occupied by the text in the column flutter

Time:11-24

How to control svg width using the space occupied by the text in the column.

What I want

enter image description here enter image description here

What I am getting- enter image description here

Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(text),
        SvgPicture.asset(
          Assets.weirdLine,
          fit: BoxFit.fill,
        ),
      ],
    )

Also tried with code-

Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(text),
        Container(
          height: 4,
          child: Expanded(
//            fit: FlexFit.tight,
            child: SvgPicture.asset(
              Assets.weirdLine,
              fit: BoxFit.fill,
            ),
          ),
        )
      ],
    )

CodePudding user response:

you can get text width and set it to svg width. use this code,

Size calcTextSize(String text, TextStyle style) {
final TextPainter textPainter = TextPainter(
  text: TextSpan(text: text, style: style),
  textDirection: TextDirection.ltr,
  textScaleFactor: WidgetsBinding.instance.window.textScaleFactor,
)..layout();
return textPainter.size;
}

final double textWidth = calcTextSize(text, TextStyle(fontSize: fontSize)).width;
  • Related