Cant align widgets correctly in Flutter

Issue

I have the following code, but i cant figure out, how to align the username to the left (next to the image) and the class-text to the right of the row. MainAxisAlignment.spaceBetween doesnt do the trick. I tried several different alignments on all the rows and columns but nothing is working. the only way i would get space between the two texts is by adding padding to one of the texts but this is not what want because the usernames have different sizes and the class text wouldnt be alligned to the right.

Container(
  width: 180,
  decoration: BoxDecoration(
    color: const Color(0xffe8d9c3),
    border: Border.all(color: Colors.transparent),
    borderRadius: const BorderRadius.all(
      Radius.circular(4),
    ),
    boxShadow: const [
      BoxShadow(
        color: Colors.black,
        blurRadius: 2.0,
        spreadRadius: 0.0,
        offset: Offset(0, 1),
      ),
    ],
  ),
  child: Row(
    //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
    children: [
      const CircleAvatar(
        backgroundImage: NetworkImage(
          'https://www.woolha.com/media/2020/03/eevee.png',
        ),
      ),
      const SizedBox(
        width: 5,
      ),
      Column(
        //mainAxisAlignment: MainAxisAlignment.spaceBetween,
        //crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              StreamBuilder<DocumentSnapshot>(
                stream: userdoc.snapshots(),
                builder: (BuildContext context,
                    AsyncSnapshot<DocumentSnapshot> snapshot) {
                  return Text(
                    "${snapshot.data?["username"]}",
                    style: const TextStyle(
                      fontSize: 10,
                      color: Colors.black,
                    ),
                  );
                },
              ),
              const Text(
                "Class 8",
                style: TextStyle(
                  fontSize: 10,
                  color: Colors.black,
                ),
              ),
            ],
          ),
          Stack(
            clipBehavior: Clip.none,
            children: const [
              SizedBox(
                width: 120,
                height: 15,
                child: LinearProgressIndicator(
                  value: 0.3,
                  backgroundColor: Color(0xff816840),
                  color: Color(0xffffc518),
                ),
              ),
              Positioned(
                right: 10,
                top: 2,
                child: Text(
                  "2918",
                  style: TextStyle(
                    fontSize: 10,
                    color: Colors.white,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    ],
  ),
)

Solution

enter image description here

import 'package:flutter/material.dart';

void main() {
  runApp(const MaterialApp(
    home: MyApp(),
  ));
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          body: Center(
        child: Container(
          width: 180,
          padding: EdgeInsets.all(5),
          decoration: BoxDecoration(
            color: const Color(0xffe8d9c3),
            border: Border.all(color: Colors.transparent),
            borderRadius: const BorderRadius.all(
              Radius.circular(4),
            ),
            boxShadow: const [
              BoxShadow(
                color: Colors.black,
                blurRadius: 2.0,
                spreadRadius: 0.0,
                offset: Offset(0, 1),
              ),
            ],
          ),
          child: Row(
            // crossAxisAlignment: CrossAxisAlignment.center,
            //mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              const CircleAvatar(
                backgroundImage: NetworkImage(
                  'https://www.woolha.com/media/2020/03/eevee.png',
                ),
              ),
              const SizedBox(
                width: 5,
              ),
              Expanded(
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Row(
                      children: const [
                        Text(
                          "username",
                          style: TextStyle(
                            fontSize: 10,
                            color: Colors.black,
                          ),
                        ),
                        Spacer(),
                        Text(
                          "Class 8",
                          style: TextStyle(
                            fontSize: 10,
                            color: Colors.black,
                          ),
                        ),
                      ],
                    ),
                    Stack(
                      clipBehavior: Clip.none,
                      children: const [
                        SizedBox(
                          height: 15,
                          child: LinearProgressIndicator(
                            value: 0.5,
                            backgroundColor: Color(0xff816840),
                            color: Color(0xffffc518),
                          ),
                        ),
                        Positioned(
                          right: 10,
                          top: 2,
                          child: Text(
                            "2918",
                            style: TextStyle(
                              fontSize: 10,
                              color: Colors.white,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      )),
    );
  }
}

Answered By – Kherel

Answer Checked By – Pedro (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *