I am trying to use center widget in flutter, but due to some unknown reason it is not working, please help me

Viewed 34

I have recently started learning flutter and got stuck at my very first basic hello world application:/
So, basically I am trying to center the entire content on the screen using the center tag, but it is not working, it would be really helpful if someone looks into my problem and helps me:)

Here's the code

import 'package:flutter/material.dart';

void main() {
  runApp(MaterialApp(home: Center(child: Text('Hello World'))));
}

The application screen:

The application screen

1 Answers

Try this:

void main() {
  runApp(MaterialApp(
     home: Scaffold(
        body: Center(
           child: Text('Hello World')
        ),
      ),
    ),
  );
}

Text needs an ancestor of Material to be rendered "correctly" and since Scaffold has one, it'll work fine.

Related