Flutter时间线展示插件timelineview的使用

Flutter时间线展示插件timelineview的使用

在本教程中,我们将介绍如何使用 timelineview 插件来创建一个时间线展示组件。该插件可以帮助您轻松地构建带有时间标签的时间线。

如何使用

首先,确保您已经在项目中添加了 timelineview 依赖项。在 pubspec.yaml 文件中添加以下内容:

dependencies:
  timelineview: ^版本号

然后运行 flutter pub get 来安装它。

接下来,我们来看一下 TimelineView 的基本用法。

基本用法

TimelineView(
    activeIndex: activeIndex, // 当前激活的索引,默认为0
    showLabels: false, // 是否显示底部标签,默认为true
    circleRadius: 15.0, // 圆圈的半径,如果提供了selected和unSelectedWidgets,则会被忽略
    lineHeight: 2.0, // 线的高度,如果提供了lineWidget,则会被忽略
    lineWidget: _lineWidget, // 放置在线上的小部件
    selectedTextStyle: , // 选中项的标签样式
    selectedWidget: , // 选中的小部件
    unSelectedTextStyle: , // 未选中项的标签样式
    unSelectedWidget: , // 未选中的小部件
    onChanged: (index) {
        setState(() {
            activeIndex = index;
        });
    }, // 选择改变时触发的回调
    labelWidgets: [
        Text("One"),
        Text("TwoThree"),
        Text("Four"),
    ], // 时间线上的标签列表
),

完整示例

以下是一个完整的示例代码,展示了如何使用 timelineview 插件。

import 'package:flutter/material.dart';
import 'package:timelineview/timelineview.dart';

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  [@override](/user/override)
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int activeIndex = 0; // 当前激活的索引

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          children: [
            TimelineView(
              activeIndex: activeIndex, // 当前激活的索引
              showLabels: true, // 显示底部标签
              circleRadius: 7.0, // 圆圈的半径
              lineHeight: 2.0, // 线的高度
              selectedWidget: Container(
                width: 14,
                height: 14,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.red, // 选中项的颜色
                ),
              ), // 选中的小部件
              unSelectedWidget: Container(
                width: 14,
                height: 14,
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: Colors.white, // 未选中小部件的颜色
                  border: Border.all(color: Colors.red, width: 1), // 边框颜色
                ),
              ), // 未选中的小部件
              onChanged: (index) { // 选择改变时触发的回调
                setState(() {
                  activeIndex = index; // 更新当前激活的索引
                });
              },
              labelWidgets: [ // 时间线上的标签列表
                Text("One"),
                Text("TwoThree"),
                Text("Four"),
              ],
              iconWidgets: [ // 可选的图标列表
                Icon(Icons.home),
                Icon(Icons.ac_unit),
                Icon(Icons.access_alarm),
              ],
              hasFinished: true, // 是否已完成(用于动画效果)
              unFinishedLineColor: Colors.red[100], // 未完成线的颜色
              finishedLineColor: Colors.red, // 已完成线的颜色
            ),
          ],
        ),
      ),
    );
  }
}

更多关于Flutter时间线展示插件timelineview的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter时间线展示插件timelineview的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


timeline_view 是一个用于在 Flutter 应用中展示时间线的插件。它可以帮助你创建漂亮的时间线布局,适用于展示事件、任务、进度等。以下是使用 timeline_view 的基本步骤和示例代码。

1. 添加依赖

首先,你需要在 pubspec.yaml 文件中添加 timeline_view 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  timeline_view: ^1.0.0  # 请检查最新版本

然后运行 flutter pub get 来获取依赖。

2. 基本使用

以下是一个简单的示例,展示如何使用 timeline_view 创建一个时间线。

import 'package:flutter/material.dart';
import 'package:timeline_view/timeline_view.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Timeline View Example'),
        ),
        body: TimelineView(
          events: [
            TimelineEvent(
              title: 'Event 1',
              description: 'This is the first event',
              time: '10:00 AM',
            ),
            TimelineEvent(
              title: 'Event 2',
              description: 'This is the second event',
              time: '11:00 AM',
            ),
            TimelineEvent(
              title: 'Event 3',
              description: 'This is the third event',
              time: '12:00 PM',
            ),
          ],
        ),
      ),
    );
  }
}

3. 自定义时间线

你可以通过自定义 TimelineView 的属性来调整时间线的外观和行为。以下是一些常用的属性:

  • lineColor: 设置时间线的颜色。
  • lineWidth: 设置时间线的宽度。
  • eventGap: 设置事件之间的间距。
  • eventBuilder: 自定义事件的构建方式。
TimelineView(
  lineColor: Colors.blue,
  lineWidth: 2.0,
  eventGap: 20.0,
  events: [
    TimelineEvent(
      title: 'Event 1',
      description: 'This is the first event',
      time: '10:00 AM',
    ),
    TimelineEvent(
      title: 'Event 2',
      description: 'This is the second event',
      time: '11:00 AM',
    ),
  ],
  eventBuilder: (context, event) {
    return Card(
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              event.title,
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8),
            Text(event.description),
            SizedBox(height: 8),
            Text(event.time),
          ],
        ),
      ),
    );
  },
)

4. 处理事件点击

你还可以为时间线中的事件添加点击事件处理。

TimelineView(
  events: [
    TimelineEvent(
      title: 'Event 1',
      description: 'This is the first event',
      time: '10:00 AM',
      onTap: () {
        print('Event 1 tapped');
      },
    ),
    TimelineEvent(
      title: 'Event 2',
      description: 'This is the second event',
      time: '11:00 AM',
      onTap: () {
        print('Event 2 tapped');
      },
    ),
  ],
)

5. 更多自定义

你可以根据需要进一步自定义时间线的外观和行为。例如,你可以使用 TimelineEventicon 属性为事件添加图标,或者使用 TimelineViewindicatorBuilder 自定义时间线指示器。

TimelineView(
  events: [
    TimelineEvent(
      title: 'Event 1',
      description: 'This is the first event',
      time: '10:00 AM',
      icon: Icon(Icons.check_circle, color: Colors.green),
    ),
    TimelineEvent(
      title: 'Event 2',
      description: 'This is the second event',
      time: '11:00 AM',
      icon: Icon(Icons.error, color: Colors.red),
    ),
  ],
  indicatorBuilder: (context, index) {
    return Container(
      width: 20,
      height: 20,
      decoration: BoxDecoration(
        color: Colors.blue,
        shape: BoxShape.circle,
      ),
      child: Center(
        child: Text(
          '${index + 1}',
          style: TextStyle(color: Colors.white, fontSize: 12),
        ),
      ),
    );
  },
)
回到顶部