This app shows a text field where the user can type. Below it, the app shows what the user typed in real time.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('Input Example')),
body: InputExample(),
),
);
}
}
class InputExample extends StatefulWidget {
@override
State<InputExample> createState() => _InputExampleState();
}
class _InputExampleState extends State<InputExample> {
String userInput = '';
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(16),
child: Column(
children: [
TextField(
decoration: InputDecoration(labelText: 'Type something'),
onChanged: (text) {
setState(() {
userInput = text;
});
},
),
SizedBox(height: 20),
Text('You typed: $userInput'),
],
),
);
}
}