BMI CALCULATOR
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: const BmiApp(),
);
}
}
class BmiApp extends StatefulWidget {
const BmiApp({super.key});
@override
State<BmiApp> createState() => _BmiAppState();
}
class _BmiAppState extends State<BmiApp> {
final nameController = TextEditingController();
final ageController = TextEditingController();
String gender = "Laki-laki";
double weight = 60;
double height = 170;
double bmi = 0;
String category = "-";
String advice = "Masukkan data lalu hitung BMI";
Color color = Colors.white;
void calculateBMI() {
double h = height / 100;
bmi = weight / (h * h);
if (bmi < 18.5) {
category = "KURUS";
color = Colors.lightBlueAccent;
advice =
"🔹 Tingkatkan berat badan dengan:\n"
"• Makan 5–6x sehari\n"
"• Protein & karbohidrat tinggi\n"
"• Latihan beban ringan\n"
"• Tidur cukup";
} else if (bmi < 25) {
category = "IDEAL";
color = Colors.greenAccent;
advice =
"✅ Berat badan ideal!\n"
"• Pertahankan pola makan\n"
"• Olahraga teratur\n"
"• Hidrasi cukup";
} else if (bmi < 30) {
category = "GEMUK";
color = Colors.orangeAccent;
advice =
"⚠️ Turunkan berat badan dengan:\n"
"• Kurangi gula & gorengan\n"
"• Cardio 30 menit\n"
"• Perbanyak sayur & buah";
} else {
category = "OBESITAS";
color = Colors.redAccent;
advice =
"🚨 Perlu perhatian khusus:\n"
"• Atur porsi makan\n"
"• Olahraga bertahap\n"
"• Konsultasi tenaga medis";
}
setState(() {});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
decoration: const BoxDecoration(
gradient: LinearGradient(
colors: [Color(0xFF00B4DB), Color(0xFF0083B0)],
begin: Alignment.topCenter,
end: Alignment.bottomCenter,
),
),
child: SafeArea(
child: SingleChildScrollView(
padding: const EdgeInsets.all(20),
child: Column(
children: [
const Text(
"BMI CALCULATOR",
style: TextStyle(
fontSize: 26,
fontWeight: FontWeight.bold,
color: Colors.white,
letterSpacing: 2,
),
),
const SizedBox(height: 20),
// PROFILE CARD
buildGlassCard(
child: Column(
children: [
buildTextField("Nama", nameController),
buildTextField("Umur", ageController,
type: TextInputType.number),
DropdownButtonFormField<String>(
value: gender,
dropdownColor: Colors.white,
items: const [
DropdownMenuItem(
value: "Laki-laki", child: Text("Laki-laki")),
DropdownMenuItem(
value: "Perempuan", child: Text("Perempuan")),
],
onChanged: (v) => setState(() => gender = v!),
decoration: const InputDecoration(
labelText: "Jenis Kelamin",
),
),
],
),
),
const SizedBox(height: 20),
// SLIDERS
buildGlassCard(
child: Column(
children: [
buildSlider("Berat Badan", "${weight.toInt()} kg", weight,
30, 150, (v) => setState(() => weight = v)),
buildSlider("Tinggi Badan", "${height.toInt()} cm", height,
120, 220, (v) => setState(() => height = v)),
],
),
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: calculateBMI,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.black,
padding: const EdgeInsets.symmetric(
horizontal: 50, vertical: 15),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30)),
),
child: const Text(
"HITUNG BMI",
style: TextStyle(fontSize: 18),
),
),
const SizedBox(height: 25),
// RESULT CARD
buildGlassCard(
child: Column(
children: [
Text(
bmi == 0 ? "-" : bmi.toStringAsFixed(1),
style: TextStyle(
fontSize: 50,
fontWeight: FontWeight.bold,
color: color,
),
),
Text(
category,
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.bold,
color: color,
),
),
const SizedBox(height: 10),
Text(
advice,
textAlign: TextAlign.center,
style: const TextStyle(fontSize: 16),
),
],
),
),
],
),
),
),
),
);
}
// ================= COMPONENTS =================
Widget buildGlassCard({required Widget child}) {
return Container(
width: double.infinity,
padding: const EdgeInsets.all(16),
decoration: BoxDecoration(
color: Colors.white.withOpacity(0.9),
borderRadius: BorderRadius.circular(20),
),
child: child,
);
}
Widget buildTextField(String label, TextEditingController controller,
{TextInputType type = TextInputType.text}) {
return Padding(
padding: const EdgeInsets.only(bottom: 12),
child: TextField(
controller: controller,
keyboardType: type,
decoration: InputDecoration(labelText: label),
),
);
}
Widget buildSlider(String title, String value, double current, double min,
double max, Function(double) onChanged) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("$title ($value)",
style: const TextStyle(fontWeight: FontWeight.bold)),
Slider(
min: min,
max: max,
value: current,
onChanged: onChanged,
),
],
);
}
}
https://z84a06mk84b0.zapp.page/#/

Komentar
Posting Komentar