Untitled

 avatar
unknown
plain_text
a year ago
4.1 kB
2
Indexable
import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(
    title: "Quiz Pemrograman Mobile Lanjut",
    home: QuizForm(),
  ));
}

class QuizForm extends StatefulWidget {
  @override
  QuizFormState createState() => QuizFormState();
}

class QuizFormState extends State<QuizForm> {
  final _formKey = GlobalKey<FormState>();
  String username = '', password = '';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Quiz Pemrograman Web Lanjut - M Reza"),
        backgroundColor: Colors.deepOrange,
      ),
      body: Form(
        key: _formKey,
        child: SingleChildScrollView(
          child: Container(
            padding: EdgeInsets.all(20.0),
            child: Column(
              children: [
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    decoration: new InputDecoration(
                      hintText: "contoh: admin1234",
                      labelText: "User Name",
                      icon: Icon(Icons.people),
                      border: OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(5.0)),
                    ),
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'Username tidak boleh kosong';
                      }
                      return null;
                    },
                    onChanged: (value) => {
                      setState(() {
                        username = value;
                      })
                    },
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.all(8.0),
                  child: TextFormField(
                    obscureText: true,
                    decoration: new InputDecoration(
                      labelText: "Password",
                      icon: Icon(Icons.security),
                      border: OutlineInputBorder(
                          borderRadius: new BorderRadius.circular(5.0)),
                    ),
                    validator: (value) {
                      if (value!.isEmpty) {
                        return 'Password tidak boleh kosong';
                      }
                      return null;
                    },
                    onChanged: (value) => {
                      setState(() {
                        password = value;
                      })
                    },
                  ),
                ),
                ElevatedButton(
                  child: Text(
                    "Login",
                    style: TextStyle(color: Colors.white),
                  ),
                  onPressed: () {
                    if (_formKey.currentState!.validate()) {
                      showAlertDialog(context, username, password);
                    }
                  },
                ),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

showAlertDialog(BuildContext context, username, password) {
  // Create button
  Widget closeButton = ElevatedButton(
    child: Text("Tutup"),
    onPressed: () {
      Navigator.of(context).pop();
    },
  );

  // Create AlertDialog
  AlertDialog alertSuccess = AlertDialog(
    title: Text("Selamat datang"),
    content: Text("Selamat Datang, $username"),
    actions: [
      closeButton,
    ],
  );

  AlertDialog alertFailed = AlertDialog(
    title: Text("Gagal Login"),
    content: Text("user name atau password salah"),
    actions: [
      closeButton,
    ],
  );

  // show the dialog
  showDialog(
    context: context,
    builder: (BuildContext context) {
      // username dan password yang benar
      // username: mreza
      // password: 2020081054
      if (username == 'mreza' && password == '2020081054') {
        return alertSuccess;
      }
      return alertFailed;
    },
  );
}