简体   繁体   English

如何在 flutter 行小部件中绘制水平线?

[英]How to draw a horizontal line in flutter row widgets?

In my flutter project, I have initialized a Row.在我的flutter项目中,我初始化了一个Row。 Inside that, I created some Texts in a column and after that, I want to add a line but it is not showing anything.在里面,我在一列中创建了一些文本,然后我想添加一行,但它没有显示任何内容。 I have used Expanded for that reason and followed the given solutions-出于这个原因,我使用了 Expanded 并遵循了给定的解决方案-

Horizontal divider with text in the middle in Flutter? Flutter 中间有文字的水平分隔线?

But none of them worked either.但他们都没有工作。

Here's the image of my code output-这是我的代码输出的图像 -

在此处输入图像描述

Here's my code-这是我的代码-

Container(
      color:Colors.white,
      child: (
        Row(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: Image(
                  height: 100,
                  width: 100,
                  image: NetworkImage("https://www.gstatic.com/webp/gallery/1.jpg"),
                ),
            ),
            Column(
              children: <Widget>[
                Text("Book Name"),
                Text("Author name"),

                Divider(
                  color: Colors.black,
                )
              ],
            )
          ],
        )
      ),
    ),

So, I need a line below the two texts and show it like the below picture-所以,我需要在这两个文本下方添加一行,并如下图所示-

在此处输入图像描述

Try wrapping you Column in an Expanded so the Divider knows how much space to occupy.尝试将您的Column包裹在Expanded以便Divider知道要占用多少空间。

Container(
  color: Colors.white,
  child: (Row(
    children: <Widget>[
      // ...
      Expanded(
        child: Column(
          children: <Widget>[
            Text("Book Name"),
            Text("Author name"),
            Divider(
              color: Colors.black
            )
          ],
        ),
      )
    ],
  )),
);

The idea is that you placed your divider inside a column whereas the divider by default is horizontal so it doesn't have enough space to occupy.这个想法是你将divider放在一列内,而默认情况下分隔线是水平的,所以它没有足够的空间来占据。 If you change your height property for the divider you can see it clearly.如果您更改divider height属性,您可以清楚地看到它。

If you want you can wrap your divider in a row or perhaps make the divider part of the outer row though you might have to fix its alignment and wrap it in an expanded .如果您愿意,您可以将divider包裹成row或者将divider作为外row一部分,尽管您可能必须修复其对齐方式并将其包裹在expanded . You can also wrap the column in an expanded so it occupies all enough space for the divider to appear.您还可以将列包裹在展开的列中,以便它占据足够的空间让分隔符出现。

I'd do the code for you but I only have part of the code of what's displayed + it shouldn't be too difficult.我会为您编写代码,但我只有所显示内容的部分代码 + 应该不会太难。 If you needed more help let me know!如果您需要更多帮助,请告诉我!

If you know exactly how long you want the line to be.如果你确切地知道你想要这条线有多长。 You can wrap the Divider with a SizedBox specifying the width of the line with the SizedBox properties.您可以使用 SizedBox 包装Divider ,并使用SizedBox属性指定线的SizedBox

添加垂直分隔线使用Divider(),添加水平分隔线使用Spacer()

Easy Pezy Lemon Squeezy简单的 Pezy 柠檬挤压

Its simple, see take a container, do not mention its width, because it will take the space dynamically and then only give the color to the lower border of the radius.很简单,看拿一个容器,不提它的宽度,因为它会动态的占用空间,然后只给半径的下边框颜色。

 Container( child: Text( 'Your Text Here', ), decoration: BoxDecoration( border: Border( bottom: BorderSide(color: Colors.black), ), ), ),

To add Horizantal divider in row在行中添加水平分隔线

add a container widget to child: and fix container width:0.1, and color:Colors.black,向 child: 添加容器小部件,并修复容器宽度:0.1 和颜色:Colors.black,

sample:Container(width: 0.1,
                  color:Colors.black),

//example to add divider in Row(), // 在 Row() 中添加分隔符的示例,

 Container(
                    width: double.infinity,
                    height: 80.0,
                    color: Colors.grey[200],
                    child:Row(
                      children:[
                        Expanded(
                        child:ListTile(
                        //leading: Icon(Icons.speed_sharp),
                        title: Text('Speed'),
                        subtitle: Text('0 mbps'),
                        trailing: Icon(Icons.speed_sharp),
                        horizontalTitleGap: 2.0,
                        )
                      ),
//Sample Code from here:
                       Container(width: 0.1,
                      color:Colors.black),


                      Expanded(child: ListTile(
                        
                       // leading: Icon(Icons.account_balance_wallet_sharp),
                        title: Text('Renewal Date'),
                        subtitle: Text('N/A'),
                        trailing: Icon(Icons.account_balance_wallet_sharp),
                        horizontalTitleGap: 4.0,
                        hoverColor: Colors.red,
                      ),flex: 1,
                      ),
                      Expanded(child: ListTile(
                       // leading: Icon(Ionicons.bag_add),
                        title: Text('Wallet'),
                        subtitle: Text(' 15.33'),
                        trailing: Icon(Ionicons.bag_add),
                        horizontalTitleGap: 2.0,
                        
                      )),
                      ],
                    ) ,
                  ),

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

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