EN VI

Adding Column as child to Container in flutter is not working. How to solve it?

2024-03-12 21:00:09
Adding Column as child to Container in flutter is not working. How to solve it?

I have this code for contacts page which renders fine:

import 'package:flutter/material.dart';

class ContactsPage extends StatelessWidget {
  const ContactsPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Center(
        child: Text("Contacts Page"),
      ),
    );
  }
}

But when I change the container contents with the following nothing gets displayed in the screen:

return Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RichText(
              text: TextSpan(children: [
                TextSpan(text: "Text 2"),
              ]),
            ),
          ],
        ),
      ),
    );

UPDATE 01: The ContactsPage object is assigned to body in Scaffold of other class extending stateful widget.

I was looking at this Youtube tutorial and tried to create an about page, but failed. Also this help.

Pastebin links:

  1. main.dart
  2. contacts.dart (first code in the question above)
  3. dashboard.dart and events.dart are very similar to contacts.dart only minor changes.
  4. about.dart which I'm trying to modify to add information into.

What am I coding wrong here?

Solution:

Try wrapping the Container in Scaffold.

@override
  Widget build(BuildContext context) {
    return Scaffold(
    body: Container(
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            RichText(
              text: TextSpan(
                style: TextStyle(color: Colors.black),
                children: [
                   TextSpan(text: "Text 2"),
                 ],
               ),
            ),
          ],
        ),
      ),
    ),
 );
Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login