Skip to content

Instantly share code, notes, and snippets.

@roipeker
Created September 8, 2020 03:15
Show Gist options
  • Save roipeker/307d0756dea1d7171a02d78f8e0f1b1e to your computer and use it in GitHub Desktop.
Save roipeker/307d0756dea1d7171a02d78f8e0f1b1e to your computer and use it in GitHub Desktop.
GetX - confirm password demo
import 'package:flutter/material.dart';
import 'package:get/get.dart';
class SampleCompareTextFields extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GetMaterialApp(
home: _Home(),
);
}
}
class FormController extends GetxController {
final pass1 = ''.obs;
final pass2 = ''.obs;
final error1 = RxString(null);
final error2 = RxString(null);
final _showPass = true.obs;
bool get isHidingPassword => _showPass();
void togglePassword() => _showPass.value = !_showPass.value;
@override
void onInit() {
everAll([pass1, pass2], (val) {
error1.value = _validatePass(pass1.value);
error2.value = _validatePass(pass2.value);
});
}
VoidCallback get onConfirm => isValidForm() ? _onConfirm : null;
void _onConfirm() {
print('Sending form data, pass: "$pass1"');
}
String get buttonLabel {
if (_isEmpty) return 'Choose a good password!';
if (_hasError) return 'Please, fix the errors';
return 'Cool, send';
}
bool isValidForm() => !_hasError && !_isEmpty;
bool get _hasError => error1.value != null && error2.value != null;
bool get _isEmpty => pass1().isEmpty && pass2().isEmpty;
String _validatePass(String val) {
if (val.isEmpty) return 'Password cant be empty';
if (val.length < 6) return 'Password is too short';
if (pass1.value != pass2.value) return 'Passwords doesnt match :(';
return null;
}
}
class _Home extends StatelessWidget {
final controller = Get.put(FormController());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('form')),
body: Center(
child: FractionallySizedBox(
widthFactor: .6,
child: Card(
elevation: 12,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Confirm password', style: Get.textTheme.headline6),
Obx(() {
return Column(
children: [
_TextInput('Enter password', controller.pass1,
controller.error1, controller.isHidingPassword, true),
_TextInput(
'Confirm password',
controller.pass2,
controller.error2,
controller.isHidingPassword,
false)
.paddingOnly(top: 12),
OutlineButton(
onPressed: controller.onConfirm,
child: Text(controller.buttonLabel),
).paddingOnly(top: 32),
],
);
}),
],
).paddingAll(24),
),
),
),
);
}
}
class _TextInput extends GetWidget<FormController> {
final RxString value;
final RxString error;
final String label;
final bool showPassButton;
final bool hiddingPass;
_TextInput(this.label, this.value, this.error, this.hiddingPass,
this.showPassButton);
@override
Widget build(BuildContext context) {
return TextField(
onChanged: value,
obscureText: hiddingPass,
decoration: InputDecoration(
errorText: error(),
counterText: ' ',
labelText: label,
suffixIcon: !showPassButton
? null
: GestureDetector(
onTap: controller.togglePassword,
child: Icon(
hiddingPass ? Icons.visibility : Icons.visibility_off,
size: 18,
),
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment