The ElevatedButton isn’t changing the text value.

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

The ElevatedButton isn't changing the text value

问题

I'm trying to change the value of the text in the button whenever I press it, but it stays the same. Tried using Navigator.push but I kept getting errors from it so I deleted it off.

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

int myVariableNumber = 0;

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Hello World"),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black,
      body: Scaffold(
        body: Container(
          alignment: Alignment.center,
          width: 300,
          height: 300,
          color: Colors.white,
          child: SizedBox(
            width: 140,
            height: 70,
            child: ElevatedButton(
              onPressed: () {
                myVariableNumber = myVariableNumber + 1;
                Navigator.push(context, -)
              },
              child: Text('Hello $myVariableNumber'),
            ),
          ),
        ),
      ),
    );
  }
}

Tried Navigator.push and I expected it to refresh and change numbers, but it only gave me errors.

英文:

I'm trying to change the value of the text in the button whenever I press it, but it stays the same. Tried using Naviagation.push but I kept getting errors from it so I deleted it off.


class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

int myVariableNumber = 0;

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(&quot;Hello World&quot;),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black,
      body: Scaffold(
        body: Container(
          alignment: Alignment.center,
          width: 300,
          height: 300,
          color: Colors.white,
          child: SizedBox(
            width: 140,
            height: 70,
            child: ElevatedButton(
              onPressed: () {
                myVariableNumber = myVariableNumber + 1;
                Navigator.push(context, -)
                
              },
              
              child: Text(&#39;Hello $myVariableNumber&#39;),
            ),
          ),

        ),
      ),
    );
  }
}

Tried navigation.push and I expected it to refresh and change numbers, but it only gave me errors.

答案1

得分: 1

要更新此按钮的值,您需要通过调用setState方法重建您的小部件。并将您的状态移到State子类中(在这种情况下是_MyHomePageState)。

import 'package:flutter/material.dart';

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int myVariableNumber = 0; // 将您的状态移到这里

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Hello World"),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black,
      body: Container(
        alignment: Alignment.center,
        width: 300,
        height: 300,
        color: Colors.white,
        child: SizedBox(
          width: 140,
          height: 70,
          child: ElevatedButton(
            onPressed: () => setState(() { // 这将使用新的myVariableNumber值更新您的小部件
              myVariableNumber = myVariableNumber + 1;
            }),
            child: Text('Hello $myVariableNumber'),
          ),
        ),
      ),
    );
  }
}
英文:

To update value of this button, you need to rebuild your widget by calling setState method. And move your state into State subclass (in this case is _MyHomePageState).

import &#39;package:flutter/material.dart&#39;;

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State&lt;MyHomePage&gt; createState() =&gt; _MyHomePageState();
}

class _MyHomePageState extends State&lt;MyHomePage&gt; {
  int myVariableNumber = 0; // move your state here

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text(&quot;Hello World&quot;),
        backgroundColor: Colors.black,
      ),
      backgroundColor: Colors.black,
      // body: Scaffold( // remove this unnecessary Scaffold
      body: Container(
        alignment: Alignment.center,
        width: 300,
        height: 300,
        color: Colors.white,
        child: SizedBox(
          width: 140,
          height: 70,
          child: ElevatedButton(
            onPressed: () =&gt; setState(() { // this will update your widget with new myVariableNumber value
              myVariableNumber = myVariableNumber + 1;
            }),
            child: Text(&#39;Hello $myVariableNumber&#39;),
          ),
        ),
      ),
      // ),
    );
  }
}

答案2

得分: 0

使用 setState() 方法来操作 myVariableNumber

英文:

Use setState() method for myVariableNumber.

huangapple
  • 本文由 发表于 2023年7月13日 10:35:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675543.html
匿名

发表评论

匿名网友

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

确定