#android #flutter
#Android #флаттер
Вопрос:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: BmiMain(),
);
}
}
class BmiMain extends StatefulWidget {
@override
_BmiMainState createState() => _BmiMainState();
}
enum Gender {MAN, WOMEN}
enum Activity{LOW, MEDIUM, HIGH}
class _BmiMainState extends State<BmiMain> {
final _formKey = GlobalKey<FormState>();
final _heightController = TextEditingController();
final _weightController = TextEditingController();
final _ageController = TextEditingController();
Gender _gender = Gender.MAN;
Activity _activity = Activity.LOW;
@override
void dispose() {
_heightController.dispose();
_weightController.dispose();
_ageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Diary')),
body: Container(
padding: const EdgeInsets.all(16.0),
child: Form(
key: _formKey,
child: Column(
children: <Widget>[
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Height',
),
controller: _heightController,
keyboardType: TextInputType.number,
validator: (value) {
if (value.trim().isEmpty) {
return 'Height : ';
}
return null;
},
// )
),
SizedBox(
height: 16.0,
),
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Weight',
),
controller: _weightController,
keyboardType: TextInputType.number,
validator: (value) {
if (value.trim().isEmpty) {
return 'Weight : ';
}
return null;
},
),
SizedBox(
height: 16.0,
),
TextFormField(
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Age',
),
controller: _ageController,
keyboardType: TextInputType.number,
validator: (value) {
if (value.trim().isEmpty) {
return 'Age : ';
}
return null;
},
// )
),
SizedBox(
height: 16.0,
),
RadioListTile(
title: Text('MEN'),
value: Gender.MAN,
groupValue: _gender,
onChanged: (value) {
setState(() {
_gender = value;
});
},
),
RadioListTile(
title: Text('WOMEN'),
value: Gender.WOMEN,
groupValue: _gender,
onChanged: (value) {
setState(() {
_gender = value;
});
},
),
SizedBox(
height: 16.0,
),
RadioListTile(
title: Text('No exercise'),
value: Activity.LOW,
groupValue: _activity,
onChanged: (value) {
setState(() {
_activity = value;
});
},
),
RadioListTile(
title: Text('Exercise three to five times a week'),
value: Activity.MEDIUM,
groupValue: _activity,
onChanged: (value) {
setState(() {
_activity = value;
});
},
),
RadioListTile(
title: Text('Exercise six to seven times a week'),
value: Activity.HIGH,
groupValue: _activity,
onChanged: (value) {
setState(() {
_activity = value;
});
},
),
Container(
margin: const EdgeInsets.only(top: 16.0),
alignment: Alignment.centerRight,
child: RaisedButton(
onPressed: () {
if (_formKey.currentState.validate()) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => BmiResult(
double.parse(_heightController.text.trim()),
double.parse(_weightController.text.trim()),
double.parse(_ageController.text.trim()))),
);
}
},
child: Text('결과'),
),
)
],
),
),
),
);
}
}
class BmiResult extends StatelessWidget {
final double height;
final double weight;
final double age;
BmiResult(this.height, this.weight, this.age);
get basal => null;
get _gender => null;
@override
Widget build(BuildContext context) {
final bmi = weight / ((height / 100) * (height / 100));
// ignore: unrelated_type_equality_checks
if(_gender == Gender.MAN){
final basal = 66.47 (13.75*weight) (5*height)-(6.76*age);
}else if(_gender == Gender.WOMEN){
final basal = 65.51 (9.56*weight) (1.85*height)-(4.68*age);
}
print('bmi : $bmi');
print('basal : $basal');
return Scaffold(
appBar: AppBar(title: Text('Result')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
_calcBmi(bmi),
style: TextStyle(fontSize: 36),
),
SizedBox(
height: 16,
),
_buildIcon(bmi),
Text(
basal,
style: TextStyle(fontSize: 10),
),
],
),
),
);
}
String _calcBmi(double bmi) {
var result = 'underweight';
if (bmi >= 35) {
result = 'high obesity';
} else if (bmi >= 30) {
result = 'two-stage obesity';
} else if (bmi >= 25) {
result = 'first-stage obesity';
} else if (bmi >= 23) {
result = 'overweight';
}else if(bmi>=18.5){
result='normal';
}
return resu<
}
Widget _buildIcon(double bmi) {
if (bmi >= 23) {
return Icon(
Icons.sentiment_very_dissatisfied,
color: Colors.red,
size: 100,
);
} else if (bmi >= 18.5) {
return Icon(
Icons.sentiment_satisfied,
color: Colors.green,
size: 100,
);
} else {
return Icon(
Icons.sentiment_dissatisfied,
color: Colors.orange,
size: 100,
);
}
}
}
Я разрабатываю приложение, которое на практике вычисляет ожирение, базовую скорость метаболизма и активную скорость метаболизма. Я создал виджет radio, который позволяет выбирать между мужчинами и женщинами, но я собираюсь использовать его для расчета основных скоростей метаболизма, и я не знаю, как получить значения radio из оператора if.
Ответ №1:
Вы можете использовать этот метод. Я использую enum
как переменную и присваиваю ей значение
С моей точки зрения, я думаю, что этот метод прост и прост. Я приведу вам пример, который, я надеюсь, принесет вам пользу:
import 'package:flutter/material.dart';
class TestPage extends StatefulWidget {
@override
_TestPageState createState() => _TestPageState();
}
enum RadioGender { male, female }
class _TestPageState extends State<TestPage> {
RadioGender radioGender;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: Icon(radioGender == RadioGender.male
? Icons.radio_button_checked
: Icons.radio_button_off),
onPressed: () {
setState(() {
radioGender = RadioGender.male;
_checkTypeSelect();
});
},
color: Colors.blue,
),
Text(
'Male',
style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),
),
SizedBox(
width: 32.0,
),
IconButton(
icon: Icon(radioGender == RadioGender.female
? Icons.radio_button_checked
: Icons.radio_button_off),
onPressed: () {
setState(() {
radioGender = RadioGender.female;
_checkTypeSelect();
});
},
color: Colors.blue,
),
Text(
'Female',
style: TextStyle(fontSize: 12.0, fontWeight: FontWeight.bold),
),
],
),
),
);
}
_checkTypeSelect() {
if (radioGender == RadioGender.male) {
print('You Select Male Radio');
} else if (radioGender == RadioGender.female) {
print('You Select Female Radio');
}
}
}