如何将 for 循环的值打印到按钮部件上并显示在应用屏幕上?-Flutter

huangapple go评论72阅读模式
英文:

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&lt;MyHomePage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(&quot;For D&#246;ng&#252;s&#252;&quot;),
        ),
        body: Column(
          children: [
            ElevatedButton(
              child: Text(&quot;Sayilar&quot;),
              onPressed: () {
                for (int i = 0; i &lt; 15; i++) {
                  Text(&quot;$i&quot;);
                }
              },
            ),
          ],
        ));
  }
}

答案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&lt;Widget&gt; items = [];
@override
Widget build(BuildContext context) {
  return Scaffold(
      appBar: AppBar(
        title: Text(&quot;For D&#246;ng&#252;s&#252;&quot;),
      ),
      body: Column(
        children: [
          ElevatedButton(
            child: Text(&quot;Sayilar&quot;),
            onPressed: () {
              for (int i = 0; i &lt; 15; i++) {
                items.add(Text(&quot;$i&quot;));
              }
              setState(() {}); //update the ui
            },
          ),
          Expanded(
              child: ListView.builder(
                  itemCount: items.length,
                  itemBuilder: (context, index) =&gt; items[index]))
        ],
      ));
}

huangapple
  • 本文由 发表于 2023年2月27日 01:14:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573700.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定