Bird
Raised Fist0
Fluttermobile~20 mins

StatelessWidget in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
StatelessWidget Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
What will this StatelessWidget display?
Consider this Flutter code for a simple stateless widget. What text will appear on the screen?
Flutter
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return const Text('Hello Flutter');
  }
}

void main() {
  runApp(const MaterialApp(home: Scaffold(body: Center(child: MyWidget()))));
}
AHello World
BHello Flutter
CError: Missing build method
DBlank screen
Attempts:
2 left
💡 Hint
Look at the Text widget inside the build method.
❓ lifecycle
intermediate
2:00remaining
What happens when a StatelessWidget's data changes?
If you change a variable used inside a StatelessWidget, what happens when you call setState() in the parent widget?
AThe StatelessWidget rebuilds with new data
BThe StatelessWidget updates its internal state automatically
CNothing happens because StatelessWidget cannot rebuild
DThe app crashes with an error
Attempts:
2 left
💡 Hint
StatelessWidgets rebuild when their parent rebuilds.
📝 Syntax
advanced
2:00remaining
Which code snippet correctly defines a StatelessWidget?
Choose the correct Flutter code that defines a valid StatelessWidget named GreetingWidget that shows 'Hi there!'.
A
class GreetingWidget extends StatefulWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hi there!');
  }
}
B
class GreetingWidget extends StatelessWidget {
  Widget build() {
    return Text('Hi there!');
  }
}
C
class GreetingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hi there!');
  }
}
D
class GreetingWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello');
  }
}
Attempts:
2 left
💡 Hint
Check the class inheritance and build method signature.
🔧 Debug
advanced
2:00remaining
Why does this StatelessWidget cause a runtime error?
This code causes an error when run. What is the cause?
Flutter
import 'package:flutter/material.dart';

class MyWidget extends StatelessWidget {
  final String? text;

  const MyWidget({Key? key, required this.text}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Text(text!);
  }
}

void main() {
  runApp(const MaterialApp(
    home: Scaffold(
      body: MyWidget(text: null)
    )
  ));
}
AConstructor missing super.key parameter
BMissing override annotation on build method
CText widget cannot accept nullable strings
DNull check operator used on a null value causing an exception
Attempts:
2 left
💡 Hint
Look at the use of the exclamation mark (!) on a nullable variable.
🧠 Conceptual
expert
2:00remaining
Why choose StatelessWidget over StatefulWidget?
Which reason best explains why you would use a StatelessWidget instead of a StatefulWidget in Flutter?
AWhen the widget does not need to manage or update any internal state
BWhen you want to update the UI based on user interaction
CWhen you need to store data that changes over time
DWhen you want to use animations or timers inside the widget
Attempts:
2 left
💡 Hint
StatelessWidgets are simple and do not hold changing data.

Practice

(1/5)
1. What is the main characteristic of a StatelessWidget in Flutter?
easy
A. It builds UI that never changes after creation.
B. It can hold and update mutable state.
C. It requires a setState() method to update UI.
D. It automatically rebuilds when data changes.

Solution

  1. Step 1: Understand StatelessWidget behavior

    A StatelessWidget builds UI that does not depend on any mutable state and does not change after it is created.
  2. Step 2: Compare with StatefulWidget

    Unlike StatefulWidget, it does not have a setState() method and does not rebuild on data changes.
  3. Final Answer:

    It builds UI that never changes after creation. -> Option A
  4. Quick Check:

    StatelessWidget = UI never changes [OK]
Hint: StatelessWidget means no UI changes after build [OK]
Common Mistakes:
  • Confusing StatelessWidget with StatefulWidget
  • Thinking StatelessWidget can update UI dynamically
  • Assuming setState() is used in StatelessWidget
2. Which of the following is the correct way to define a StatelessWidget in Flutter?
easy
A. class MyWidget extends StatelessWidget { Widget build() { return Text('Hi'); } }
B. class MyWidget extends StatelessWidget { Widget build(BuildContext context) { return Text('Hi'); } }
C. class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Text('Hi'); } }
D. class MyWidget extends StatelessWidget { @override Widget build() { return Text('Hi'); } }

Solution

  1. Step 1: Check method signature

    The build method must override the parent and accept a BuildContext parameter.
  2. Step 2: Verify syntax correctness

    class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Text('Hi'); } } correctly uses @override and the method signature Widget build(BuildContext context).
  3. Final Answer:

    class MyWidget extends StatelessWidget { @override Widget build(BuildContext context) { return Text('Hi'); } } -> Option C
  4. Quick Check:

    build method needs @override and BuildContext [OK]
Hint: build method must override and take BuildContext [OK]
Common Mistakes:
  • Omitting @override annotation
  • Missing BuildContext parameter in build method
  • Incorrect method signature without parameters
3. What will be displayed when this StatelessWidget runs?
class HelloWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Text('Hello, Flutter!');
  }
}
medium
A. A blank screen
B. Text widget showing 'Hello, Flutter!'
C. Error: Missing MaterialApp wrapper
D. Nothing, because StatelessWidget cannot display text

Solution

  1. Step 1: Analyze the build method

    The build method returns a Text widget with the string 'Hello, Flutter!'.
  2. Step 2: Understand StatelessWidget output

    StatelessWidget can display static UI like text. It will show the text on screen.
  3. Final Answer:

    Text widget showing 'Hello, Flutter!' -> Option B
  4. Quick Check:

    StatelessWidget returns Text widget = shows text [OK]
Hint: build returns Text widget, so text appears on screen [OK]
Common Mistakes:
  • Thinking StatelessWidget needs MaterialApp to display text
  • Assuming StatelessWidget cannot show UI
  • Confusing blank screen with error
4. Identify the error in this StatelessWidget code:
class MyWidget extends StatelessWidget {
  Widget build(BuildContext context) {
    return Text('Hi');
  }
}
medium
A. StatelessWidget cannot have a build method
B. build method must return a Container, not Text
C. Missing constructor for MyWidget
D. Missing @override annotation before build method

Solution

  1. Step 1: Check for @override annotation

    The build method should have @override to indicate it overrides the parent method.
  2. Step 2: Verify other parts

    Returning a Text widget is valid, and no constructor is required if no parameters.
  3. Final Answer:

    Missing @override annotation before build method -> Option D
  4. Quick Check:

    build method needs @override annotation [OK]
Hint: Always add @override before build method in widgets [OK]
Common Mistakes:
  • Ignoring missing @override warning
  • Thinking build must return Container only
  • Assuming constructor is mandatory without parameters
5. You want to create a static welcome screen using StatelessWidget that shows a centered title and a subtitle below it. Which code snippet correctly implements this?
hard
A. class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Welcome'), Text('Enjoy your stay') ], ), ); } }
B. class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Welcome'), Text('Enjoy your stay') ], ); } }
C. class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Row( children: [ Text('Welcome'), Text('Enjoy your stay') ], ); } }
D. class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( body: Text('Welcome'), subtitle: Text('Enjoy your stay'), ); } }

Solution

  1. Step 1: Understand layout requirements

    The title and subtitle should be centered vertically and horizontally, stacked vertically.
  2. Step 2: Analyze options

    class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Welcome'), Text('Enjoy your stay') ], ), ); } } wraps a Column inside a Center widget, centering the whole column on screen. This matches the requirement.
  3. Step 3: Check other options

    class WelcomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Column( mainAxisAlignment: MainAxisAlignment.center, children: [ Text('Welcome'), Text('Enjoy your stay') ], ); } } lacks 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.
  4. Final Answer:

    Correctly centers a column with title and subtitle. -> Option A
  5. Quick Check:

    Center + Column = vertical centered texts [OK]
Hint: Use Center widget to center Column vertically and horizontally [OK]
Common Mistakes:
  • Forgetting to wrap Column with Center
  • Using Row instead of Column for vertical layout
  • Misusing Scaffold properties like subtitle
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard