class BmiCalc extends StatefulWidget {
const BmiCalc({Key? key}) : super(key: key);
@override
State<BmiCalc> createState() => _BmiCalcState();
}
class _BmiCalcState extends State<BmiCalc> {
double _value = 150;
int weight = 60;
int age = 25;
double answer = 10;
String calc = "CALCULATE";
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color.fromARGB(255, 12, 9, 34),
body: SafeArea(
child: SingleChildScrollView(
child: Column(
children: [
Row(
children: [
FemaleBox("MALE", Icons.male),
FemaleBox("FEMALE", Icons.female),
],
),
Column(children: [
Container(
padding: EdgeInsets.all(32),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: _hasBeenPressedFemale
? Color.fromARGB(255, 230, 72, 124)
: colorOfLittleBox,
borderRadius: BorderRadius.circular(15),
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text("HEIGHT",
style: TextStyle(color: Colors.grey, fontSize: 20)),
const SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(_value.toStringAsFixed(0),
style: const TextStyle(
fontSize: 45,
color: Colors.white,
fontWeight: FontWeight.w900)),
const Text(
"cm",
style:
TextStyle(fontSize: 20, color: Colors.grey),
),
],
),
Slider(
min: 100,
max: 230,
thumbColor: Colors.pink,
value: _value,
onChanged: (value) {
setState(() {
_value = value;
});
},
),
],
))
]),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children:<Widget> [
Operation(),
Operation(),
],
),
Padding(
padding: const EdgeInsets.only(bottom: 6),
child: Container(
color: Colors.pink,
height: 55,
width: MediaQuery.of(context).size.width,
child: TextButton(
child: Text(
calc,
style: const TextStyle(
fontSize: 22,
color: Colors.white,
fontWeight: FontWeight.w900),
),
onPressed: () {
calculate();
},
),
),
)
],
),
),
),
);
}
}
Widget Operations() {
int weight = 60;
int age = 25;
return Expanded(
child: Container(
padding: EdgeInsets.all(35),
margin: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Color.fromARGB(255, 27, 28, 48),
borderRadius: BorderRadius.circular(20),
),
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("AGE",
style: TextStyle(
color: Colors.grey, fontSize: 20)),
Text(
age.toString(),
style: TextStyle(
fontSize: 40,
color: Colors.white,
fontWeight: FontWeight.w900),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: 40,
width: 40,
decoration: BoxDecoration(
border: Border.all(),
borderRadius: BorderRadius.circular(45),
color: Color.fromARGB(255, 76, 79, 94),
),
child: IconButton(
iconSize: 23,
icon: Icon(FontAwesomeIcons.minus,
color: Colors.white),
onPressed: () {
setState(() {
age--;
});
},
),
),
SizedBox(
width: 5,
),
Container(
height: 40,
width: 40,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(45),
color: Color.fromARGB(255, 76, 79, 94),
),
child: IconButton(
iconSize: 23,
icon: Icon(FontAwesomeIcons.plus,
color: Colors.white),
onPressed: () {
setState(() {
age ;
});
},
),
),
],
),
],
),
)),
);
}
在 Widget Operations() 中,setState() 出現錯誤:未定義函式“setState”。嘗試匯入定義“setState”的庫,將名稱更正為現有函式的名稱,或者定義一個名為“setState”的函式。有什么問題?我該如何解決?沒有功能一切正常,但在功能上是錯誤的
uj5u.com熱心網友回復:
我看過你的代碼,我注意到你已經在 _BmiCalcState 類之外定義了函式。在類中定義您的操作小部件,它應該可以解決問題。或者,您可以在函式引數中請求構建背景關系,并在呼叫函式時發送背景關系,它也可以解決問題
Widget Operations (BuildContext context){//and your operations here}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/457140.html
下一篇:沒有改變容器的顏色
