Closed. This question needs details or clarity。它当前不接受答案。












想改善这个问题吗?添加详细信息,并通过editing this post阐明问题。

1年前关闭。



Improve this question




我是新手,希望在按下按钮后显示文本小部件。
   import 'package:flutter/material.dart';

    class dellecturer extends StatefulWidget {
      @override
      _dellecturerState createState() => _dellecturerState();
    }

    class _dellecturerState extends State<dellecturer> {
      @override
      Widget build(BuildContext context) {
        return Card(
          color: Colors.grey.shade300,
          margin: EdgeInsets.all(10.0),
        child: Container(
          margin: EdgeInsets.all(20),
          child: ListView(
            children: <Widget>[
             new Container(
               margin: EdgeInsets.only(top:13,bottom: 25),
               alignment: Alignment.topCenter,
                 child:Text("DELETE BY LECTURER ID",style: TextStyle(color: Colors.redAccent,fontWeight: FontWeight.bold,fontSize: 20.0),),
             ),
             new TextField(
                decoration:InputDecoration(border: OutlineInputBorder(borderSide: BorderSide(style: BorderStyle.solid),),labelText: "LECTURER ID",hintText: "Enter lecturer ID",prefixIcon: Icon(Icons.person)),
              ),
              Padding(padding: EdgeInsets.only(top:20),),
             new RaisedButton(child: Text("OK",style: TextStyle(fontWeight: FontWeight.bold,color: Colors.white,fontSize: 15),),color: Colors.red,onPressed: (){Form(child: Search());},),//onclick search
            ],
          ),


        ),
        );

      }
    Widget Search(){  //should display
      setState(() {
      return Scaffold(
        body: Container(
        child: ListView(
          children: <Widget>[
            new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
             new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
              new Text("ID :1234",style: TextStyle(color: Colors.redAccent),),
          ],
        ),
        )
      );
      });

    }
    }

最佳答案

我不明白您到底想在代码中做什么,但是如果您需要在按下按钮后显示Text小部件,则可以简单地定义一个bool变量来检测按钮的单击,如下所示:

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(
      home: MyApp(),
    ));

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  bool pressed = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView(
        children: <Widget>[
          pressed ? Text(" text is here ") : SizedBox(),
          RaisedButton(
            child: Text("show text"),
            onPressed: () {
              setState(() {
                pressed = true;
              });
            },
          )
        ],
      ),
    );
  }
}

关于dart - Flutter代码,用于在按下按钮时添加或显示小部件,或在按下方法时设置状态,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53799794/

10-09 03:22