TextField in Flutter

18-Sep-2024

Learn how to implement TextField in Flutter for user text input with various configurations

The TextField widget is used to create an input field where the user can enter text.
Users can enter alphanumeric characters, and you can customize them using various properties to control their appearance and behavior.

There Are Some Common Attributes for TextField

Here's a basic example of how you can use the TextField widget:

File open lib->main.dart file −



import 'package:flutter/material.dart';

void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
title: Text('Baseline Example',
style:TextStyle(fontSize: 20,color: Colors.white)),
),
body: MyApp(),
),
)
);
}

class MyApp extends StatelessWidget {

TextEditingController _textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return
Column(
children: [
Padding(
padding: EdgeInsets.all(16.0),
child: TextField(
controller: _textEditingController,
decoration: InputDecoration(
labelText: 'Enter your text',
),
),
),
SizedBox(height: 16.0),
ElevatedButton(
onPressed: () {
// Access the text using the controller
String enteredText = _textEditingController.text;
print('Entered Text: $enteredText');
},
child: Text('Submit'),
),
],
);

}
}






Open Device Manager, run the emulator, and then run the application. Next, check the working output and check the output you declared in your code.


Output




Comments