Flutter is a UI toolkit developed by Google, a powerful framework that allows you to create iOS, Android, web, and desktop applications from a single codebase. In this course, we will delve deeply into how to create classes in Flutter. By effectively utilizing classes, you can enhance code reusability and clarify code structure.
1. Basic Concept of Class
A class is a core concept of object-oriented programming, allowing us to define objects with specific attributes and functionalities through the framework of a class. The way to create a class in Flutter is similar to other object-oriented languages.
1.1 Declaring a Class
To declare a class, the class
keyword is used. Here is a simple example.
class Animal {
String name;
Animal(this.name);
void speak() {
print('$name makes a sound!');
}
}
In the example above, we define a class named Animal
. This class has an attribute called name
and a method called speak
.
1.2 Constructor
A constructor is a special method that is called when creating an instance of a class. The constructor must have the same name as the class and supports both the default and named constructors.
class Animal {
String name;
Animal(this.name); // Default constructor
Animal.named(this.name); // Named constructor
}
2. Class Properties and Methods
A class defines data and behavior through properties (fields) and methods. Properties are the data that instances of the class possess, while methods define operations that can be applied to this data.
2.1 Defining Properties
When defining properties, you can specify data types and set initial values.
class Person {
String name;
int age;
Person(this.name, this.age);
}
Here, the Person
class has two properties: name
and age
.
2.2 Defining Methods
Methods define the behavior of a class. Methods can take data as input and return results.
class Person {
String name;
int age;
Person(this.name, this.age);
String introduce() {
return 'Hello, my name is $name and I am $age years old.';
}
}
2.3 Getters and Setters
Getters and setters provide a way to access properties of a class. They are used for data encapsulation and security purposes.
class Person {
String _name; // private field
Person(this._name);
String get name => _name; // Getter
set name(String newName) => _name = newName; // Setter
}
3. Class Inheritance
A class can inherit from another class, which enhances code reusability. Through inheritance, a child class can use the properties and methods of a parent class.
3.1 Example of Inheritance
class Animal {
void eat() {
print('Eating grains.');
}
}
class Dog extends Animal {
void bark() {
print('Woof!');
}
}
In this example, the Dog
class inherits from the Animal
class and can use the eat()
method.
3.2 Polymorphism
Polymorphism is the concept that allows the same interface to be used for different data types. Leveraging polymorphism in Flutter enables handling various forms of objects.
class Cat extends Animal {
void bark() {
print('Meow!');
}
}
void makeSound(Animal animal) {
animal.bark();
}
void main() {
Dog dog = Dog();
Cat cat = Cat();
makeSound(dog); // Woof!
makeSound(cat); // Error occurs
}
4. Using Mixins
Mixins allow a class to reuse methods from other classes, enabling code reuse in a way that differs from inheritance.
mixin Flyable {
void fly() {
print('I am flying!');
}
}
class Bird with Flyable {
void chirp() {
print('Chirp!');
}
}
In the example above, the Bird
class uses the Flyable
mixin to access the fly()
method.
5. Summary and Example
Classes are essential elements of object-oriented programming. Through what we’ve learned in this course, you will be able to effectively utilize classes in Flutter applications. Here is a simple example that summarizes what we learned.
void main() {
Person person = Person('John Doe', 25);
print(person.introduce());
Dog dog = Dog();
dog.eat();
dog.bark();
Cat cat = Cat();
cat.eat();
cat.bark();
}
In this Flutter course, we thoroughly explored the basic concepts of classes, constructors, methods, inheritance, and mixins. Based on this knowledge, try to build your Flutter applications. You will continue to learn more in the future.