0

我需要帮助解决问题。我正在尝试检索一个值,该值是添加多个文本字段值的结果,以便能够在另一个屏幕中使用它(通过 sharedpreferencies 或 sqlite)......我似乎无法得到这个值(在我的示例中为“总值”)。有人有我的方法吗?

import 'package:flutter/material.dart';

class Screen1 extends StatefulWidget {
  Screen1({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _Screen1State createState() => _Screen1State();
}

class _Screen1State extends State<Screen1> {

  final myController1 = TextEditingController();
  final myController2 = TextEditingController();


  @override
  void dispose() {
    myController1.dispose();
    myController2.dispose();
    super.dispose();
  }
  double value1;
  double value2;
  //double totalvalues = value1 + value2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Screen 1'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: myController1,
                    keyboardType: TextInputType.number,
                    onChanged: (val) {
                      setState(() {
                        value1 = double.parse(myController1.text) * 10;
                      });
                    },
                    decoration: InputDecoration(
                        hintText: '0'
                    ),
                  ),
                ),
                Expanded(
                    child: Center(
                        child: value1 != null
                            ? Text(value1.toStringAsFixed(0))
                            : Text('0')
                    )
                ),
              ],
            ),
            Row(
              children: [
                Expanded(
                  child: TextField(
                    controller: myController2,
                    keyboardType: TextInputType.number,
                    onChanged: (val) {
                      setState(() {
                        value2 = double.parse(myController2.text) * 5;
                      });
                    },
                    decoration: InputDecoration(
                        hintText: '0'
                    ),
                  ),
                ),
                Expanded(
                    child: Center(
                        child: value2 != null
                            ? Text(value2.toStringAsFixed(0))
                            : Text('0')
                    )
                ),
              ],
            ),
            Center(
              child: Text('totalvalues = value1 + value2'),
            ),
            Center(
                child: ElevatedButton(
                    onPressed: () {
                      print('$value1');
                      print('$value2');
                      myController1.clear();
                      myController2.clear();

                    },
                    child: Text('Next screen >')
                )
            )
          ],
        ),
      ),
    );
  }
}
4

2 回答 2

0

对于你的情况,你甚至不TextEditingController

class _Screen1State extends State<Screen1> {

 // final myController1 = TextEditingController();
 // final myController2 = TextEditingController();


  @override
  void dispose() {
   // myController1.dispose();
   // myController2.dispose();
    super.dispose();
  }
  double value1;
  double value2;
  //double totalvalues = value1 + value2;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Screen 1'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            Row(
              children: [
                Expanded(
                  child: TextField(
                    // controller: myController1,
                    keyboardType: TextInputType.number,
                    onChanged: (val) {
                      setState(() {
                        //value1 = double.parse(myController1.text) * 10;
                        value1 = double.parse(val) * 10;
                      });
                    },
                    decoration: InputDecoration(
                        hintText: '0'
                    ),
                  ),
                ),
                Expanded(
                    child: Center(
                        child: value1 != null
                            ? Text(value1.toStringAsFixed(0))
                            : Text('0')
                    )
                ),
              ],
            ),
            Row(
              children: [
                Expanded(
                  child: TextField(
                    // controller: myController2,
                    keyboardType: TextInputType.number,
                    onChanged: (val) {
                      setState(() {
                        // value2 = double.parse(myController2.text) * 5;
                        value2 = double.parse(val) * 5;

                      });
                    },
                    decoration: InputDecoration(
                        hintText: '0'
                    ),
                  ),
                ),
                Expanded(
                    child: Center(
                        child: value2 != null
                            ? Text(value2.toStringAsFixed(0))
                            : Text('0')
                    )
                ),
              ],
            ),
            Center(
              child: Text('totalvalues = ${value1 + value2}'),
            ),
            Center(
                child: ElevatedButton(
                    onPressed: () {
                      print('$value1');
                      print('$value2');
                      // myController1.clear();
                      // myController2.clear();
                      value1=0;
                      value2=0;
                    },
                    child: Text('Next screen >')
                )
            )
          ],
        ),
      ),
    );
  }
}
于 2021-03-23T09:37:48.043 回答
0

totalvalue这里的问题是每次值更改时都必须重新计算。

首先声明变量而不初始化它:

double totalvalues;

其次,在每个onChanged函数上,重新计算值:

对于value1

onChanged: (val) {
    setState(() {
        value1 = double.parse(myController1.text) * 10;
        totalvalues = value1 + value2;
    });
},

对于value2

onChanged: (val) {
    setState(() {
        value2 = double.parse(myController2.text) * 10;
        totalvalues = value1 + value2;
    });
},

然后,每次更改输入并将其显示在 Text 中时都会更新总价值,如下所示:

Text('$value1 + $value2 = $totalvalues')

如果您希望有两个以上的值或计算方式totalvalue可以改变,我建议您将重新计算移至函数。

于 2021-03-23T08:34:04.227 回答