Fakultas Ilmu Komputer UI

custom_input_form.dart 2.69 KB
Newer Older
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
1
2
3
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

4
class CustomTextInputForm extends StatefulWidget {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
5
6
7
8
9
10
11
12
  final String label;
  final bool isPassword;
  final Function onSave;
  final bool isNumberOnly;
  final String prefixText;
  final int maxLength;
  final IconData prefixIcon;
  final String initialValue;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
13
14
  final Function(String) onSubmit;
  final TextInputAction textInputAction;
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
15
16
17

  const CustomTextInputForm(
      {Key key,
18
19
20
21
22
23
24
25
26
27
        @required this.label,
        this.isPassword = false,
        @required this.onSave,
        this.isNumberOnly = false,
        this.prefixText,
        this.maxLength,
        @required this.prefixIcon,
        this.initialValue,
        this.onSubmit,
        this.textInputAction})
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
28
29
30
31
32
      : assert(label != null),
        assert(onSave != null),
        assert(prefixIcon != null),
        super(key: key);

33
34
35
36
37
38
39
40
41
42
43
44
45
  @override
  _CustomTextInputFormState createState() => _CustomTextInputFormState();
}

class _CustomTextInputFormState extends State<CustomTextInputForm> {
  bool _obscureText = true;

  void togglePassword() {
    setState(() {
      _obscureText = !_obscureText;
    });
  }

Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
46
47
48
  @override
  Widget build(BuildContext context) {
    return TextFormField(
49
      initialValue: widget.initialValue,
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
50
      decoration:
51
52
53
54
55
56
57
58
      customInputDecorationForm(context, widget.label, widget.prefixText, widget.prefixIcon),
      obscureText: widget.isPassword == true ? _obscureText : false,
      onFieldSubmitted: widget.onSubmit,
      onSaved: widget.onSave,
      maxLength: widget.maxLength,
      textInputAction: widget.textInputAction,
      keyboardType: widget.isNumberOnly ? TextInputType.number : null,
      inputFormatters: widget.isNumberOnly
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
59
60
61
62
          ? <TextInputFormatter>[WhitelistingTextInputFormatter.digitsOnly]
          : null,
      validator: (value) {
        if (value.isEmpty) {
63
64
          return 'Masukkan ${widget.label.toLowerCase()}';
        } else if (widget.isPassword && value.length < 8) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
65
          return 'Panjang password minimal 8';
66
        } else if (widget.prefixText == '+62' && value.length <= 8) {
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
67
68
69
70
71
72
73
          return 'Masukkan nomor telepon yang benar';
        }
        return null;
      },
    );
  }

74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  InputDecoration customInputDecorationForm(
      BuildContext context, String label, String prefix, IconData prefixIcon) {

    return InputDecoration(
      prefixText: prefix,
      labelText: label,
      prefixIcon: Icon(
        prefixIcon,
        color: Theme.of(context).primaryColor,
      ),
      suffixIcon: label == 'Password' ? GestureDetector(
        onTap: togglePassword,
        child: Icon(
          Icons.remove_red_eye,
          color: Theme.of(context).primaryColor,
        ),
      ) : null,
    );
  }
Michael Wiryadinata Halim's avatar
Michael Wiryadinata Halim committed
93
}