Note:刚刚意识到容器和背景是相似的色调,很抱歉!

I have the following text fields:enter image description here

我试图将"标题"和"描述"文本字段固定在容器的顶部,然而,每当我的描述展开时,我的"描述"文本小部件就会移动到容器的中心,而不是保持在顶部.

以下是我的代码:

Container(
          decoration: BoxDecoration(
            color: Theme.of(context).primaryColorDark,
            borderRadius: BorderRadius.circular(9),
          ),
          child: Column(
            children: [
              Padding(
                padding:
                    const EdgeInsets.symmetric(horizontal: 15.0, vertical: 5),
                child: Row(
                  // crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(
                      // element name
                      name,
                      style: const TextStyle(
                        fontSize: 18.5,
                      ),
                    ),
                    const SizedBox(width: 5),
                    // element widget
                    Expanded(child: widget),
                  ],
                ),
              ),
              const Divider(
                thickness: 1.5,
                color: Color.fromARGB(60, 105, 105, 105),
                height: 0,
              ),
            ],
          ),
        ),

我认为我可以在我的行内使用crossAxisAlignment: CrossAxisAlignment.start,但我得到了这样的结果:

enter image description here

如您所见,文本和文本字段没有垂直对齐! 我不想用SizedBox对文本进行换行,因为当字体大小发生变化时,我将不得不重新配置它.

有什么主意吗?

谢谢!

推荐答案

您遇到的问题似乎与TextField的默认填充有关.

这就是我实现的输出布局,我相信这就是你想要的:

target layout

请参阅下面的Widget.build方法(注释代码):

@override
Widget build(BuildContext context) {
  /// Define common properties to easily apply them to different components.
  const style = TextStyle(fontSize: 20.0);
  const separator = SizedBox(width: 16.0);
  const textAlign = TextAlign.right;

  const textFieldDecoration = InputDecoration(
    hintStyle: style,

    /// IMPORTANT: We remove the default TextField padding.
    contentPadding: EdgeInsets.zero,
    border: InputBorder.none,
  );

  /// Dynamically get the size of the largest label in order to properly align
  /// based on the widest label.
  ///
  /// Note that we pass the [style] that we will use to properly calculate the
  /// size.
  ///
  /// This is useful if you use localized text, as it will allow proper
  /// layout/alignment.
  /// Example: [localization.title, localization.description].map(...)
  final maxLabelWidth = ["Title", "Description"]
        .map((e) => e.intrinsicSize(context: context, style: style).width)
        .reduce((a, b) => a > b ? a : b);

  return Column(
    /// The column should occupy as much horizontal space as possible.
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [
      Row(
        children: [
          /// Our first label is wrapped with our determined max label width.
          SizedBox(
            width: maxLabelWidth,
            child: const Text("Title", textAlign: textAlign, style: style),
          ),
          separator,
          Expanded(
            child: TextField(
              decoration: textFieldDecoration.copyWith(hintText: "Required"),
              style: style,
            ),
          ),
        ],
      ),
      const Divider(height: 24.0),
      Row(
        /// You had it right, we must align to the start.
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          SizedBox(
            width: maxLabelWidth,
            child: const Text(
              "Description",
              textAlign: textAlign,
              style: style,
            ),
          ),
          separator,
          const Expanded(
            child: TextField(
              decoration: textFieldDecoration,
              style: style,
              textAlign: TextAlign.justify,
              maxLines: null,
            ),
          ),
        ],
      ),
    ],
  );
}

intrinsicSize功能的实现:

Size intrinsicSize({
  BuildContext? context,
  double maxWidth = double.infinity,
  int? maxLines,
  TextStyle? style,
}) {
  final double textScaleFactor;
  final TextDirection textDirection;

  if (context == null) {
    textScaleFactor = 1.0;
    textDirection = TextDirection.ltr;
  } else {
    textScaleFactor = MediaQuery.of(context).textScaleFactor;
    textDirection = Directionality.of(context);
  }

  return (TextPainter(
    text: TextSpan(text: this, style: style),
    textScaleFactor: textScaleFactor,
    textDirection: textDirection,
    maxLines: maxLines,
  )..layout(maxWidth: maxWidth))
      .size;
}

Flutter相关问答推荐

无法在主屏幕视图中设置当前日期容器'

如何让经过Firebase身份验证的用户能够将Zip文件上载到Firebase云存储?

Flutter /dart 列表.第一个子类列表中的Where()错误

升级到Ffltter 3.16后,应用程序栏、背景 colored颜色 、按钮大小和间距都会发生变化

如何在 Flutter 中设计像 zomato 应用程序一样的搜索字段?

Supabase Auth:重定向到 Google.com 而不是我的应用程序

如何从flutter中不同类别的列表中获取所有产品

如何在列表视图生成器中显示图像

无法在 Flutter 项目中安装最新版本的 image_picker

自定义绘画未显示 - Flutter

Riverpod中stateNotiferProvider和notifierProvider的区别

Dart 撕掉 const 构造函数

更改下拉按钮的背景 colored颜色 - flutter

小部件堆栈未显示顶层

Flutter 分析发现 Gitlab CI/CD 管道中的问题,但它在本地没有

如何格式化网址?

为什么将 bloc 提供程序放在单独的文件/类中会引发错误?

Flutter HLS .m3u8 视频无法实时播放

未处理的异常:FileSystemException:无法打开文件,路径 = 'assets/res.zip'(操作系统错误:没有这样的文件或目录,errno = 2)

用于空值 FirebaseFirestore 的空值判断运算符