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

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

安装

首先,在您的 pubspec.yaml 文件中添加 katro_timeline 作为依赖项。

dependencies:
  katro_timeline: ^版本号

然后运行 flutter pub get 来安装该包。

屏幕截图

水平环绕型时间线

水平环绕型时间线

水平直线型时间线

水平直线型时间线

垂直直线型时间线

垂直直线型时间线

垂直环绕型时间线

垂直环绕型时间线

使用示例

以下是一个完整的示例代码,展示了如何在 Flutter 应用中使用 katro_timeline 插件来创建一个时间线。

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:katro_timeline/data/enum/type_time_line.dart';
import 'package:katro_timeline/data/icon_and_image_style.dart';
import 'package:katro_timeline/data/line_style.dart';
import 'package:katro_timeline/data/time_line_item.dart';
import 'package:katro_timeline/flutter_timeline.dart';
import 'package:line_icons/line_icons.dart';

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter TimeLine',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Flutter TimeLine'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  final GlobalKey keyContainer = GlobalKey();

  [@override](/user/override)
  Widget build(BuildContext context) {
    List<Color> colorNode = [
      const Color(0xff1052b5),
      const Color(0xffb81a22),
      const Color(0xff4c81b0),
      const Color(0xffcf235c),
      const Color(0xffedd207),
    ];
    double storkLineWidth = 5;
    List<String> title = [
      '2004',
      '2005',
      '2006',
      '2010',
      '2011',
    ];
    List<String> description = [
      " Facebook \n Originally designed for college students, it quickly grew into the world's largest social networking platform, connecting billions of users globally.",
      " YouTube \n Revolutionized the way people consume and share video content online, becoming the leading platform for video sharing and streaming.",
      " Twitter \n Introduced the concept of microblogging, enabling users to share short updates and follow others in real-time.",
      " Instagram \n Focused on photo and video sharing, it quickly gained popularity for its visual-centric approach to social networking.",
      " Snapchat \n Pioneered ephemeral messaging, allowing users to send photos and videos that disappear after being viewed.",
    ];
    List<IconData> icons = [
      LineIcons.facebook,
      LineIcons.youtube,
      LineIcons.twitter,
      LineIcons.instagram,
      LineIcons.snapchat,
    ];

    return Scaffold(
      backgroundColor: const Color(0xffcad6e8),
      appBar: AppBar(
        backgroundColor: const Color(0xff4c81b0),
        title: Text(
          widget.title,
          style: const TextStyle(color: Colors.white),
        ),
      ),
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          FlutterTimeLine(
            direction: Axis.vertical,
            typeTimeLine: TypeTimeLine.winding,
            height: 500,
            itemsTimeLine: title
                .map(
                  (e) => ItemLineItem(
                    title: e,
                    endWidget: Padding(
                      padding: const EdgeInsets.all(8.0),
                      child: Text(description.elementAt(title.indexOf(e))),
                    ),
                    height: 80,
                    isFirst: title.indexOf(e) == 0,
                    isLast: title.indexOf(e) == title.length - 1,
                    icon: icons.elementAt(title.indexOf(e)),
                    iconAndImageStyle: const IconAndImageStyle(
                      color: Colors.white,
                      size: 28,
                    ),
                    afterLineStyle: LineStyle(
                      lineColor: colorNode.elementAt(title.indexOf(e)),
                      lineStokeWidth: storkLineWidth,
                    ),
                    circleColor: colorNode.elementAt(title.indexOf(e)),
                    beforeLineStyle: LineStyle(
                      lineColor:
                          colorNode.elementAt(max(title.indexOf(e) - 1, 0)),
                      lineStokeWidth: storkLineWidth,
                    ),
                  ),
                )
                .toList(),
          ),
        ],
      ),
    );
  }
}

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

1 回复

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


当然,以下是一个关于如何在Flutter中使用katro_timeline插件来展示时间线的示例代码。这个插件可以帮助你创建一个美观且易于使用的时间线视图。

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

dependencies:
  flutter:
    sdk: flutter
  katro_timeline: ^latest_version  # 请替换为最新的版本号

然后,运行flutter pub get来安装依赖。

接下来,你可以在你的Flutter应用中使用KatroTimeline来创建时间线。以下是一个完整的示例:

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

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

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

class TimelineDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Katro Timeline Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: KatroTimeline(
          items: [
            KatroTimelineItem(
              leading: Icon(Icons.today),
              title: Text('Event 1'),
              description: Text('This is the description for Event 1.'),
              trailing: Icon(Icons.arrow_forward),
              time: Text('01/01/2023'),
            ),
            KatroTimelineItem(
              leading: Icon(Icons.event),
              title: Text('Event 2'),
              description: Text('This is the description for Event 2.'),
              trailing: Icon(Icons.arrow_forward),
              time: Text('02/01/2023'),
            ),
            KatroTimelineItem(
              leading: Icon(Icons.work),
              title: Text('Event 3'),
              description: Text('This is the description for Event 3.'),
              trailing: Icon(Icons.arrow_forward),
              time: Text('03/01/2023'),
            ),
          ],
          lineColor: Colors.grey[400]!,
          lineThickness: 2.0,
          dotColor: Colors.blue,
          dotSize: 12.0,
          dotShape: KatroDotShape.circle,
          alignment: KatroAlignment.center,
          crossAxisAlignment: KatroCrossAxisAlignment.center,
          mainAxisSpacing: 20.0,
          crossAxisSpacing: 20.0,
        ),
      ),
    );
  }
}

在这个示例中,我们创建了一个简单的Flutter应用,并在其中使用了KatroTimeline来展示三个事件。每个事件包括一个图标(leading)、标题(title)、描述(description)、一个指向右侧的箭头图标(trailing)以及事件的时间(time)。

KatroTimeline还允许你自定义线条的颜色(lineColor)、线条的厚度(lineThickness)、点的颜色(dotColor)、点的大小(dotSize)和形状(dotShape),以及事件项之间的间距(mainAxisSpacingcrossAxisSpacing)。

你可以根据需要调整这些参数来满足你的设计要求。希望这个示例能够帮助你快速上手katro_timeline插件!

回到顶部