Skip to content

Instantly share code, notes, and snippets.

@diegoveloper
Created March 10, 2023 17:14
Show Gist options
  • Save diegoveloper/cd8e0421ac0592930d8fcc492cf9ff92 to your computer and use it in GitHub Desktop.
Save diegoveloper/cd8e0421ac0592930d8fcc492cf9ff92 to your computer and use it in GitHub Desktop.
ensorcelled-kingdom-0353

ensorcelled-kingdom-0353

Created with <3 with dartpad.dev.

import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({super.key});
static const String _title = 'Flutter Stateful Clicker Counter';
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: _title,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({super.key});
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state.
@override
_MyHomePageState createState() => _MyHomePageState();
}
const errorMobileRequired = 'El celular es obligatorio';
const errorPhoneRequired = 'El teléfono es obligatorio';
const errorCantBeEquals = 'El teléfono y el celular no pueden ser iguales';
class _MyHomePageState extends State<MyHomePage> {
final TextEditingController mobileController = TextEditingController();
final TextEditingController phoneController = TextEditingController();
String? currentErrorPhone;
String? currentErrorMobile;
void validation() {
currentErrorPhone = null;
currentErrorMobile = null;
if(mobileController.text.isEmpty) {
currentErrorMobile = errorMobileRequired;
}
if(phoneController.text.isEmpty) {
currentErrorPhone = errorPhoneRequired;
}
if(mobileController.text.isNotEmpty && mobileController.text == phoneController.text){
currentErrorPhone = errorCantBeEquals;
}
setState((){});
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextFormField(
controller: mobileController,
decoration: InputDecoration(
errorText: currentErrorMobile,
),
onChanged: (_) => validation(),
),
TextFormField(
controller: phoneController,
decoration: InputDecoration(
errorText: currentErrorPhone,
),
onChanged: (_) => validation(),
),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment