A Simple Flutter Events Example



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

// Main entry point of the application
void main() {
  runApp(MyApp());
}

// The root widget of the application
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Add Two Numbers', // Title of the application
      theme: ThemeData(
        primarySwatch: Colors.blue, // Set the primary color theme
      ),
      home: MyHomePage(), // The main page of the app
    );
  }
}

// A stateful widget to handle the dynamic behavior of the main page
class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

// The state for MyHomePage
class _MyHomePageState extends State<MyHomePage> {
  // Controllers to handle text input for the two numbers
  final TextEditingController _controller1 = TextEditingController();
  final TextEditingController _controller2 = TextEditingController();
  
  // Variable to store the result of the addition
  String _result = '';

  // Method to add the two numbers and update the result
  void _addNumbers() {
    // Parse the input texts to integers, defaulting to 0 if parsing fails
    final int num1 = int.tryParse(_controller1.text) ?? 0;
    final int num2 = int.tryParse(_controller2.text) ?? 0;
    
    // Add the two numbers
    final int sum = num1 + num2;
    
    // Update the result state
    setState(() {
      _result = 'Result: $sum';
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Add Two Numbers'), // Title in the app bar
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0), // Padding around the content
        child: Column(
          children: <Widget>[
            // Text field for the first number
            TextField(
              controller: _controller1,
              keyboardType: TextInputType.number, // Set the keyboard type to number
              decoration: InputDecoration(
                labelText: 'Enter first number', // Label for the text field
              ),
            ),
            // Text field for the second number
            TextField(
              controller: _controller2,
              keyboardType: TextInputType.number, // Set the keyboard type to number
              decoration: InputDecoration(
                labelText: 'Enter second number', // Label for the text field
              ),
            ),
            SizedBox(height: 20), // Add some space between the text fields and the button
            // Button to trigger the addition
            ElevatedButton(
              onPressed: _addNumbers, // Set the onPressed callback
              child: Text('Add'), // Text on the button
            ),
            SizedBox(height: 20), // Add some space between the button and the result text
            // Text widget to display the result
            Text(
              _result,
              style: TextStyle(fontSize: 20), // Set the text size
            ),
          ],
        ),
      ),
    );
  }
}
```

### Explanation of Comments:

- **General Structure**: Comments explain the purpose of each major section (e.g., `main()`, `MyApp`, `MyHomePage`, etc.).
- **UI Elements**: Comments describe the purpose of each widget (e.g., `TextField`, `ElevatedButton`, `Text`, etc.).
- **Logic**: Comments within the `_addNumbers` method explain how the input is parsed and the sum is calculated.
 







 



Contact us for software training, education or development










 

Post a Comment

0 Comments

Me