【发布时间】:2020-11-25 07:10:20
【问题描述】:
当我尝试将动态小部件添加到我的应用程序时,我遇到了一个奇怪的错误。当按下添加按钮屏幕变成完全白色时,我找不到它发生的原因。 我使用https://www.youtube.com/watch?v=xPW1vtDDlt4 作为资源我在 Flutter 上真的很新,也许我忘记了要添加的东西,但我检查了很多次。 这是我的代码,
class DynamicWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Container(
child: new TextField(
decoration: new InputDecoration(hintText: 'Press + to Add Field'),
),
);
}
}
列表的初始化。
List<DynamicWidget> listDynamic = [];
我的功能是将小部件添加到列表中。
addDynamic() {
listDynamic.add(new DynamicWidget());
print("addDynamic");
setState(() {});
}
我不确定,但问题可能在这里,
final testText = Visibility(
child: new Column(
children: <Widget>[
new Flexible(
child: new ListView.builder(
itemCount: listDynamic.length,
itemBuilder: (_, index) => listDynamic[index],
),
),
],
),
);
在这里我调用我的小部件,我在这里将它声明为变量。
final body = Container(
child: SingleChildScrollView(
child: Column(
children: <Widget>[
testText,
strPhoto
],
),
),
);
最后是我的按钮。
return Scaffold(
appBar: new AppBar(title: new Text(device_type), centerTitle: true),
drawer: Menu(),
body: body,
floatingActionButton: Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
heroTag: null,
child: Icon(
Icons.add,
color: Colors.white,
),
onPressed: () {
addDynamic();
},
),
],
));
谢谢你帮助我。
【问题讨论】:
-
所有这些代码块都在一个文件中吗?如果所有这些都在一个文件中,请更新您的问题,将所有这些代码块提供到一个代码块中。因为你的函数,testText 和其他函数在哪里可能有问题。
-
我知道哪个代码块在做什么。但我要求将所有这些都放在一个代码块中。因为您的代码形成可能有问题。我已经复制了所有这些代码。但我不能跑。它在
SingleChildScrollView中显示错误。因此,如果您将它们全部放在一个代码块中,将会很有帮助。
标签: android flutter dart mobile cross-platform