加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

dart – 在按钮单击时更新flutter中的文本字段

发布时间:2020-12-14 14:52:22 所属栏目:百科 来源:网络整理
导读:我正在开发一个扑动的应用程序,在主屏幕上我有一个按钮打开另一个屏幕,这是一个方法.在那个屏幕上我想要做一些计算,比如取用户输入和更新文本字段,点击按钮方法calculateAmount调用哪个更新变量总数反映在文本字段但文本字段没有更新,它只在更新时按下完成键
我正在开发一个扑动的应用程序,在主屏幕上我有一个按钮打开另一个屏幕,这是一个方法.在那个屏幕上我想要做一些计算,比如取用户输入和更新文本字段,点击按钮方法calculateAmount调用哪个更新变量总数反映在文本字段但文本字段没有更新,它只在更新时按下完成键盘……如何才能完成这项任务.
这是我的代码:

import 'package:flutter/material.dart';
    void main() {

      runApp(new MaterialApp(
          debugShowCheckedModeBanner: false,home: new homePage()
      ));
    }

    class homePage extends StatefulWidget {

      @override
      homePageState  createState() => new homePageState();
    }
    class homePageState extends State<homePage> {

    double metal = 0.0;
    double total = 0.0;

      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("APP-Title",),backgroundColor: Colors.orange),body: new Container(
            child: new Center(
                child: new Column(
                  mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[

                    new Row(
                      mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[

                        new Expanded(child: new RaisedButton.icon(
                          color: Colors.orange,icon: const Icon(
                            Icons.info,size: 25.0,color: Colors.white,label: new Text('Calculate',style: new TextStyle(
                              color: Colors.white
                              )),onPressed: () {
                            calculateWindow();
                          ),],)
              ],)
        ),);
  }
 void calculateWindow(){
    Navigator.of(context).push(
      new MaterialPageRoute(
        builder: (context) {
          return new Scaffold(
            appBar: new AppBar(
              title: new Text('Calculator'),backgroundColor: Colors.orange,body: new ListView(
              children: <Widget>[

                new Row(
                  crossAxisAlignment: CrossAxisAlignment.center,children: <Widget>[
                    new Container(
                      height: 50.0,width: 360.0,decoration: new BoxDecoration(
                        color: Colors.orange,shape: BoxShape.rectangle,child: new Center(
                        child: new Row(
                          children: <Widget>[
                            new Expanded(
                              child: new Container(
                                child:  new Text("Gold & Silver in Sold & Ornaments",style: textStyle,textAlign: textAlign
                                ),new Container(
                              height: 40.0,width: 80.0,decoration:  new BoxDecoration(
                                  color: Colors.white),child: new TextField(
                                  keyboardType: TextInputType.number,onSubmitted : (String value) {
                                    try {
                                      metal = double.parse(value.toString());
                                      print(total);
                                    } catch (exception) {
                                      metal = 0.0;
                                    }
                                  }
                              ),new Row(
                  mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
                    new Expanded(
                      child: new Container(
                          width: 50.0,child: new RaisedButton.icon(
                            color: Colors.grey,icon: const Icon(
                              Icons.check,style: new TextStyle(
                                color: Colors.white,)),onPressed: calculateAmount,new Row(
                  crossAxisAlignment: CrossAxisAlignment.center,width: 350.0,decoration: new BoxDecoration(
                        color: Colors.blueGrey,child: new Center(
                        child: new Row(
                          children: <Widget>[
                            new Expanded(
                              child: new Container(
                                child:  new Text("Total Amount:",style: new TextStyle(
                                  color: Colors.white,textAlign: TextAlign.left,new Container(

                              child: new Text(
                                '$total',overflow: TextOverflow.ellipsis,textDirection: TextDirection.ltr,)
                            ),);
        },);
  }
 void calculateAmount(){

    setState((){
      total =  metal + 0.025;

    });
  }
}

解决方法

我真的不明白你想要的输出是什么,但是这个例子可能有助于向你展示代码中出错的地方.

enter image description here

class TextFieldEx extends StatefulWidget {
  @override
  _TextFieldExState createState() => new _TextFieldExState();
}

class _TextFieldExState extends State<TextFieldEx> {
  TextEditingController _c ;
  double _metal = 0.0;
  double _total = 0.0;
  String _text = "initial";
  @override
  void initState() {
      _c = new TextEditingController();
      super.initState();
    }
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,children: <Widget>[
            new TextField(
              keyboardType: TextInputType.number,onChanged: (v)=>setState((){_text=v;}),controller: _c,new RaisedButton(
              child: new Text("Update"),onPressed: (){
                setState((){
                  _metal = double.parse(_c.text);
                  _total = _metal+0.025;
                  _c.text = "";
                });
              },new Text("Text Input: $_text"),new Text("Metal :$_metal"),new Text("Total:$_total")
          ],)
      )
    );
  }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读