Home Mobile How do I disable a Button in Flutter?

How do I disable a Button in Flutter?

0

بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم

Pada materi ini akan dijelaskan bagaimana cara untuk disable 1 button dengan button lainnya

Salah satu penerapan ini digunakan untuk pengguna yang ingin memilih antara kedua pilihan seperti kasus pada kali ini adalah pemasukan dan pengeluaran uang penyimpanan

1. Start a new Flutter project

2. Pilih flutter pplication

3. Konfigurasi project anda

4. Edit main.dart menjadi seperti ini

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(MaterialApp(
    home: MyApp(),
    debugShowCheckedModeBanner: false,
  ));
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  TextEditingController cAlasan = TextEditingController();
  TextEditingController cUang = TextEditingController();
  String id = "", nAlasan, nUang;
  bool button1 = true;
  bool button2 = false;
  String jenisUang = "";
  void _button1() {
    setState(() {
      button1 = false;
      button2 = true;
      jenisUang = "spending";
    });
  }

  void _button2() {
    setState(() {
      button1 = true;
      button2 = false;
      jenisUang = "income";
    });
  }
  final _keyForm = GlobalKey<FormState>();


  checkForm() {
    final form = _keyForm.currentState;
    if (form.validate()) {
      form.save();
    }
  }
  @override
  void initState() {
    // TODO: implement initState
    jenisUang = "income";
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text("Home"),
          backgroundColor: Colors.green,
        ),
        body:
      Padding(
        padding: const EdgeInsets.all(25.0),
        child: Column(
            children: <Widget>[
              Center(
                child: Form(
                  key: _keyForm,
                  child: Wrap(
                    children: <Widget>[
                      Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          MaterialButton(
                            onPressed: button1 ? _button1 : null,
                            child: Text("Income"),
                            color: Colors.greenAccent,
                          ),
                          MaterialButton(
                            onPressed: button2 ? _button2 : null,
                            child: Text("Spending"),
                            color: Colors.greenAccent,
                          ),
                        ],
                      ),
                      TextFormField(
                        controller: cAlasan,
                        validator: (value) {
                          //cek kalau value nya kosong
                          if (value.isEmpty) {
                            return 'Please Input Reason $jenisUang';
                          }
                          if (value.length < 5) {
                            return 'Minimum 5 word';
                          }
                          return null;
                        },
                        onSaved: (value) => nAlasan = cAlasan.text,
                        decoration: InputDecoration(
                          labelText: 'Reason $jenisUang',
                        ),
                      ),
                      TextFormField(
                        controller: cUang,
                        inputFormatters: [FilteringTextInputFormatter.digitsOnly],
                        keyboardType: TextInputType.number,
                        validator: (value) {
                          //cek kalau value nya kosong
                          if (value.isEmpty) {
                            return 'Please Input Money $jenisUang';
                          }
                          if (int.parse(value) < 10000) {
                            return 'Minimum Rp. 10.000';
                          }
                          if (int.parse(value) > 100000000) {
                            return 'Minimum Rp. 100.000.000';
                          }

                          return null;
                        },
                        onSaved: (value) => nUang = cUang.text,
                        decoration: InputDecoration(
                          labelText: 'Money $jenisUang',
                        ),
                      ),
                    ],
                  ),
                ),
              ),
              RaisedButton(
                color: Colors.lightGreen,
                onPressed: () {
                  setState(() {
                    checkForm();
                  });
                },
                child: Text(
                  "Add to $jenisUang",
                  style: TextStyle(color: Colors.white, fontSize: 20),
                ),
              )
            ],
          ),
      ),
      );
  }
}