import 'package:flutter/material.dart';
class UserInputCaptureScreen extends StatefulWidget {
@override
State<UserInputCaptureScreen> createState() => _UserInputCaptureScreenState();
}
class _UserInputCaptureScreenState extends State<UserInputCaptureScreen> {
final TextEditingController _controller = TextEditingController();
String _submittedName = '';
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('User Input Capture')),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('Enter your name:'),
TextField(
controller: _controller,
decoration: InputDecoration(
border: OutlineInputBorder(),
hintText: 'Type your name here',
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: () {
setState(() {
_submittedName = _controller.text;
});
},
child: Text('Submit'),
),
SizedBox(height: 20),
Text('Your name is:'),
Text(
_submittedName,
style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
),
],
),
),
);
}
}
This screen uses a TextField widget to let the user type their name. The TextEditingController captures what the user types. When the user taps the Submit button, the app reads the current text from the controller and saves it in a variable _submittedName. Then it updates the screen to show the entered name below. This shows how input widgets capture user data and how the app can use that data to update the UI.
We use a StatefulWidget because the screen changes when the user submits their name. The setState() call tells Flutter to redraw the screen with the new data.