Home > other >  How can i use textfield widget in a string?
How can i use textfield widget in a string?

Time:01-12

I want to use textfield or textformfield in a String. Ex: Beautiful ____ flutter. String value can have more than one word. How can i do that?

Thanks.

CodePudding user response:

You can use a Row like so

Row(
  children: [
    Expanded(
      child: Text(
        '..',
        overflow: TextOverflow.visible,
        maxLines: 1,
      ),
    ),
    Expanded(child: TextFormField(/*...*/)),
    Expanded(
      child: Text(
        '..',
        overflow: TextOverflow.visible,
        maxLines: 1,
      ),
    ),
  ],
),

CodePudding user response:

Well, guess I was wrong. I commented saying this was impossible, but it was simply me not knowing the right widget for the job, which is WidgetSpan. Extending what @developer extraordinare almost live example from dartpad

The results probably aren't as nice as you'd like, but it gets the job done. You probably have just to fiddle with SizedBox, constraints and the such to make the editable TextFormField fit nicely in the text.

CodePudding user response:

Assuming you want the same format for everything, here's the simplest solution I can think of:

Text("Beautiful"   myString   "flutter")

If you want to go a little more fancy and have different styles within your line of text you can use the RichText widget.

RichText(
  text: TextSpan(
    text: 'Beautiful ',
    style: DefaultTextStyle.of(context).style,
    children: const <TextSpan>[
      TextSpan(text: myString, style: TextStyle(fontWeight: FontWeight.bold)),
      TextSpan(text: ' flutter!'),
    ],
  ),
)
  •  Tags:  
  • Related