简体   繁体   English

Flutter:获取vericalDivider的小部件的相对高度

[英]Flutter: Getting relative height of widget for vericalDivider

Im building a screen where a widget is supposed to look like a Tweet.我正在构建一个屏幕,其中的小部件应该看起来像 Tweet。 Im trying to implement the vertical bars seen under profile pictures with the following:我正在尝试使用以下内容实现个人资料图片下看到的垂直条:

Expanded(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                VerticalDivider(
                  color: Colors.red,
                  thickness: 10,
                  width: 20,
                ),
                //make a widget that looks like twitter composing tweet
                CircleAvatar(
                  radius: 20,
                  backgroundImage: NetworkImage(user.imageUrls[0]),
                ),
                //add a gray line that runs down the middle of the screen
                VerticalDivider(
                  color: Colors.red,
                  thickness: 10,
                  width: 200,
                ),
              ],
            ),
          ),

Which looks like this:看起来像这样:

在此处输入图像描述

As you can see, the dividers are invisible.如您所见,分隔线是不可见的。 Wrapping the divider with a specified height makes it look like this:用指定的高度包裹分隔线使它看起来像这样:

在此处输入图像描述

It is now visible, but obviously expands outside of the original size of the widget.它现在可见,但显然超出了小部件的原始大小。 Is there any way to pass the size of the parent into the VerticalDivider so i knows how much space it has to work with?有什么方法可以将父级的大小传递到 VerticalDivider 中,这样我就知道它必须使用多少空间?

Thanks!谢谢!

You can wrap your Row with the IntrinsicHeight widget.您可以使用IntrinsicHeight小部件包装您的Row It will set the rows height based on its tallest child (here the Container with height = 200).它将根据其最高的子项(此处为高度 = 200 的容器)设置行高。 Of course in your example you don't know the tallest child's size before layout, I used the container simply to give you an example.当然在你的例子中你不知道布局前最高的孩子的尺寸,我使用容器只是为了给你一个例子。 IntrinsicHeight is relatively expensive to use but the right choice for such use cases (see its docs ). IntrinsicHeight使用起来相对昂贵,但却是此类用例的正确选择(请参阅其文档)。

import 'package:flutter/material.dart';

const Color darkBlue = Color.fromARGB(255, 18, 32, 47);

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark().copyWith(
        scaffoldBackgroundColor: darkBlue,
      ),
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: Center(
          child: MyWidget(),
        ),
      ),
    );
  }
}

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return IntrinsicHeight(
      child: Row(
        children: [
          Container(
            color: Colors.grey,
            child: Column(
              children: [
                SizedBox(
                  child: VerticalDivider(
                    color: Colors.red,
                    thickness: 10,
                    width: 20,
                  ),
                  height: 20,
                ),
                Container(height: 40, width: 40, color: Colors.blue),
                Expanded(
                  child: VerticalDivider(
                    color: Colors.red,
                    thickness: 10,
                    width: 20,
                  ),
                ),
              ],
            ),
          ),
          Container(
            height: 200,
            width: 200,
            color: Colors.white,
          )
        ],
      ),
    );
  }
}

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

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