首页 > 解决方案 > 在 Flutter 的列中单击同一页面上的按钮返回一个新的小部件

问题描述

我想在同一页面上的一列中单击按钮返回一个新的小部件。因此,当单击按钮时,我试图在与列中的一个子项相同的页面上返回一个容器。

这是我的示例代码:

Column(
      children: <Widget>[
       // here I want to return my container in the show function

        Text(
          "This is a sample text",
          style: TextStyle(fontSize: 40),

        ),
        RaisedButton(
          child: Text("click"),
          onPressed: () {
            debugPrint("clicked");
            show();
          },
        )
      ],
    );



show() {
    return SampleContainer();
  }



class SampleContainer extends StatefulWidget {
  @override
  _SampleContainerState createState() => _SampleContainerState();
}

class _SampleContainerState extends State<SampleContainer> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Text("hi"),


);
  }
}

标签: flutterdart

解决方案


创建一个变量

bool showContainer=false;

然后是一个列小部件

Column(
      children: <Widget>[
       // here I want to return my container in the show function

            Text(
              "This is a sample text",
              style: TextStyle(fontSize: 40),

            ),
            if(showContainer)
              SampleContainer(),
            RaisedButton(
              child: Text("click"),
              onPressed: () {
                debugPrint("clicked");
                setState () {
                 showContainer=true;
                }
              },
            )
          ],
        );

推荐阅读