setState() 在 Flutter 中没有更新 Text() widget。

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

setState() is not updating the Text() widget in flutter

问题

我正在尝试构建一个类似计算器的应用程序,但它具有一些额外的按钮,可以自动将一个数字设置为displayedNumber。我尝试使用setState()来设置数字,但Text()小部件没有更新。

这是我尝试的代码:

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {

    String name = 'Amir Mahdi Abravesh';
    String phoneNumber = '+989920250829';
    double input = 0.00;
    double displayedNumber = 0.0;
    String inputST = '\$' + '$displayedNumber';

    void displayingDigits(){
      if(displayedNumber == 0.0){
        displayedNumber += input;
      } else {
        displayedNumber = displayedNumber * 10;
        displayedNumber += input;
      }
    }

    return Scaffold(
      backgroundColor: Colors.white,

      appBar: AppBar(
        title: Center(
          child: Text(
            'Send Money to',
            style: TextStyle(
              color: Colors.black,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0,
      ),

      body: Column(
        children: <Widget>[

          SizedBox(height: 10,),

          Expanded(
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,

                children: <Widget>[
                  CircleAvatar(
                    backgroundImage: AssetImage('assets/AMA25 trs.png'),
                    backgroundColor: Colors.black,
                    radius: 30,
                  ),
                  
                  SizedBox(height: 5,),
            
                  Text(
                    '$name',
                    style: TextStyle(
                      fontWeight: FontWeight bold,
                      fontSize: 20,
                    ),
                  ),
                  
                  SizedBox(height: 5,),
            
                  Text(
                    '$phoneNumber',
                    style: TextStyle(
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
          ),

          Expanded(
            child: Container(
              padding: EdgeInsets.all(30),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.end,
                children: <Widget>[
                      
                  Text(
                    '$displayedNumber',
                    style: TextStyle(
                      fontSize: 30,
                      fontWeight: FontWeight.bold,
                    ),
                  ),

                  Divider(
                    height: 30,
                    color: Colors.grey[700],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      
                      TextButton(
                        onPressed: () {
                          setState(() {
                            displayedNumber = 50.0;
                          });
                        },
                        child: Container(
                          padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
                          decoration: BoxDecoration(
                            border: Border.all(color: Colors.grey),
                            borderRadius: BorderRadius.circular(15),
                          ),
                          child: Text(
                            '\$50',
                            style: TextStyle(
                              color: Colors.grey,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

你可以忽略我已注释的inputST,那只是为了在displayedNumber之前显示$

我希望这个按钮将50设置为displayedNumber,并在屏幕上更新数字。

英文:

I'm trying to build an app that is like a calculator but it has some additional buttons that automaticly sets a number to displayedNumber. I tried the setState() for setting the number but the Text() widget is not updating.

This is what I tried:

import &#39;package:flutter/material.dart&#39;;
void main() {
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatefulWidget {
@override
State&lt;Home&gt; createState() =&gt; _HomeState();
}
class _HomeState extends State&lt;Home&gt; {
@override
Widget build(BuildContext context) {
String name = &#39;Amir Mahdi Abravesh&#39;;
String phoneNumber = &#39;+989920250829&#39;;
double input = 0.00;
double displayedNumber = 0.0;
String inputST = &#39;\$&#39; + &#39;$displayedNumber&#39;;
void displayingDigits(){
if(displayedNumber == 0.0){
displayedNumber += input;
} else {
displayedNumber = displayedNumber * 10;
displayedNumber += input;
}
}
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Center(
child: Text(
&#39;Send Money to&#39;,
style: TextStyle(
color: Colors.black,
// fontFamily: &#39;SF Pro&#39;,
fontWeight: FontWeight.bold,
),
),
),
centerTitle: true,
backgroundColor: Colors.white,
// shadowColor: Color.fromARGB(197, 192, 192, 192),
elevation: 0,
),
body: Column(
children: &lt;Widget&gt;[
SizedBox(height: 10,),
Expanded(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
CircleAvatar(
backgroundImage: AssetImage(&#39;assets/AMA25 trs.png&#39;),
backgroundColor: Colors.black,
radius: 30,
),
SizedBox(height: 5,),
Text(
&#39;$name&#39;,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
SizedBox(height: 5,),
Text(
&#39;$phoneNumber&#39;,
style: TextStyle(
color: Colors.grey,
),
),
],
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: &lt;Widget&gt;[
Text(
&#39;$displayedNumber&#39;,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
Divider(
height: 30,
color: Colors.grey[700],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
TextButton(
onPressed: () {
setState(() {
displayedNumber = 50.0;
// inputST = &#39;\$&#39; + &#39;$displayedNumber&#39;;
});
},
child: Container(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 15),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15),
),
child: Text(
&#39;\$50&#39;,
style: TextStyle(
color: Colors.grey,
// fontSize: 18,
),
),
),
),
],
),
),
),
],
),
);
}
}

You can ignore the inputST that I commented. That's just for showing a $ before the displayedNumber.

I want this button to set 50 to the displayedNumber and update the number on the screen.

setState() 在 Flutter 中没有更新 Text() widget。

答案1

得分: 1

因为你在build()内部定义了double displayedNumber = 0.0;

请将double displayedNumber = 0.0;定义在build()外部。

class _HomeState extends State<Home> {
  double displayedNumber = 0.0; // 在这里初始化
  @override
  Widget build(BuildContext context) {}
}
英文:

Because you have define double displayedNumber = 0.0; inside build().

Define double displayedNumber = 0.0; outside the build().

class _HomeState extends State&lt;Home&gt; {
double displayedNumber = 0.0; // Initialize here
@override
Widget build(BuildContext context) {}}

答案2

得分: 1

以下是代码部分的中文翻译:

// 这里是代码部分
// 忽略文件中的字符串拼接,优先使用插值
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    home: Home(),
  ));
}

class Home extends StatefulWidget {
  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  String name = 'Amir Mahdi Abravesh';
  String phoneNumber = '+989920250829';
  double input = 0.00;
  double displayedNumber = 0.0;

  @override
  Widget build(BuildContext context) {
    void displayingDigits() {
      if (displayedNumber == 0.0) {
        displayedNumber += input;
      } else {
        displayedNumber = displayedNumber * 10;
        displayedNumber += input;
      }
    }

    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        title: Center(
          child: Text(
            'Send Money to',
            style: TextStyle(
              color: Colors.black,
              // fontFamily: 'SF Pro',
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
        centerTitle: true,
        backgroundColor: Colors.white,
        // shadowColor: Color.fromARGB(197, 192, 192, 192),
        elevation: 0,
      ),
      body: Column(
        children: <Widget>[
          SizedBox(
            height: 10,
          ),
          Expanded(
            child: Center(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  CircleAvatar(
                    backgroundImage: AssetImage('assets/AMA25 trs.png'),
                    backgroundColor: Colors.black,
                    radius: 30,
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '$name',
                    style: TextStyle(
                      fontWeight: FontWeight.bold,
                      fontSize: 20,
                    ),
                  ),
                  SizedBox(
                    height: 5,
                  ),
                  Text(
                    '$phoneNumber',
                    style: TextStyle(
                      color: Colors.grey,
                    ),
                  ),
                ],
              ),
            ),
          ),
          Expanded(
            child: Container(
              padding: EdgeInsets.all(30),
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: <Widget>[
                    Text(
                      '$displayedNumber',
                      style: TextStyle(
                        fontSize: 30,
                        fontWeight: FontWeight.bold,
                      ),
                    ),
                    Divider(
                      height: 30,
                      color: Colors.grey[700],
                    ),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        TextButton(
                          onPressed: () {
                            setState(() {
                              displayedNumber = 50.0;
                              // inputST = '\$' + '$displayedNumber';
                            });
                          },
                          child: Container(
                            padding: EdgeInsets.symmetric(
                                vertical: 10, horizontal: 15),
                            decoration: BoxDecoration(
                              border: Border.all(color: Colors.grey),
                              borderRadius: BorderRadius.circular(15),
                            ),
                            child: Text(
                              '\$50',
                              style: TextStyle(
                                color: Colors.grey,
                                // fontSize: 18,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ]),
            ),
          )
        ],
      ),
    );
  }
}

注意:这是你提供的代码的中文翻译,只包括代码部分,没有其他内容。

英文:

Here you go . You just needed to move variables after State

// ignore_for_file: prefer_interpolation_to_compose_strings
import &#39;package:flutter/material.dart&#39;;
void main() {
runApp(MaterialApp(
home: Home(),
));
}
class Home extends StatefulWidget {
@override
State&lt;Home&gt; createState() =&gt; _HomeState();
}
class _HomeState extends State&lt;Home&gt; {
String name = &#39;Amir Mahdi Abravesh&#39;;
String phoneNumber = &#39;+989920250829&#39;;
double input = 0.00;
double displayedNumber = 0.0;
@override
Widget build(BuildContext context) {
void displayingDigits() {
if (displayedNumber == 0.0) {
displayedNumber += input;
} else {
displayedNumber = displayedNumber * 10;
displayedNumber += input;
}
}
return Scaffold(
backgroundColor: Colors.white,
appBar: AppBar(
title: Center(
child: Text(
&#39;Send Money to&#39;,
style: TextStyle(
color: Colors.black,
// fontFamily: &#39;SF Pro&#39;,
fontWeight: FontWeight.bold,
),
),
),
centerTitle: true,
backgroundColor: Colors.white,
// shadowColor: Color.fromARGB(197, 192, 192, 192),
elevation: 0,
),
body: Column(
children: &lt;Widget&gt;[
SizedBox(
height: 10,
),
Expanded(
child: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
CircleAvatar(
backgroundImage: AssetImage(&#39;assets/AMA25 trs.png&#39;),
backgroundColor: Colors.black,
radius: 30,
),
SizedBox(
height: 5,
),
Text(
&#39;$name&#39;,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20,
),
),
SizedBox(
height: 5,
),
Text(
&#39;$phoneNumber&#39;,
style: TextStyle(
color: Colors.grey,
),
),
],
),
),
),
Expanded(
child: Container(
padding: EdgeInsets.all(30),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: &lt;Widget&gt;[
Text(
&#39;$displayedNumber&#39;,
style: TextStyle(
fontSize: 30,
fontWeight: FontWeight.bold,
),
),
Divider(
height: 30,
color: Colors.grey[700],
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: &lt;Widget&gt;[
TextButton(
onPressed: () {
setState(() {
displayedNumber = 50.0;
// inputST = &#39;\$&#39; + &#39;$displayedNumber&#39;;
});
},
child: Container(
padding: EdgeInsets.symmetric(
vertical: 10, horizontal: 15),
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(15),
),
child: Text(
&#39;\$50&#39;,
style: TextStyle(
color: Colors.grey,
// fontSize: 18,
),
),
),
),
],
),
]),
),
)
],
),
);
}
}

huangapple
  • 本文由 发表于 2023年2月18日 17:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492463.html
匿名

发表评论

匿名网友

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

确定