Untitled
unknown
plain_text
21 days ago
5.2 kB
6
Indexable
Never
import 'package:flutter/material.dart'; import 'package:get/get.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return GetMaterialApp( title: 'Flutter Demo', home: NameFormScreen(), ); } } class NameFormController extends GetxController { // TextEditingControllers for the text fields var firstNameController = TextEditingController(); var lastNameController = TextEditingController(); // Variables to track validation states var isFirstNameValid = true.obs; var isLastNameValid = true.obs; // Function to validate the individual fields void validateFirstName() { isFirstNameValid.value = firstNameController.text.isNotEmpty; } void validateLastName() { isLastNameValid.value = lastNameController.text.isNotEmpty; } // Reset the form when 'Reset' button is pressed void resetForm() { firstNameController.clear(); lastNameController.clear(); isFirstNameValid.value = true; isLastNameValid.value = true; } @override void dispose() { firstNameController.dispose(); lastNameController.dispose(); super.dispose(); } } class NameFormScreen extends StatelessWidget { final NameFormController controller = Get.put(NameFormController()); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: const Text('Name Form')), body: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ // First Name TextFormField with manual validation Obx(() => TextFormField( controller: controller.firstNameController, decoration: InputDecoration( labelText: 'First Name', border: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), enabledBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), errorText: controller.isFirstNameValid.value ? null : 'Please enter First Name', errorBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), focusedErrorBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), ), onChanged: (value) { controller.validateFirstName(); }, onFieldSubmitted: (value) { controller.validateFirstName(); }, )), const SizedBox(height: 16), // Last Name TextFormField with manual validation Obx(() => TextFormField( controller: controller.lastNameController, decoration: InputDecoration( labelText: 'Last Name', border: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), enabledBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), focusedBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.black), ), errorText: controller.isLastNameValid.value ? null : 'Please enter Last Name', errorBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), focusedErrorBorder: const OutlineInputBorder( borderSide: BorderSide(color: Colors.red), ), ), onChanged: (value) { controller.validateLastName(); }, onFieldSubmitted: (value) { controller.validateLastName(); }, )), const SizedBox(height: 24), // Buttons: Update and Reset Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, children: [ ElevatedButton( onPressed: () { controller.validateFirstName(); controller.validateLastName(); }, child: const Text('Update'), ), ElevatedButton( onPressed: controller.resetForm, child: const Text('Reset'), ), ], ), ], ), ), ); } }
Leave a Comment