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

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

framework: flutter

platform: Android, iOS, Web, macOS, Linux, Windows
tags: flutter, timeline, flutter timeline v2, timeline tile
title: flutter timeline v2

flutter_timeline

[logo]

该插件基于gridaco的flutter_timeline包创建。我只是为了Flutter社区保持其更新。

a fully customizable & general timeline widget, based on real-world application references

Installation

pubspec.yaml文件中添加以下依赖:

dependencies:
  flutter_timeline_v2: latest

usage

简单示例

// 定义一个简单的事件显示方式
TimelineEventDisplay get plainEventDisplay {
  return TimelineEventDisplay(
      child: TimelineEventCard(
        title: Text("just now"),
        content: Text("someone commented on your timeline ${DateTime.now()}"),
      ),
      indicator: TimelineDots.of(context).circleIcon);
}

List<TimelineEventDisplay> events;

Widget _buildTimeline() {
  return TimelineTheme(
      data: TimelineThemeData(lineColor: Colors.blueAccent), // 设置时间轴线条颜色
      child: Timeline(
        indicatorSize: 56, // 设置指示器大小
        events: events, // 设置事件列表
      ));
}

void _addEvent() {
  setState(() {
    events.add(plainEventDisplay); // 添加新事件
  });
}

使用偏移量

Widget _buildTimeline() {
  return Timeline(
      indicatorSize: 56,
      events: events,
      altOffset: Offset(0, -24) // 设置偏移量
  );
}

使用锚点和偏移量

TimelineEventDisplay get plainEventDisplay {
  return TimelineEventDisplay(
      anchor: IndicatorPosition.top, // 设置锚点位置
      indicatorOffset: Offset(0, 24), // 设置偏移量
      child: TimelineEventCard(
        title: Text("multi\nline\ntitle\nawesome!"), // 设置多行标题
        content: Text("someone commented on your timeline ${DateTime.now()}"), // 设置内容
      ),
      indicator: randomIndicator); // 设置指示器
}

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

1 回复

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


当然,下面是一个关于如何使用 flutter_timeline_v2 插件的示例代码。这个插件可以用于在 Flutter 应用中展示时间线。

首先,确保你已经在 pubspec.yaml 文件中添加了 flutter_timeline_v2 依赖:

dependencies:
  flutter:
    sdk: flutter
  flutter_timeline_v2: ^最新版本号  # 替换为最新版本号

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

接下来,在你的 Dart 文件中使用 flutter_timeline_v2 插件。以下是一个简单的示例,展示了如何创建一个时间线:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Timeline Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: TimelineDemo(),
    );
  }
}

class TimelineDemo extends StatelessWidget {
  final List<TimelineEvent> events = [
    TimelineEvent(
      dot: TimelineDot(
        color: Colors.blue,
      ),
      content: Container(
        padding: EdgeInsets.all(16.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.0),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Event 1',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8.0),
            Text(
              'This is the description for event 1.',
              style: TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
      time: '2023-10-01',
    ),
    TimelineEvent(
      dot: TimelineDot(
        color: Colors.green,
      ),
      content: Container(
        padding: EdgeInsets.all(16.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.0),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Event 2',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8.0),
            Text(
              'This is the description for event 2.',
              style: TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
      time: '2023-10-05',
    ),
    TimelineEvent(
      dot: TimelineDot(
        color: Colors.red,
      ),
      content: Container(
        padding: EdgeInsets.all(16.0),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(8.0),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withOpacity(0.5),
              spreadRadius: 5,
              blurRadius: 7,
              offset: Offset(0, 3), // changes position of shadow
            ),
          ],
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Event 3',
              style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
            ),
            SizedBox(height: 8.0),
            Text(
              'This is the description for event 3.',
              style: TextStyle(fontSize: 14),
            ),
          ],
        ),
      ),
      time: '2023-10-10',
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Timeline Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Timeline(
          events: events,
          lineColor: Colors.grey[300]!,
          lineThickness: 2.0,
          alignment: TimelineAlign.center,
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的 Flutter 应用,其中包含一个时间线。时间线包含三个事件,每个事件都有一个自定义的 TimelineEvent 对象,包含时间戳、颜色和描述内容。

  • TimelineDot 用于定义时间线点的样式。
  • TimelineEvent 包含 dotcontenttime 属性,分别表示时间线点、内容和时间戳。
  • Timeline 组件用于渲染整个时间线,通过 events 属性传入事件列表。

你可以根据需要自定义每个事件的样式和内容。希望这个示例对你有所帮助!

回到顶部