# Basic Interactivity

Creating interactive user interfaces is at the heart of app development. Flutter provides various widgets and mechanisms to handle user interactions. Let's explore how to make your app interactive!

## 1. Handling Button Presses 🖱️

Buttons are the simplest interactive widgets. Here's how you can create a button and handle its press event:

{% tabs %}
{% tab title="Code" %}

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  void handlePress() {
    print('Button Pressed!');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Button Example'),
        ),
        body: Center(
          child: ElevatedButton(
            onPressed: handlePress,
            child: Text('Press me'),
          ),
        ),
      ),
    );
  }
}
```

{% endtab %}

{% tab title="Result" %}

<figure><img src="https://522858097-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOdSyMra5vx5HbARBPAir%2Fuploads%2FWNUDHvs74sD6kxA43tO5%2Fezgif.com-video-to-gif%20(2).gif?alt=media&#x26;token=1085abab-bce9-4607-8b07-2c2af82d395b" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

## 2. Handling Text Input 📝

Text fields allow users to input text. Here’s how you can create a text field and retrieve its value:

{% tabs %}
{% tab title="Code" %}

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final TextEditingController textController = TextEditingController();

  void handleSubmit() {
    print('User Input: ${textController.text}');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Text Input Example'),
        ),
        body: Padding(
          padding: const EdgeInsets.all(16.0),
          child: Column(
            children: [
              TextField(
                controller: textController,
                decoration: InputDecoration(labelText: 'Enter something'),
              ),
              ElevatedButton(
                onPressed: handleSubmit,
                child: Text('Submit'),
              )
            ],
          ),
        ),
      ),
    );
  }
}
```

{% endtab %}

{% tab title="Result" %}

<figure><img src="https://522858097-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FOdSyMra5vx5HbARBPAir%2Fuploads%2Fh7FURIWDJERSHxf8YifL%2Fezgif.com-video-to-gif%20(3).gif?alt=media&#x26;token=810cbe00-971a-46a0-8edc-2178e1d30189" alt=""><figcaption></figcaption></figure>
{% endtab %}
{% endtabs %}

## 3. Using Gesture Detectors 👆

Gesture detectors can identify various gestures, like taps, double-taps, drags, and pinches. Here’s a simple tap handler:\
(**Try it yourself in your IDE!)**

```dart
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  void handleTap() {
    print('Box tapped!');
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Gesture Detector Example'),
        ),
        body: Center(
          child: GestureDetector(
            onTap: handleTap,
            child: Container(
              color: Colors.blue,
              width: 200,
              height: 200,
              child: Center(
                child: Text(
                  'Tap me',
                  style: TextStyle(color: Colors.white),
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}
```

## Assignments 📝

Let’s reinforce your learning with some hands-on tasks:

* [ ] Create a button that changes the color of a container when pressed.
* [ ] Build a text field with a submit button; display the text input below the text field when the submit button is pressed.
* [ ] Create a draggable box on the screen using the `Draggable` widget.
* [ ] Implement a custom gesture detector to handle double taps.

{% hint style="info" %}
By completing these assignments, you’ll gain a practical understanding of handling user interactions in Flutter. The more you experiment, the more comfortable you’ll become with creating interactive interfaces.&#x20;
{% endhint %}

Ready for the next challenge? Dive into Navigation to learn how to move between different screens in your app!
