如何解决Flutter setState函数不起作用:W / IInputConnectionWrapper4965:无效的InputConnection错误上的getTextBeforeCursor
我正在制作身体质量指数应用程序,我需要获得用户输入才能计算bmi公式。因此,我使用setState()函数根据我从用户那里获得的信息来更新身高和体重,但它们不会被更新。你能帮忙吗?
这是我的代码:
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'calculation.dart';
void main() => runApp(MyApp());
/// This is the main application widget.
class MyApp extends StatelessWidget {
static const String _title = 'BMI';
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,home: Scaffold(
appBar: AppBar(title: const Text(_title),centerTitle: true,backgroundColor: Colors.amber[900],),body: Center(
child: MyStatefulWidget(),);
}
}
enum SingingCharacter { lafayette,jefferson }
/// This is the stateful widget that the main application instantiates.
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key key}) : super(key: key);
@override
_MyStatefulWidgetState createState() => _MyStatefulWidgetState();
}
/// This is the private State class that goes with MyStatefulWidget.
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
SingingCharacter _character = SingingCharacter.lafayette;
double height=1;
double weight=1;
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(10.0),child:Scrollbar(
child:SingleChildScrollView(
child:Card(
color: Colors.amber[50],child:Column(
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(0,30,10,10),child: Text("Sex:",style:TextStyle(fontSize: 24,letterSpacing: 1.0)),ListTile(
title: const Text('Female',style:TextStyle(fontSize: 18,letterSpacing: 1.0)
),leading: Radio(
activeColor: Colors.orange,value: SingingCharacter.lafayette,groupValue: _character,onChanged: (SingingCharacter value) {
setState(() {
_character = value;
});
},ListTile(
title: const Text('Male',letterSpacing: 1.0,)
),value: SingingCharacter.jefferson,SizedBox(height: 10.0),Text("Your height:",letterSpacing: 1.0)
),SizedBox(height: 10),Padding(
padding: const EdgeInsets.fromLTRB(30,50,child: TextField(
decoration: new InputDecoration(labelText: "Your height(cm)"),keyboardType: TextInputType.number,inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],// Only numbers can be entered
onSubmitted: (input1){
if(input1 as double>0){
setState(() => height=input1 as double);
print(input1);
// }
},SizedBox(height: 20),Text("Your weight:",letterSpacing: 1.0)
),Padding(
padding: const EdgeInsets.fromLTRB(30,child: new TextField(
decoration: new InputDecoration(labelText: "Your weight(kg)"),inputFormatters: <TextInputFormatter>[
FilteringTextInputFormatter.digitsOnly
],// Only numbers can be entered
onSubmitted: (input2){
if (input2 as double>0){
// print(weight);
setState(() {
return weight=input2 as double;
});
}
},SizedBox(height: 10,RaisedButton(
padding:EdgeInsets.fromLTRB(20,5,20,5),onPressed: (){
return Calculation(height,weight);
},color: Colors.amber[900],child:Text(
'Calculate',style:TextStyle(
color: Colors.white,fontSize: 30,letterSpacing: 2.0,SizedBox(height: 20,Text('Results: $height,$weight'),],);
}
}
这是计算功能:
import 'dart:math';
void Calculation(height,weight){
double value=pow(height,2)/weight;
print(value);
// return value.toString();
}
void Results(value){
if(value<18.5){
print("kilonuz ideal kilonuzdan az.");
}
if(value>25){
if(value>30){
print("kilonuz ideal kilonuzun çok üstünde,sağlığınız risk altında.");
}
else{
print("kilonuz ideal kilonuzdan fazla.");
}
}
else{
print("ideal kilonuzdasınız.");
}
}
这是错误:
W/IInputConnectionWrapper( 4965): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 4965): requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper( 4965): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 4965): getTextBeforeCursor on inactive InputConnection
I/flutter ( 4965): 1.0
W/IInputConnectionWrapper( 4965): getSelectedText on inactive InputConnection
W/IInputConnectionWrapper( 4965): requestCursorAnchorInfo on inactive InputConnection
W/IInputConnectionWrapper( 4965): getTextBeforeCursor on inactive InputConnection
W/IInputConnectionWrapper( 4965): getTextBeforeCursor on inactive InputConnection
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。