We create a simple app to learn how Flutter shows things on the screen. It helps us understand the basics of making mobile apps.
First Flutter app (Hello World)
Introduction
Syntax
Flutter
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: Center( child: Text('Hello World'), ), ), ); } }
runApp() starts the app and shows the widget you give it.
MaterialApp sets up basic app design and navigation.
Examples
Flutter
Text('Hello World')Flutter
Center(child: Text('Hello World'))Flutter
Scaffold(body: Center(child: Text('Hello World')))Sample App
This program shows the text "Hello World" centered on a white screen. It uses MaterialApp for app basics, Scaffold for page layout, and Center to put the text in the middle.
Flutter
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { return const MaterialApp( home: Scaffold( body: Center( child: Text('Hello World'), ), ), ); } }
Important Notes
Use const for widgets that don't change to make your app faster.
Every Flutter app starts with runApp() to show the first widget.
Widgets are the building blocks of Flutter apps, like LEGO pieces.
Summary
Flutter apps start with runApp() and a widget tree.
Use MaterialApp and Scaffold for basic app structure.
Text is shown using the Text widget inside layout widgets like Center.
Practice
1. What is the main function used to start a Flutter app?
easy
Solution
Step 1: Identify the Flutter app entry point
Flutter apps always start by calling the runApp() function to load the widget tree.Step 2: Confirm the correct function name
The function runApp() is the official way to start the app and display widgets on screen.Final Answer:
runApp() -> Option BQuick Check:
App start function = runApp() [OK]
Hint: Remember Flutter apps always start with runApp() [OK]
Common Mistakes:
- Using startApp() instead of runApp()
- Confusing with main() function
- Trying to use launchApp()
2. Which of the following is the correct way to display the text 'Hello World' centered on the screen in Flutter?
easy
Solution
Step 1: Understand widget nesting in Flutter
Widgets are nested by passing child widgets as parameters, so Center(child: Text('Hello World')) is correct.Step 2: Check syntax correctness
Options A, B and C are invalid: A misses the named parameter 'child', B and C use invalid methods.Final Answer:
Center(child: Text('Hello World')) -> Option CQuick Check:
Center widget uses child: Text(...) [OK]
Hint: Use Center(child: Text('...')) to center text [OK]
Common Mistakes:
- Omitting 'child:' label
- Using invalid method calls on Text
- Trying to call center() on Text widget
3. What will be displayed on the screen when running this Flutter code?
void main() {
runApp(MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello Flutter'),
),
),
));
}medium
Solution
Step 1: Analyze widget tree structure
The MaterialApp provides app structure, Scaffold gives a page layout, Center centers its child, and Text shows the string.Step 2: Determine the visible output
The Text widget with 'Hello Flutter' is centered on the screen inside Scaffold's body.Final Answer:
Centered text 'Hello Flutter' -> Option AQuick Check:
Center widget centers Text = centered text [OK]
Hint: Center widget centers its child widget on screen [OK]
Common Mistakes:
- Thinking Scaffold body is empty
- Assuming default alignment is top-left
- Expecting error without syntax issues
4. Identify the error in this Flutter code snippet:
void main() {
runApp(MaterialApp(
home: Scaffold(
body: Center(
child: Text('Hello World')
),
),
));
}medium
Solution
Step 1: Check widget parameter separators
In Flutter, Center has only one named parameter (child: Text(...)); no comma is needed after a single parameter.Step 2: Verify parentheses and semicolons
Parentheses are balanced and semicolons are correctly placed after runApp call. Code runs fine.Final Answer:
No error, code is correct -> Option AQuick Check:
Single named param needs no trailing comma [OK]
Hint: No comma needed for single widget param [OK]
Common Mistakes:
- Thinking comma is required after single child
- Confusing semicolon with comma
- Ignoring Dart's flexible comma syntax
5. You want to create a Flutter app that shows 'Hello World' in the center with a blue background. Which code snippet correctly achieves this?
hard
Solution
Step 1: Understand Scaffold background color property
Scaffold uses backgroundColor property to set the screen background color. It must be inside Scaffold's constructor.Step 2: Check correct color usage
runApp(MaterialApp(home: Scaffold(backgroundColor: Colors.blue, body: Center(child: Text('Hello World'))))); correctly uses Colors.blue inside Scaffold. A places backgroundColor on MaterialApp (invalid). C uses blueGrey (greyish), D uses blueAccent (bright accent).Final Answer:
runApp(MaterialApp(home: Scaffold(backgroundColor: Colors.blue, body: Center(child: Text('Hello World'))))); -> Option DQuick Check:
Scaffold backgroundColor = Colors.blue [OK]
Hint: Set backgroundColor inside Scaffold, not MaterialApp [OK]
Common Mistakes:
- Putting backgroundColor in MaterialApp
- Using invalid color names
- Forgetting to wrap Text in Center
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
