Build: Simple Greeting Screen
This screen shows a greeting message using a StatelessWidget. It displays a centered text that says Hello, Flutter!
Target UI
----------------------- | | | | | Hello, Flutter! | | | | | -----------------------
----------------------- | | | | | Hello, 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: GreetingScreen(), ); } } class GreetingScreen extends StatelessWidget { const GreetingScreen({super.key}); @override Widget build(BuildContext context) { // TODO: Implement the UI here return Container(); } }
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: GreetingScreen(), ); } } class GreetingScreen extends StatelessWidget { const GreetingScreen({super.key}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: const Text('Greeting'), ), body: const Center( child: Text('Hello, Flutter!'), ), ); } }
We created a GreetingScreen class that extends StatelessWidget. This means the UI does not change after it is built. Inside the build method, we return a Scaffold which provides the basic visual layout structure. The AppBar shows the title 'Greeting'. The body uses a Center widget to place the Text widget in the middle of the screen. The text says 'Hello, Flutter!'. This is a simple static screen perfect for learning how to use StatelessWidget.
----------------------- | Greeting | |---------------------| | | | Hello, Flutter! | | | -----------------------
StatelessWidget in Flutter?StatelessWidget builds UI that does not depend on any mutable state and does not change after it is created.StatefulWidget, it does not have a setState() method and does not rebuild on data changes.StatelessWidget in Flutter?build method must override the parent and accept a BuildContext parameter.@override and the method signature Widget build(BuildContext context).StatelessWidget runs?class HelloWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Text('Hello, Flutter!');
}
}Text widget with the string 'Hello, Flutter!'.StatelessWidget code:class MyWidget extends StatelessWidget {
Widget build(BuildContext context) {
return Text('Hi');
}
}@override to indicate it overrides the parent method.Text widget is valid, and no constructor is required if no parameters.StatelessWidget that shows a centered title and a subtitle below it. Which code snippet correctly implements this?Column inside a Center widget, centering the whole column on screen. This matches the requirement.Center, so column aligns top-left. class WelcomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Row(
children: [
Text('Welcome'),
Text('Enjoy your stay')
],
);
}
} uses Row which arranges horizontally. class WelcomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Text('Welcome'),
subtitle: Text('Enjoy your stay'),
);
}
} misuses Scaffold properties.Pass the test and this pattern moves unknown