Flutter University
  • πŸ‘‹Welcome to Flutter University
  • Learn Flutter
    • πŸš—Basics
      • 🧺Dart Basics
      • πŸš›Setup & Installation
      • 🐣Hello Flutter
      • πŸŒ‰Widgets
      • ⛸️Basic State Management
      • πŸ‡ΎπŸ‡ΉBasic Layout and Styling
      • 🐝Basic Interactivity
      • πŸ›£οΈNavigation
      • πŸͺ„Working with Assets
    • πŸš…Intermediate
      • 🎯Deeper into Dart
      • ⭐More on State Management
      • πŸ“ƒForm Handling
      • πŸ—ΌNetworking
      • πŸŽ‡Persistence
      • πŸ§™β€β™‚οΈAnimations
      • πŸ§ͺTesting
      • πŸ“¦Package Management
    • ✈️Professional
      • πŸŽ“Advanced Animations
      • 🎨Custom Painters
      • 🐼Continuous Integration/Continuous Deployment (CI/CD)
      • 🎭Performance Profiling
      • πŸ”¬Native Integrations
      • 🌍Accessibility and Localization
      • 🀘Understanding Design Patterns
      • πŸ“šFlutter Architecture
        • The Layer Model
        • Reactive User Interfaces
        • Flutter Widgets
        • The Rendering Process
        • Platform Embedders Overview
        • Integrating with Other Code
        • Support for the Web
  • Tutorials
    • 🌈UI
      • 🏚️Clubhouse Clone
      • πŸ”‰Netflix Clone
    • βš”οΈFull Stack
    • ⛓️Blockchain
    • πŸ€–AI/ML
  • Miscellaneous
    • πŸ–₯️100 Days of Flutter
    • 🎨Join Community
Powered by GitBook
On this page
  • 1. Handling Button Presses πŸ–±οΈ
  • 2. Handling Text Input πŸ“
  • 3. Using Gesture Detectors πŸ‘†
  • Assignments πŸ“

Was this helpful?

  1. Learn Flutter
  2. Basics

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:

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'),
          ),
        ),
      ),
    );
  }
}

2. Handling Text Input πŸ“

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

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'),
              )
            ],
          ),
        ),
      ),
    );
  }
}

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!)

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:

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.

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

Last updated 1 year ago

Was this helpful?

πŸš—
🐝