I'm new on flutter, I have 2 screens and I have tried a lot to fix the problems and make this code run, the error shows when I tried to push arguments in the constructor to BmiResultScreen
BmiScreen code (which i need to get the values from):
import 'dart:math';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'bmi_result_screen.dart';
class BmiScreen extends StatefulWidget {
@override
_BmiScreenState createState() => _BmiScreenState();
}
class _BmiScreenState extends State<BmiScreen> {
bool isMale = true;
double height = 120.0;
int age = 0;
double weight = 0.0;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('BMI Calculator'),
),
body: Column(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Row(children: [
Expanded(
child: GestureDetector(
onTap: (){
setState(() {
isMale = true;
});
},
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Image(
image: AssetImage('assets/images/male.png'),
height: 90.0,
width: 90.0,
),
SizedBox(
height: 15.0,
),
Text('MALE',
style: TextStyle(
fontSize: 25.0, fontWeight: FontWeight.bold))
],
),
decoration: BoxDecoration(
color: isMale ? Colors.blue : Colors.grey[400],
borderRadius: BorderRadiusDirectional.circular(10.0)),
),
),
),
SizedBox(
width: 20.0,
),
Expanded(
child: GestureDetector(
onTap: () {
setState(() {
isMale = false;
});
},
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
Image(
image: AssetImage('assets/images/female.png'),
height: 90,
width: 90,
),
SizedBox(
height: 15.0,
),
Text('FEMALE',
style: TextStyle(
fontSize: 25.0, fontWeight: FontWeight.bold))
],
),
decoration: BoxDecoration(
color: !isMale ? Colors.blue : Colors.grey[400],
borderRadius: BorderRadiusDirectional.circular(10.0)),
),
),
),
]),
)),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(
horizontal: 20.0
),
child: Container(
decoration: BoxDecoration(
color: Colors.grey[400],
borderRadius: BorderRadiusDirectional.circular(10.0)
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('HEIGHT',
style:
TextStyle(fontSize: 25.0, fontWeight: FontWeight.bold)),
Row(
crossAxisAlignment: CrossAxisAlignment.baseline,
textBaseline: TextBaseline.alphabetic,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'${height.round()}',
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.w900,
)
),
SizedBox(
width: 5.0,
),
Text(
'cm',
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.w900
),
),
],
),
Slider(
value: height,
max: 220.0,
min: 80.0,
onChanged: (value) {
setState(() {
height = value;
});
},
),
],
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Row(
children: [
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.grey[400],
borderRadius: BorderRadiusDirectional.circular(10.0)
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'WEIGHT',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
)
),
SizedBox(
height: 15.0,
),
Text(
'${weight}',
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.w900,
)
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
onPressed: () {
setState(() {
if(weight>=0.5){
weight = weight -0.5;
}
});
},
child: Icon(
Icons.remove
),
mini: true,
heroTag: '--weight',
),
FloatingActionButton(
onPressed: () {
setState(() {
weight = weight 0.5;
});
},
child: Icon(
Icons.add
),
mini: true,
heroTag: ' weight',
),
],
),
],
),
),
),
SizedBox(
width: 20.0,
),
Expanded(
child: Container(
decoration: BoxDecoration(
color: Colors.grey[400],
borderRadius: BorderRadiusDirectional.circular(10.0)
),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'AGE',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
)
),
SizedBox(
height: 15.0,
),
Text(
'${age}',
style: TextStyle(
fontSize: 50.0,
fontWeight: FontWeight.w900,
)
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FloatingActionButton(
onPressed: () {
setState(() {
if(age >= 1){
--age;
}
});
},
child: Icon(
Icons.remove
),
mini: true,
heroTag: '--age',
),
FloatingActionButton(
onPressed: () {
setState(() {
age;
});
},
child: Icon(
Icons.add
),
mini: true,
heroTag: ' age',
),
],
),
],
),
),
),
],
),
),
),
Container(
color: Colors.blue,
width: double.infinity,
height: 50.0,
child: MaterialButton(
onPressed: () {
double result = weight / pow(height/100,2);
print(result.round());
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BmiResultScreen(
isMale: isMale,
age: age,
result: result.round(),
),
)
);
},
child: const Text('CALCULATE'),
),
),
],
),
);
}
}
BmiResultScreen code(which I'm trying to use the values comes from the last screen):
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
class BmiResultScreen extends StatelessWidget {
final bool isMale;
final int result;
final int age;
BmiResultScreen({
required this.isMale,
required this.age,
required this.result,
});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(
'BMI Result'
),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(
'Gender: $isMale',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Result: ${result.round()}',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
Text(
'Age: $age',
style: TextStyle(
fontSize: 25.0,
fontWeight: FontWeight.bold,
),
),
],
),
)
);
}
}
CodePudding user response:
Problems like this can be fixed by upgrading your flutter version or cleaning.
flutter upgrade --force
flutter clean
delete Podfile
and Podfile.lock
run pub get
flutter run