简体   繁体   English

无法在 Flutter 中正确对齐小部件

[英]Cant align widgets correctly in Flutter

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. MainAxisAlignment.spaceBetween 不能解决问题。 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.我在两个文本之间获得空间的唯一方法是向其中一个文本添加填充,但这不是想要的,因为用户名具有不同的大小,并且 class 文本不会向右对齐。

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,
                  ),
                ),
              ),
            ],
          ),
        ],
      ),
    ],
  ),
)

you can use Spacer() widget between them您可以在它们之间使用Spacer()小部件

在此处输入图像描述

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,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ],
                ),
              ),
            ],
          ),
        ),
      )),
    );
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM