Fakultas Ilmu Komputer UI

custom_input_form.dart 2.28 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class CustomTextInputForm extends StatelessWidget {
  final String label;
  final bool isPassword;
  final Function onSave;
  final bool isNumberOnly;
  final String prefixText;
  final int maxLength;
  final IconData prefixIcon;
  final String initialValue;

14
15
16
17
18
19
20
21
22
23
  const CustomTextInputForm(
      {Key key,
      @required this.label,
      this.isPassword = false,
      @required this.onSave,
      this.isNumberOnly = false,
      this.prefixText,
      this.maxLength,
      @required this.prefixIcon,
      this.initialValue})
24
25
26
27
28
29
30
31
32
      : assert(label != null),
        assert(onSave != null),
        assert(prefixIcon != null),
        super(key: key);

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      initialValue: initialValue,
33
34
      decoration: customInputDecorationForm(
          this.label, this.prefixText, this.prefixIcon),
35
36
37
38
      obscureText: isPassword,
      onSaved: onSave,
      maxLength: maxLength,
      keyboardType: isNumberOnly ? TextInputType.number : null,
39
40
41
      inputFormatters: isNumberOnly
          ? <TextInputFormatter>[WhitelistingTextInputFormatter.digitsOnly]
          : null,
42
43
44
      validator: (value) {
        if (value.isEmpty) {
          return 'Masukkan ${this.label.toLowerCase()}';
45
        } else if (isPassword && value.length < 8) {
46
          return 'Panjang password minimal 8';
47
        } else if (prefixText == "+62" && value.length <= 8) {
48
49
50
51
52
53
54
55
          return 'Masukkan nomor telepon yang benar';
        }
        return null;
      },
    );
  }
}

56
57
InputDecoration customInputDecorationForm(
    String label, String prefix, IconData prefixIcon) {
58
59
  return InputDecoration(
    contentPadding: const EdgeInsets.symmetric(vertical: 1.5, horizontal: 8),
60
61
62
63
64
65
66
67
68
69
70
    prefixText: prefix,
    labelText: label,
    prefixIcon: Icon(
      prefixIcon,
      color: const Color(0xff3C8DBC),
    ),
    labelStyle: const TextStyle(color: Color(0xff4E7890)),
    fillColor: const Color(0xffF1F1F1),
    filled: true,
    border: const OutlineInputBorder(),
    enabledBorder: const OutlineInputBorder(
71
      borderSide: BorderSide(color: Color(0xff4E7890)),
72
    ),
73
74
75
76
77
    focusedBorder: const OutlineInputBorder(
      borderSide: BorderSide(color: Color(0xff4E7890), width: 2.4),
    ),
  );
}