Build: Simple Person Info
This screen shows a person's name and age using a class and object in Flutter.
Target UI
--------------------- | Simple Person Info | --------------------- | Name: | | Age: | ---------------------
--------------------- | Simple Person Info | --------------------- | Name: | | Age: | ---------------------
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { // TODO: Create Person class and object // TODO: Display person's name and age return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Simple Person Info')), body: const Center( child: Text(''), // TODO: Replace with person info ), ), ); } }
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class Person { final String name; final int age; Person(this.name, this.age); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { final person = Person('Alice', 30); return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Simple Person Info')), body: Center( child: Text('Name: ${person.name}\nAge: ${person.age}', style: const TextStyle(fontSize: 24), textAlign: TextAlign.center, ), ), ), ); } }
We created a Person class with two properties: name and age. Then, inside the MyApp widget, we made an object of Person named person with sample data.
We used string interpolation to show the person's name and age inside a Text widget. The text is centered and has a bigger font size for easy reading.
--------------------- | Simple Person Info | --------------------- | Name: Alice | | Age: 30 | ---------------------
Car in Dart?class followed by the class name and curly braces.class Car { }. Others use incorrect keywords or syntax.class Dog {
String name;
Dog(this.name);
void bark() {
print('Woof! My name is $name');
}
}
void main() {
var dog = Dog('Buddy');
dog.bark();
}Dog has a constructor that sets the name property to 'Buddy'.bark() method prints 'Woof! My name is ' followed by the dog's name, which is 'Buddy'.class Person {
String name
int age;
Person(this.name, this.age);
}String name is missing a semicolon at the end, which is required in Dart.Book that stores a title and author, and has a method describe() that returns a string like "Title by Author". Which code correctly implements this?describe() should return a string, so its return type must be String and it must use return.Pass the test and this pattern moves unknown