简体   繁体   English

即使宽度设置为 50,容器也会占用整个宽度

[英]Container takes entire width even though the width is set to 50

I'm observing a very strange behavior.我正在观察一种非常奇怪的行为。 I have set the width of the Container to 50 even then it's taking the entire width of the screen.我已经将Containerwidth设置为50即使它占用了屏幕的整个width

import 'package:flutter/material.dart';

class AppDrawerNew extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      width: 50,
      color: Colors.green,
    );
  }
}

I tried wrapping the Container inside a SizedBox as well.我也尝试将Container包裹在SizedBox中。 Even then it occupies the entire width of the screen.即便如此,它也占据了整个屏幕的width

class AppDrawerNew extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: 50.0,
      child: Container(
        color: Colors.green,
      ),
    );
  }
}

I'm not sure what exactly I'm doing wrong.我不确定我到底做错了什么。 Could anyone please help me in fixing this issue?有人可以帮我解决这个问题吗? Thanks in advance.提前致谢。

import 'package:flutter/material.dart';

void main() {
  runApp(AppDrawerNew());
}
class AppDrawerNew extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          body: Container(
            width: 50.0,
            height: 100.0,
            color: Colors.green,
          ),
      ),
    );
  }
}

the container must be a child of MaterialApp and Scaffold容器必须是 MaterialApp 和 Scaffold 的孩子

Give this a try 🙂试试这个🙂

Container(
      constraints: BoxConstraints(minWidth: 50, maxWidth: 50),
      color: Colors.green,
)

Study about constraints concept, it is more important!研究约束概念,更重要!

In documentation is the best for this: https://flutter.dev/docs/development/ui/layout/constraints在文档中是最好的: https : //flutter.dev/docs/development/ui/layout/constraints

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

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