我需要帮助解决问题。我正在尝试检索一个值,该值是添加多个文本字段值的结果,以便能够在另一个屏幕中使用它(通过 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 >')
)
)
],
),
),
);
}
}