英文:
How can I print the for loop values in the button widget to the application screen?-Flutter
问题
当我按下按钮时,我希望在应用程序屏幕上打印出for循环中的值。
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("For 循环"),
),
body: Column(
children: [
ElevatedButton(
child: Text("数字"),
onPressed: () {
for (int i = 0; i < 15; i++) {
print("$i");
}
},
),
],
));
}
}
英文:
I'm learning Flutter. I have a problem. When I press the button, I want the values in the for loop to be printed on the application screen.
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("For Döngüsü"),
),
body: Column(
children: [
ElevatedButton(
child: Text("Sayilar"),
onPressed: () {
for (int i = 0; i < 15; i++) {
Text("$i");
}
},
),
],
));
}
}
答案1
得分: 0
List<Widget> items = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("For Döngüsü"),
),
body: Column(
children: [
ElevatedButton(
child: Text("Sayılar"),
onPressed: () {
for (int i = 0; i < 15; i++) {
items.add(Text("$i"));
}
setState(() {}); // 更新界面
},
),
Expanded(
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) => items[index]))
],
));
}
英文:
You can create a list on state class and widget on it.Then I am using ListView.builder to show the widget on app screen.
List<Widget> items = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("For Döngüsü"),
),
body: Column(
children: [
ElevatedButton(
child: Text("Sayilar"),
onPressed: () {
for (int i = 0; i < 15; i++) {
items.add(Text("$i"));
}
setState(() {}); //update the ui
},
),
Expanded(
child: ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) => items[index]))
],
));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论