Complete the code to show the text 'Hello World' in the center of the screen.
import 'package:flutter/material.dart'; void main() { runApp([1]()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: Text('Hello World'), ), ), ); } }
The runApp function needs the root widget of the app, which is MyApp here.
Complete the code to make the app title appear in the app bar.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: [1]('Hello Flutter'), ), body: Center( child: Text('Welcome!'), ), ), ); } }
The title property of AppBar expects a Text widget to show the title string.
Fix the error in the code to correctly display 'Hello Flutter' in the center.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: [1]( child: Text('Hello Flutter'), ), ), ); } }
The body of Scaffold should be wrapped with Center to center the child widget.
Fill both blanks to create a button that shows 'Click me' and prints a message when pressed.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( body: Center( child: ElevatedButton( onPressed: [1]() { print('Button pressed'); }, child: [2]('Click me'), ), ), ), ); } }
The onPressed property expects a function, so use () { ... }. The button label is a Text widget.
Fill all three blanks to create a Scaffold with an AppBar titled 'Home' and a centered text 'Welcome'.
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( title: [1]('Home'), ), body: [2]( child: [3]('Welcome'), ), ), ); } }
The AppBar title uses a Text widget. The body uses Center to center its child, which is another Text widget.