如何在单击按钮时添加新的小部件

问题描述 投票:0回答:1
    class CustomFlatButton extends StatefulWidget {
  String ButtonName="";
  CustomFlatButton(String ButtonName){
    this.ButtonName=ButtonName;
  }
  @override
  _CustomFlatButtonState createState() => _CustomFlatButtonState(ButtonName);
}

class _CustomFlatButtonState extends State<CustomFlatButton> {
  String ButtonName="";
  _CustomFlatButtonState(String ButtonName){
    this.ButtonName=ButtonName;
  }
  @override
  Widget build(BuildContext context) {
    return FlatButton(
                                  color: Colors.blue,
                                  textColor: Colors.white,
                                  disabledColor: Colors.grey,
                                  disabledTextColor: Colors.black,
                                  padding: EdgeInsets.all(8.0),
                                  splashColor: Colors.blueAccent,
                                  onPressed: () {
                                    ListWheelScrollView(itemExtent: 31,useMagnifier: true,magnification: 1.5,children: <Widget>[
                                      Container(color: Colors.red,height: 200,width: 200)
                                    ],);
                                  },
                                  child: Text(
                                    ButtonName,
                                    style: TextStyle(fontSize: 20.0),
                                  ),
                                );
  }
}

如何在用户单击按钮时呈现窗口小部件。在代码中

onPressed: () {
                                ListWheelScrollView(itemExtent: 31,useMagnifier: true,magnification: 1.5,children: <Widget>[
                                  Container(color: Colors.red,height: 200,width: 200)
                                ],);
                              }

我想在按下按钮时显示ListWheelScrollView小部件,这意味着当按下按钮时,应该显示ListWheelScrollView。

我是新手。如果您在理解我的问题时遇到任何困难,请告诉我

flutter flutter-layout
1个回答
0
投票

您可以创建一个变量,该变量表示如果按下按钮,我们将其称为hasPressed,如果为true,则显示ListWheelScrollView,但是如果为false,则显示为空Container(height: 0, width: 0)。当按下按钮时,将调用setState()来重建小部件。看起来类似于以下内容:

Row(
      children: <Widget>[
        FlatButton(
          color: Colors.blue,
          textColor: Colors.white,
          disabledColor: Colors.grey,
          disabledTextColor: Colors.black,
          padding: EdgeInsets.all(8.0),
          splashColor: Colors.blueAccent,
          onPressed: () {
            setState(() {
              isPressed = true;
            });
          },
          child: Text(
            ButtonName,
            style: TextStyle(fontSize: 20.0),
          ),
        ),
        (isPressed)
            ? ListWheelScrollView(
                itemExtent: 31,
                useMagnifier: true,
                magnification: 1.5,
                children: <Widget>[
                  Container(color: Colors.red, height: 200, width: 200)
                ],
              )
            : Container(
                height: 0,
                width: 0,
              )
      ],
    );

您将在声明bool isPressed = false的地方声明ButtonName。希望这会有所帮助,如果可以,请投赞成票,否则请发表评论。

© www.soinside.com 2019 - 2024. All rights reserved.