setState() 没有更新 flutter 中的 Text() 小部件

问题描述 投票:0回答:2

我正在尝试构建一个类似于计算器的应用程序,但它有一些额外的按钮可以自动将数字设置为 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,
              // 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,
                            ),
                          ),
                        ),
                      ),
                ],
              ),
            ),
          ),
        ],
      ),

    );
  }
}

你可以忽略我评论的inputST。那只是为了在 $ 之前显示一个 displayedNumber

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

flutter setstate
2个回答
0
投票

因为你已经定义了 double displayedNumber = 0.0; inside build().

double displayedNumber = 0.0;.

之外定义 build()
class _HomeState extends State<Home> {
 double displayedNumber = 0.0; // Initialize here
  @override
  Widget build(BuildContext context) {}}

0
投票

给你。您只需要在 State

之后移动变量
// ignore_for_file: prefer_interpolation_to_compose_strings

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,
                              ),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ]),
            ),
          )
        ],
      ),
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.