简体   繁体   English

如何在 Flutter 行/列中重现 CSS flex-grow 空间分布行为?

[英]How to reproduce CSS flex-grow space distribution behavior in a Flutter Row/Column?

I have a Row that looks like this:我有一个看起来像这样的Row

SizedBox(
    height: 64,
    child: Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
        Expanded(
            child: Container(
            color: Colors.blue,
            child: Text("looooooooong", softWrap: false))),
        Expanded(
            child: Container(
            color: Colors.green, child: Text("short", softWrap: false)))
    ]));

在此处输入图像描述

As you can see the text in the blue container gets cut of .正如您所看到的,蓝色容器中的文本被剪切掉了

Building the same thing with CSS look like this:使用 CSS 构建相同的东西看起来像这样:

 #container { width: 100px; height: 64px; display: flex; align-items: stretch; } #first { flex-grow: 1; background-color: blue; } #second { flex-grow: 1; background-color: green; }
 <div id="container"> <div id="first">looooooooong</div> <div id="second">short</div> </div>

在此处输入图像描述

In this case the green container leaves its unused space over to the blue container and the text in the blue container doesn't get cut of.在这种情况下,绿色容器将未使用的空间留给蓝色容器,蓝色容器中的文本不会被删除。

How am I supposed to achieve the CSS flex-box behavior in Flutter?我应该如何在 Flutter 中实现 CSS flex-box 行为?

If I've understood your right you wanna rebuild the CSS behavior. 如果我理解你的权利,你想重建CSS行为。 You can just leave out the Expanded widgets. 您可以省略扩展的小部件。 Here a short standalone example: 这是一个简短的独立示例:

Flex Grow Demo

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        body: Center(
          child: SizedBox(
            height: 64,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.stretch,
              children: <Widget>[
                Container(
                  color: Colors.blue,
                  child: Text('loooooooooooong'),
                ),
                Container(
                  color: Colors.green,
                  child: Text('short'),
                ),
              ],
            ),
          ),
        ),
      )
    );
  }
}

The Expanded widget with Flex can work for you: 带有 Flex 的扩展小部件可以为您工作:

import 'package:flutter/material.dart';
    
    void main() {
      return runApp(
        MaterialApp(
          home: Scaffold(
            body: Column(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                SizedBox(
                  height: 64.0,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.stretch,
                    children: [
                      Expanded(
                        flex: 8,
                        child: Container(
                          color: Colors.blue,
                          child: const Text('loooooooooooong'),
                        ),
                      ),
                      Expanded(
                        flex: 2,
                        child: Container(
                          color: Colors.green,
                          child: const Text('short'),
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        ),
      );
    }

iPhone 模拟器的屏幕截图显示了 Flutter 扩展的 widget with Flex

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

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