Flutter:计算多个值的问题​不同文本字段的



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

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 >')
)
)
],
),
),
);
}
}

这里的问题是每次值更改时都必须重新计算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('$value1 + $value2 = $totalvalues')

如果您希望有两个以上的值,或者totalvalue的计算方式可能会更改,我建议您将重新计算移到一个函数中。

对于您的情况,您甚至没有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 >')
)
)
],
),
),
);
}
}

相关内容

  • 没有找到相关文章

最新更新