Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

Flutter: How can I change the Alignment of a Text in a Textfield programmatically via buttons?

I am new to flutter and trying to build some basic text editing options like in word. That means, bold, italic, underlined and so on...

I am currently struggling building those three buttons for left, centered and right text alignment. I′ve tried to build them the same way as I did with bold and so one;

bool isBold = false;

Button:

_NoteButton(Icons.format_bold, Colors.green, () => setState(() => isBold = !isBold)),

In the TextField:

style: new TextStyle(fontWeight: isBold ? FontWeight.bold : FontWeight.normal),

the problem I have is that i only have "one" TextAlignment line for three Alignments, so it′s not possible to do it exactly the same way as with bold or italic.

It would really help if you could share your code of three buttons to align the text on the left, in the center and on the right in a textfield.

Thank you!

question from:https://stackoverflow.com/questions/65831382/flutter-how-can-i-change-the-alignment-of-a-text-in-a-textfield-programmaticall

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Declare a variable,

TextAlign _align;

Then use three buttons in a row,

Row(
    children: <Widget>[
      FlatButton(
        onPressed: () {
          setState(() {
            _align = TextAlign.left;
          });
        },
        child: Text('Align left'),
      ),
      FlatButton(
        onPressed: () {
          setState(() {
            _align = TextAlign.center;
          });
        },
        child: Text('Align center'),
      ),
      FlatButton(
        onPressed: () {
          setState(() {
            _align = TextAlign.right;
          });
        },
        child: Text('Align right'),
      ),
    ],
  );

Then in your Text widget,

Text('Align right', textAlign: _align)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...