Flutter 中如何添加垂直分隔线【Flutter 专题 18】
作者:坚果前端
- 2021 年 12 月 08 日
本文字数:1950 字
阅读完需:约 6 分钟

今天有粉丝在群里问我 Flutter 中如何添加垂直分隔线
当然是通过使用 VerticalDivider 小部件,我们可以就可以在小部件之间添加垂直分隔线。
先来看效果
完整代码
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
/// This is the main application widget.class MyApp extends StatelessWidget { const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter VerticalDivider Sample';
@override Widget build(BuildContext context) { return MaterialApp( title: _title, home: Scaffold( appBar: AppBar(title: const Text(_title)), body: const MyStatelessWidget(), ), ); }}
/// This is the stateless widget that the main application instantiates.class MyStatelessWidget extends StatelessWidget { const MyStatelessWidget({Key? key}) : super(key: key);
@override Widget build(BuildContext context) { return Container( padding: const EdgeInsets.all(10), child: Row( children: <Widget>[ Expanded( child: Container( alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.blue, ), child: Text( "坚果", style: TextStyle(fontSize: 23), ), ), ), const VerticalDivider( color: Colors.grey, thickness: 1, indent: 20, endIndent: 0, width: 20, ), Expanded( child: Container( alignment: Alignment.center, decoration: BoxDecoration( borderRadius: BorderRadius.circular(10), color: Colors.orange, ), child: Text( "前端", style: TextStyle(fontSize: 23), ), ), ), ], ), ); }}
复制代码
构造函数
VerticalDivider({ Key? key, this.width, this.thickness, this.indent, this.endIndent, this.color,})
复制代码
特性:
color : 设置分隔线颜色thickness: 设置分隔线的厚度indent : 设置分隔符顶部的空间endIndent :设置分隔线底部的空间
复制代码
当我们在行小部件中添加 verticaldivider 时,它不会显示。我们可以通过以下方式克服不显示 verticaldivider 的问题
在 IntrinsicHeight 小部件中添加您的行小部件
在具有所需高度的 SizedBox 小部件中添加 VerticalDivider
其他使用 IntrinsicHeight 的示例
IntrinsicHeight( child: Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.menu,color: AppColors.technoBlack,), SizedBox(width: 5.w,), Expanded(child: TextField( style: TextStyle(fontSize: 16.sp,color: AppColors.technoBlack,fontWeight: FontWeight.w300), decoration: InputDecoration( hintText: "Your Current Location", focusedBorder: InputBorder.none,
),)), SizedBox(width: 5.w,),
VerticalDivider(color: AppColors.technoBlack,thickness: 2), SizedBox(width: 5.w,), Icon(Icons.filter_tilt_shift,color: AppColors.technoBlack,), ],),),
复制代码
使用 VerticalDivider 的示例
Row( mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.center, children: [ Icon(Icons.menu,color: AppColors.technoBlack,), SizedBox(width: 5.w,), Expanded(child: TextField( style: TextStyle(fontSize: 16.sp,color: AppColors.technoBlack,fontWeight: FontWeight.w300), decoration: InputDecoration( hintText: "Your Current Location", focusedBorder: InputBorder.none,
),)), SizedBox(width: 5.w,),
SizedBox(child: VerticalDivider(color: AppColors.technoBlack,thickness: 2,),height: 30,), SizedBox(width: 5.w,), Icon(Icons.filter_tilt_shift,color: AppColors.technoBlack,),],),
复制代码
今天这个小部件你学会了吗?码字不易,喜欢的话,点赞支持一下呗,我的公众号“坚果前端”
划线
评论
复制
发布于: 4 小时前阅读数: 8
版权声明: 本文为 InfoQ 作者【坚果前端】的原创文章。
原文链接:【http://xie.infoq.cn/article/428ddb42dc0366804fb63cc61】。文章转载请联系作者。
坚果前端
关注
此间若无火炬,我便是唯一的光 2020.10.25 加入
公众号:“坚果前端”,华为云享专家,51CTO博客首席体验官,专注于大前端技术的分享,包括Flutter,小程序,安卓,VUE,JavaScript。











评论