Let’s talk about Handling User Input in Flutter
Flutter makes building interactive, user-friendly apps seamless by providing a powerful toolkit for handling user input. In this blog, we’ll explore how to work with essential widgets like TextFields, Buttons, and Gestures, allowing you to create dynamic apps that respond to user actions. We’ll cover how to:
- Collect input from users using
TextField
. - Trigger actions with buttons.
- Handle gestures for more interactive UIs.

By the end of this post, you’ll have a comprehensive understanding of managing user input in Flutter apps.


1. Handling Text Input with TextFields
The TextField
widget is used to capture text input from users. It’s commonly found in forms, search fields, and anywhere else you need the user to type information.
Example: Basic TextField
TextField(
decoration: InputDecoration(
labelText: 'Enter your name',
border: OutlineInputBorder(),
),
)
In this simple example, we create a TextField
with a label that says “Enter your name.” The InputDecoration
helps us style the field with a label and border.
Using a Controller
To handle the input, we typically use a TextEditingController
, which captures the text entered by the user.
TextEditingController _controller = TextEditingController();
TextField(
controller: _controller,
decoration: InputDecoration(
labelText: 'Enter your name',
),
)
The _controller.text
stores the text input, which we can then use in our app, such as displaying it or passing it to another widget.
Example: Displaying Input Text
String _name = '';
TextField(
controller: _controller,
decoration: InputDecoration(
labelText: 'Enter your name',
),
),
Text('Hello, $_name'),
After the user inputs their name, we can display it by assigning the value from the controller to a variable (_name
) and updating it dynamically.
2. Handling Buttons in Flutter
Buttons are a key way to let users interact with the app. Flutter provides several types of buttons, including ElevatedButton
, TextButton
, and OutlinedButton
.
Example: Basic Elevated Button
ElevatedButton(
onPressed: () {
print('Button pressed');
},
child: Text('Submit'),
),
The onPressed
callback allows you to define what happens when the button is clicked. You can trigger any functionality here, from navigating to a new screen to changing the UI.
Example: Submitting a Name
We can extend our TextField
example by adding a button that submits the name entered by the user.
void _submitName() {
setState(() {
_name = _controller.text;
});
}
ElevatedButton(
onPressed: _submitName,
child: Text('Submit'),
),
In this case, the onPressed
function assigns the value from the TextField
to the _name
variable and calls setState
to update the UI.
3. Handling Gestures in Flutter
Flutter provides the GestureDetector
widget, allowing apps to respond to touch interactions beyond simple button presses. With it, you can detect taps, swipes, pinches, and other gestures.
Example: Detecting a Tap
GestureDetector(
onTap: () {
print('Container tapped');
},
child: Container(
height: 100,
width: 100,
color: Colors.blue,
child: Center(child: Text('Tap me')),
),
),
In this example, when the user taps the container, it triggers the onTap
callback, printing “Container tapped” to the console.
Handling Swipe Gestures
Swipe gestures are commonly used in list-based UIs or for navigation. The GestureDetector
widget also supports swipes.
GestureDetector(
onPanUpdate: (details) {
if (details.delta.dx > 0) {
print('Swiped right');
} else {
print('Swiped left');
}
},
child: Container(
height: 100,
width: 100,
color: Colors.red,
child: Center(child: Text('Swipe me')),
),
),
This code listens for a horizontal swipe and prints whether the user swiped left or right based on the dx
value.
Combining Gestures with Other Widgets
Gesture handling can be combined with any other widget to make your UI elements more interactive.
GestureDetector(
onDoubleTap: () {
print('Double tapped!');
},
child: Icon(Icons.favorite, color: Colors.pink, size: 50),
),
Here, the onDoubleTap
event triggers when the user double-taps on the icon.
Putting It All Together: A Simple User Input App
Let’s combine everything we’ve learned so far—TextFields, Buttons, and Gesture detection—to build a simple app that collects and displays the user’s name.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UserInputScreen(),
);
}
}
class UserInputScreen extends StatefulWidget {
@override
_UserInputScreenState createState() => _UserInputScreenState();
}
class _UserInputScreenState extends State<UserInputScreen> {
TextEditingController _controller = TextEditingController();
String _name = '';
void _submitName() {
setState(() {
_name = _controller.text;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('User Input Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: _controller,
decoration: InputDecoration(
labelText: 'Enter your name',
border: OutlineInputBorder(),
),
),
SizedBox(height: 20),
ElevatedButton(
onPressed: _submitName,
child: Text('Submit'),
),
SizedBox(height: 20),
Text('Hello, $_name!'),
SizedBox(height: 20),
GestureDetector(
onDoubleTap: () {
print('Double tapped!');
},
child: Icon(Icons.favorite, color: Colors.pink, size: 50),
),
],
),
),
);
}
}
In this example:
- The user enters their name in a
TextField
. - Upon pressing the “Submit” button, the name is displayed.
- A
GestureDetector
listens for a double tap on a favorite icon and prints a message to the console.
Conclusion
In this blog, we covered how to handle user input in Flutter using TextField
for text input, buttons for user interaction, and gestures for more advanced interactions. Mastering these input methods is key to building responsive, interactive Flutter apps. With these skills, you can now start creating dynamic, user-friendly applications.
Explore Other Flutter Topics…
- Introduction to Flutter and Dart
- Why choose Flutter
- Installing Flutter On Your Windows Mac And Linux System
- Your first Flutter app
- Flutter project structure
- Building blocks of Flutter
- Stateful vs. Stateless Widgets Explained
- Flutter layout system
- Flutter text widget
- Creating Buttons in Flutter: ElevatedButton, TextButton, and IconButton
- Handling User Input with Flutter Forms
- Container class in Flutter
- Flutter Navigation
- Flutter – Pass Data One Screen To Another Screen
- Managing Device Orientation in Flutter
- Stateful widget lifecycle in Flutter
- Future of Flutter
- Flutter Themes
- Flutter Animations
- Flutter AppBar Customization
- ListView in Flutter
- Flutter GridView
- Flutter Expanded Widget
- Flutter BottomNavigation Bar
- Floating Action Button
- Drawer Widgets in Flutter
- Form Validation in Flutter
- Flutter TextField
- Adding AdMob ads to a Flutter app
- Building Flutter Web & Desktop Applications
- What is Async and Await in Flutter
- HTTP requests in Flutter
- Parsing JSON in Flutter
- Tinder-Style Swipe Cards in Flutter
- Flutter Tic Tac Toe Game Tutorial
- Flutter Login UI Tutorial
- Flutter Card Widget Tutorial
- Flutter music player app tutorial
- Flutter introduction screens
- Shared Preferences in Flutter
- SQLite Database in Flutter
- Firebase Authentication in Flutter
- Firebase Firestore in Flutter
- Push Notifications in Flutter
- Handling File Uploads in Flutter
- Responsive Design in Flutter
- Provider in Flutter
- Riverpod in Flutter
- Flutter BLoC Pattern Tutorial