Flutter 如何在单击按钮时添加新的小部件 class CustomFlatButton扩展StatefulWidget{ 字符串ButtonName=“”; CustomFlatButton(字符串按钮名称){ this.ButtonName=ButtonName; } @凌驾 _CustomFlatButtonState createState()=>\u CustomFlatButtonState(ButtonName); } 类_CustomFlatButtonState扩展状态{ 字符串ButtonName=“”; _CustomFlatButtonState(字符串按钮名称){ this.ButtonName=ButtonName; } @凌驾 小部件构建(构建上下文){ 返回按钮( 颜色:颜色,蓝色, textColor:Colors.white, 禁用颜色:颜色。灰色, disabledTextColor:Colors.black, 填充:边缘设置。全部(8.0), splashColor:Colors.blueAccent, 已按下:(){ ListWheelScrollView(项目范围:31,使用放大镜:true,放大率:1.5,子项:[ 容器(颜色:彩色。红色,高度:200,宽度:200) ],); }, 子:文本( 按钮名称, 样式:TextStyle(fontSize:20.0), ), ); } }

Flutter 如何在单击按钮时添加新的小部件 class CustomFlatButton扩展StatefulWidget{ 字符串ButtonName=“”; CustomFlatButton(字符串按钮名称){ this.ButtonName=ButtonName; } @凌驾 _CustomFlatButtonState createState()=>\u CustomFlatButtonState(ButtonName); } 类_CustomFlatButtonState扩展状态{ 字符串ButtonName=“”; _CustomFlatButtonState(字符串按钮名称){ this.ButtonName=ButtonName; } @凌驾 小部件构建(构建上下文){ 返回按钮( 颜色:颜色,蓝色, textColor:Colors.white, 禁用颜色:颜色。灰色, disabledTextColor:Colors.black, 填充:边缘设置。全部(8.0), splashColor:Colors.blueAccent, 已按下:(){ ListWheelScrollView(项目范围:31,使用放大镜:true,放大率:1.5,子项:[ 容器(颜色:彩色。红色,高度:200,宽度:200) ],); }, 子:文本( 按钮名称, 样式:TextStyle(fontSize:20.0), ), ); } },flutter,flutter-layout,Flutter,Flutter Layout,如何在用户单击按钮时呈现小部件。这里是代码 class CustomFlatButton extends StatefulWidget { String ButtonName=""; CustomFlatButton(String ButtonName){ this.ButtonName=ButtonName; } @override _CustomFlatButtonState createState() => _CustomFlatButtonSta

如何在用户单击按钮时呈现小部件。这里是代码

    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(项目范围:31,使用放大镜:true,放大率:1.5,子项:[
容器(颜色:彩色。红色,高度:200,宽度:200)
],);
}
我想在按下按钮时显示ListWheelScrollView小部件意味着当按下按钮时,应该显示ListWheelScrollView


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

,您可以创建一个变量,表示如果按下按钮,我们将称此为
已按下
,如果为真,则显示
ListWheelScrollView
,如果为假,则显示一个空的
容器(高度:0,宽度:0)
。当按下按钮时,将调用
setState()
来重建小部件。这将类似于以下内容:

行(
儿童:[
扁平按钮(
颜色:颜色,蓝色,
textColor:Colors.white,
禁用颜色:颜色。灰色,
disabledTextColor:Colors.black,
填充:边缘设置。全部(8.0),
splashColor:Colors.blueAccent,
已按下:(){
设置状态(){
isPressed=true;
});
},
子:文本(
按钮名称,
样式:TextStyle(fontSize:20.0),
),
),
(已发表)
?ListWheelScrollView(
项目范围:31,
使用放大镜:正确,
放大倍数:1.5,
儿童:[
容器(颜色:Colors.red,高度:200,宽度:200)
],
)
:容器(
高度:0,,
宽度:0,
)
],
);
您可以在声明按钮名称的地方声明
bool isPressed=false
。希望这有帮助,如果有,请投票,否则留下评论

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