Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import 'package:flutter/material.dart';
- void main() {
- runApp(MyApp());
- }
- class MyApp extends StatefulWidget {
- @override
- State<MyApp> createState() => _MyAppState();
- }
- class _MyAppState extends State<MyApp> {
- double _angka = 0;
- double _hasil = 0;
- List<String> _ukuran = ["KM", "M"];
- var _ukuranSemula;
- var _ukuranKonversi;
- @override
- Widget build(BuildContext context) {
- return MaterialApp(
- home: Scaffold(
- appBar: AppBar(
- title: Text("Pengonversi Panjang"),
- ),
- body: Container(
- padding: EdgeInsets.all(50),
- child: Column(
- children: [
- Text("Angka"),
- TextField(
- onChanged: (text) {
- var bacaAngka = double.tryParse(text);
- if (bacaAngka != null) {
- setState(() {
- _angka = bacaAngka;
- });
- } else {
- setState(() {
- _angka = 0;
- });
- }
- },
- ),
- Text("Dari"),
- DropdownButton(
- value: _ukuranSemula,
- items: _ukuran.map((String value) {
- return DropdownMenuItem(
- child: Text(value),
- value: value,
- );
- }).toList(),
- onChanged: (value) {
- setState(() {
- _ukuranSemula = value;
- });
- },
- ),
- Text("Menjadi"),
- DropdownButton(
- value: _ukuranKonversi,
- items: _ukuran.map((String value) {
- return DropdownMenuItem(
- child: Text(value),
- value: value,
- );
- }).toList(),
- onChanged: (value) {
- setState(() {
- _ukuranKonversi = value;
- });
- },
- ),
- ElevatedButton(onPressed: () => convert(), child: Text("UBAH")),
- Text(_hasil.toString()),
- ],
- ),
- ),
- ),
- );
- }
- void convert() {
- setState(() {
- if (_ukuranSemula == "KM" && _ukuranKonversi == "M") {
- _hasil = _angka * 1000;
- } else if (_ukuranSemula == "M" && _ukuranKonversi == "KM") {
- _hasil = _angka / 1000;
- } else {
- _hasil = _angka;
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment