Flutter自定义虚线绘制插件fdottedline_nullsafety的使用
Flutter自定义虚线绘制插件fdottedline_nullsafety的使用
简介
fdottedline
是一个用于在Flutter中轻松创建虚线视图的插件。它不仅支持创建水平和垂直方向的虚线,还支持为Widget添加虚线边框,并且可以灵活控制虚线的粗细、间距以及圆角效果。
作者
- Newton (coorchice.cb@alibaba-inc.com)
特性
- 支持水平和垂直方向的虚线
- 支持创建虚线形状
- 提供一种非常简单的方法为Widget添加虚线边框
- 支持创建灵活的虚线圆角效果
参数与接口
参数 | 类型 | 是否必填 | 默认值 | 描述 |
---|---|---|---|---|
color | Color | 否 | Colors.black |
虚线颜色 |
height | double | 否 | null | 高度。如果只有height 而没有width ,将生成垂直方向的虚线。如果有width 和height ,将生成虚线边框。 |
width | double | 否 | null | 宽度。如果只有width 而没有height ,将生成水平方向的虚线。如果有width 和height ,将生成虚线边框。 |
strokeWidth | double | 否 | 1.0 | 虚线的粗细 |
dottedLength | double | 否 | 5.0 | 每个小段的长度 |
space | double | 否 | 3.0 | 每个段之间的间距 |
corner | FDottedLineCorner | 否 | null | 虚线边框的圆角。详情请参见FDottedLineCorner |
child | Widget | 否 | null | 如果设置了child ,FDottedLine 将作为child 的虚线边框。此时width 和height 将不再有效。 |
示例代码
1. 水平虚线示例
FDottedLine(
color: Colors.red, // 设置虚线颜色为红色
width: 160.0, // 设置宽度为160.0
strokeWidth: 2.0, // 设置虚线的粗细为2.0
dottedLength: 10.0, // 设置每个小段的长度为10.0
space: 2.0, // 设置每个段之间的间距为2.0
)
2. 垂直虚线示例
FDottedLine(
color: Colors.blue, // 设置虚线颜色为蓝色
height: 160.0, // 设置高度为160.0
strokeWidth: 2.0, // 设置虚线的粗细为2.0
dottedLength: 10.0, // 设置每个小段的长度为10.0
space: 2.0, // 设置每个段之间的间距为2.0
)
3. 虚线矩形示例
FDottedLine(
color: Colors.lightBlue[600], // 设置虚线颜色为浅蓝色
height: 100.0, // 设置高度为100.0
width: 50, // 设置宽度为50.0
strokeWidth: 2.0, // 设置虚线的粗细为2.0
dottedLength: 10.0, // 设置每个小段的长度为10.0
space: 2.0, // 设置每个段之间的间距为2.0
)
4. 圆角虚线矩形示例
FDottedLine(
color: Colors.lightBlue[600], // 设置虚线颜色为浅蓝色
height: 70.0, // 设置高度为70.0
width: 70.0, // 设置宽度为70.0
strokeWidth: 2.0, // 设置虚线的粗细为2.0
dottedLength: 10.0, // 设置每个小段的长度为10.0
space: 2.0, // 设置每个段之间的间距为2.0
corner: FDottedLineCorner.all(50), // 设置所有角的圆角半径为50
)
5. 为Widget添加虚线边框示例
FDottedLine(
color: Colors.red, // 设置虚线颜色为红色
strokeWidth: 2.0, // 设置虚线的粗细为2.0
dottedLength: 8.0, // 设置每个小段的长度为8.0
space: 3.0, // 设置每个段之间的间距为3.0
corner: FDottedLineCorner.all(6.0), // 设置所有角的圆角半径为6.0
child: Container(
color: Colors.blue[100], // 设置容器背景颜色为浅蓝色
width: 130, // 设置容器宽度为130
height: 70, // 设置容器高度为70
alignment: Alignment.center, // 设置内容居中
child: Text("0873"), // 显示文本"0873"
),
)
6. 嵌套虚线边框示例
FDottedLine(
color: Colors.red, // 设置外层虚线颜色为红色
strokeWidth: 2.0, // 设置外层虚线的粗细为2.0
dottedLength: 8.0, // 设置外层虚线每个小段的长度为8.0
space: 3.0, // 设置外层虚线每个段之间的间距为3.0
corner: FDottedLineCorner.all(75.0), // 设置外层虚线所有角的圆角半径为75.0
child: Container(
width: 130, // 设置容器宽度为130
height: 130, // 设置容器高度为130
alignment: Alignment.center, // 设置内容居中
child: FDottedLine(
color: Colors.blue, // 设置内层虚线颜色为蓝色
strokeWidth: 2.0, // 设置内层虚线的粗细为2.0
dottedLength: 8.0, // 设置内层虚线每个小段的长度为8.0
space: 3.0, // 设置内层虚线每个段之间的间距为3.0
corner: FDottedLineCorner.all(20.0), // 设置内层虚线所有角的圆角半径为20.0
child: Container(
width: 43.0, // 设置内层容器宽度为43.0
height: 43.0, // 设置内层容器高度为43.0
color: Colors.grey[900], // 设置内层容器背景颜色为深灰色
),
),
),
)
如何使用
1. 添加依赖
在项目的pubspec.yaml
文件中添加依赖:
使用Pub依赖
dependencies:
fdottedline: ^<version number>
注意:请前往 pub 获取最新版本号。
使用Git依赖
dependencies:
fdottedline:
git:
url: 'git@github.com:Fliggy-Mobile/fdottedline.git'
ref: '<Branch number or tag number>'
注意:请参考 FDottedLine 官方项目获取分支号或标签号。
2. 运行示例项目
- 克隆 项目到本地
- 进入项目
example
目录并运行以下命令:flutter create .
- 在
example
目录中运行示例项目。
许可证
Copyright 2020-present Fliggy Android Team <alitrip_android@list.alibaba-inc.com>.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at following link.
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
更多关于Flutter自定义虚线绘制插件fdottedline_nullsafety的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter自定义虚线绘制插件fdottedline_nullsafety的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter项目中使用自定义虚线绘制插件fdottedline_nullsafety
的示例代码。这个插件允许你在Flutter应用中绘制虚线。首先,你需要确保你的pubspec.yaml
文件中已经添加了该插件的依赖。
第一步:添加依赖
在你的pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter:
sdk: flutter
fdottedline_nullsafety: ^最新版本号 # 请替换为最新的版本号
然后运行flutter pub get
来安装依赖。
第二步:导入并使用插件
接下来,你可以在你的Flutter项目中导入并使用这个插件。以下是一个简单的示例,展示如何在你的应用中绘制虚线。
import 'package:flutter/material.dart';
import 'package:fdottedline_nullsafety/fdottedline_nullsafety.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Dotted Line Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Dotted Line Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
// 使用 FDottedLine 绘制一条水平虚线
FDottedLine(
color: Colors.black,
lineWidth: 2.0,
gap: 8.0,
length: 200.0,
direction: Axis.horizontal,
),
SizedBox(height: 20.0),
// 使用 FDottedLine 绘制一条垂直虚线
FDottedLine(
color: Colors.red,
lineWidth: 2.0,
gap: 8.0,
length: 200.0,
direction: Axis.vertical,
),
],
),
),
),
);
}
}
解释
- FDottedLine: 这是
fdottedline_nullsafety
插件提供的主要组件,用于绘制虚线。 - color: 虚线的颜色。
- lineWidth: 虚线的宽度。
- gap: 虚线之间的间距。
- length: 虚线的长度。
- direction: 虚线的方向,可以是
Axis.horizontal
(水平)或Axis.vertical
(垂直)。
运行项目
保存所有文件并运行你的Flutter项目。你应该能够在屏幕上看到一条水平和一条垂直的虚线。
这个示例展示了如何使用fdottedline_nullsafety
插件来在Flutter应用中绘制自定义的虚线。你可以根据需要调整参数来满足你的设计需求。