flutter - 如何防止 Row 占用所有可用宽度?

标签 flutter dart

我的 CustomChip 有一个问题:
我只需要包装卡片以适合内容。
但是,我还有第二个要求:长文本应该溢出淡入淡出。
当我解决第二个问题时,当我添加 Expanded 时,这个问题开始出现。包裹内部Row我不明白为什么内部Row虽然它的 mainAxisSize 似乎也在扩大已经设置为 min enter image description here
这是代码:
屏幕:

import 'package:flutter/material.dart';
import 'package:app/common/custom_chip.dart';

class RowInsideExpanded extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Container(
          decoration: BoxDecoration(
            border: Border.all(
              width: 1.0,
            ),
          ),
          width: 200.0,
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              _buildChip('short'),
              _buildChip('looooooooooooooooooooooongg'),
            ],
          ),
        ),
      ),
    );
  }

  _buildChip(String s) {
    return Row(
      children: [
        Container(
          color: Colors.red,
          width: 15,
          height: 15,
        ),
        Expanded(
          child: CustomChip(
            elevation: 0.0,
            trailing: Container(
              decoration: BoxDecoration(
                color: Colors.grey,
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.close),
            ),
            onTap: () {},
            height: 42.0,
            backgroundColor: Colors.black12,
            title: Padding(
              padding: const EdgeInsets.symmetric(horizontal: 8.0),
              child: Text(
                s,
                softWrap: false,
                overflow: TextOverflow.fade,
                style: TextStyle(color: Colors.black, fontWeight: FontWeight.bold, fontSize: 16.0),
              ),
            ),
          ),
        ),
      ],
    );
  }
}
CustomChip
import 'package:flutter/material.dart';
class CustomChip extends StatelessWidget {
  final Widget leading;
  final Widget trailing;
  final Widget title;
  final double height;
  final double elevation;
  final Color backgroundColor;
  final VoidCallback onTap;
  const CustomChip({
    Key key,
    this.leading,
    this.trailing,
    this.title,
    this.backgroundColor,
    this.height: 30.0,
    this.elevation = 2.0,
    this.onTap,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Card(
      elevation: elevation,
      color: backgroundColor,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(30.0),
      ),
      child: InkWell(
        onTap: onTap,
        child: Container(
          height: height,
          child: Padding(
            padding: const EdgeInsets.only(left: 5.0, right: 5.0),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                leading ?? Container(),
                SizedBox(
                  width: 5.0,
                ),
                Flexible(
                  child: title,
                  fit: FlexFit.loose,
                ),
                SizedBox(
                  width: 5.0,
                ),
                trailing ?? Container(),
              ],
            ),
          ),
        ),
      ),
    );
  }
}

最佳答案

查找“MainAxisSize”属性并设置为“MainAxisSize.min”

关于flutter - 如何防止 Row 占用所有可用宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62901014/

相关文章:

flutter - RenderCustomMultiChildLayoutBox 的每个 child 都必须在其父数据中有一个 ID

dart - 如何在另一个类小部件中获取自定义有状态小部件值?

flutter - 水平居中图像,垂直重复

android - 任务 ':app:processDebugResources' 的 Flutter 执行失败。无法解析配置 ':app:debugCompileClasspath' 的所有依赖项

http - Flutter:如何将 POST 图像 http 发送到 Microsoft Cognitive Services

android - 我想从我的 flutter 应用程序启动 WhatsApp 应用程序

Flutter : Scrolling a ListWheelScrollView, 无法获取选中项的状态

firebase - 将来自 Firebase 的 StartAt 与 Flutter 时间戳一起使用

firebase - Flutter Firestore 理解加入

dart - flutter 错误 : The _ScaffoldLayout custom multichild layout delegate forgot to lay out the following child