flutter - 在 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"),
);
}
}
解决方案
创建一个变量
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;
}
},
)
],
);
推荐阅读
- amazon-web-services - AWS CLI Lambda 单次调用导致多次调用
- sql - 分组后的最大值与总和
- javascript - 使用 step 对 Jquery 中的许多不同 div 进行动画处理
- c# - 为什么我不能将派生程度更高的泛型类型转换为派生程度较低的泛型类型?
- javascript - Ajax 检测到“跨域请求被阻止”错误
- java - SpEL:当使用 SpEL 编译时,表达式求值的性能没有明显差异
- c++ - 如何正确实现与基类不同的版本?
- javascript - jquery:我可以使用启用和禁用的伪选择器与 javascript 中的 css 方法吗?
- python - 我应该如何组织大部分相同的脚本?
- realitykit - 我想 addChild RealityComposer 创建的实体