单击按钮时在同一屏幕上显示 TextField 值

Pra*_*pta 3 dart flutter

这是我的屏幕,带有TextFieldButton。当有人单击“显示”按钮时,我希望它在按钮下方显示名称,如下图所示。

在此输入图像描述 在此输入图像描述

代码如下:


class Demo extends StatefulWidget {
  @override
  _DemoState createState() => _DemoState();
}

class _DemoState extends State<Demo> {
  final name = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            Row(
              children: [
                Text(
                  'Name'
                ),
                TextField(
                  controller: name,
                )
              ],
            ),
            RaisedButton(
              onPressed: (){

              },
              child: Text('Show'),
            )
          ],
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

这可以作为您问题的基本示例。UI 与您上面显示的不完全一样

class Question extends StatefulWidget {
  Question({Key key}) : super(key: key);

  @override
  _QuestionState createState() => _QuestionState();
}

class _QuestionState extends State<Question> {
  String text = '';
  bool shouldDisplay = false;

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Center(
          child: TextField(
            onChanged: (value) {
              setState(() {
                text = value;
              });
            },
          ),
        ),
        FlatButton(onPressed: () {
          setState(() {
            shouldDisplay = !shouldDisplay;
          });
        }, child: Text('Submit')),
        shouldDisplay ? Text(text) : Spacer()
      ],
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。