Flutter甘特图布局插件smart_gantt_layout_view的使用

Flutter甘特图布局插件smart_gantt_layout_view的使用

在本教程中,我们将展示如何在Flutter应用中使用smart_gantt_layout_view插件来创建一个简单的甘特图布局。

特性

  • 添加SmartGanttLayoutView和一个简单的甘特图布局算法。

完整示例代码

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

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

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

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

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

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text(
              '你已经点击按钮次数:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            SizedBox(
              height: 100,
              width: 300,
              child: SmartGanttLayoutView(
                algorithmType: GanttLayoutAlgorithmType.smartSpacing,
                events: const [
                  (left: 0, length: 0.2),
                  (left: 0.1, length: 0.5),
                  (left: 0.3, length: 0.2),
                  (left: 0.3, length: 0.2),
                  (left: 0.7, length: 0.05),
                  (left: 0.7, length: 0.2),
                  (left: 0.8, length: 0.05),
                ],
                ganttCardBuilder: (index) {
                  return GanttCard(
                    index + 1,
                    width: double.infinity,
                    color: Colors.red,
                  );
                },
              ),
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: '增加',
        child: const Icon(Icons.add),
      ), // 这个逗号使自动格式化更美观
    );
  }
}

class GanttCard extends StatelessWidget {
  final int index;
  final double? height;
  final double width;
  final Color color;

  const GanttCard(this.index,
      {super.key, this.height, required this.width, required this.color});

  @override
  Widget build(BuildContext context) {
    return Container(
      height: height,
      width: width,
      color: color,
      child: Text('$index'),
    );
  }
}

解释

  1. 导入包:

    import 'package:flutter/material.dart';
    import 'package:smart_gantt_layout_view/smart_gantt_layout_view.dart';
    
  2. 主函数:

    void main() {
      runApp(const MyApp());
    }
    
  3. 创建MaterialApp:

    class MyApp extends StatelessWidget {
      const MyApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
            useMaterial3: true,
          ),
          home: const MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
  4. 创建主页:

    class MyHomePage extends StatefulWidget {
      const MyHomePage({super.key, required this.title});
    
      final String title;
    
      @override
      State<MyHomePage> createState() => _MyHomePageState();
    }
    
  5. 主页状态:

    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            backgroundColor: Theme.of(context).colorScheme.inversePrimary,
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                const Text(
                  '你已经点击按钮次数:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headlineMedium,
                ),
                SizedBox(
                  height: 100,
                  width: 300,
                  child: SmartGanttLayoutView(
                    algorithmType: GanttLayoutAlgorithmType.smartSpacing,
                    events: const [
                      (left: 0, length: 0.2),
                      (left: 0.1, length: 0.5),
                      (left: 0.3, length: 0.2),
                      (left: 0.3, length: 0.2),
                      (left: 0.7, length: 0.05),
                      (left: 0.7, length: 0.2),
                      (left: 0.8, length: 0.05),
                    ],
                    ganttCardBuilder: (index) {
                      return GanttCard(
                        index + 1,
                        width: double.infinity,
                        color: Colors.red,
                      );
                    },
                  ),
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: '增加',
            child: const Icon(Icons.add),
          ),
        );
      }
    }
    
  6. 自定义卡片组件:

    class GanttCard extends StatelessWidget {
      final int index;
      final double? height;
      final double width;
      final Color color;
    
      const GanttCard(this.index,
          {super.key, this.height, required this.width, required this.color});
    
      @override
      Widget build(BuildContext context) {
        return Container(
          height: height,
          width: width,
          color: color,
          child: Text('$index'),
        );
      }
    }
    

更多关于Flutter甘特图布局插件smart_gantt_layout_view的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter甘特图布局插件smart_gantt_layout_view的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


smart_gantt_layout_view 是一个用于在 Flutter 中创建甘特图布局的插件。它提供了灵活的布局选项,可以帮助你轻松地创建和管理甘特图。以下是如何使用 smart_gantt_layout_view 的基本步骤:

1. 添加依赖

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

dependencies:
  flutter:
    sdk: flutter
  smart_gantt_layout_view: ^1.0.0  # 请确保使用最新版本

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

2. 导入插件

在你的 Dart 文件中导入 smart_gantt_layout_view

import 'package:smart_gantt_layout_view/smart_gantt_layout_view.dart';

3. 创建甘特图布局

你可以使用 SmartGanttLayoutView 来创建甘特图布局。以下是一个简单的示例:

class GanttChartExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Gantt Chart Example'),
      ),
      body: SmartGanttLayoutView(
        tasks: [
          GanttTask(
            id: 'task1',
            title: 'Task 1',
            startDate: DateTime(2023, 10, 1),
            endDate: DateTime(2023, 10, 5),
          ),
          GanttTask(
            id: 'task2',
            title: 'Task 2',
            startDate: DateTime(2023, 10, 3),
            endDate: DateTime(2023, 10, 7),
          ),
          GanttTask(
            id: 'task3',
            title: 'Task 3',
            startDate: DateTime(2023, 10, 6),
            endDate: DateTime(2023, 10, 10),
          ),
        ],
        startDate: DateTime(2023, 10, 1),
        endDate: DateTime(2023, 10, 10),
        onTaskTap: (task) {
          print('Task tapped: ${task.title}');
        },
      ),
    );
  }
}

4. 自定义甘特图

SmartGanttLayoutView 提供了多个属性来自定义甘特图的外观和行为。以下是一些常用的属性:

  • tasks: 甘特图中显示的任务列表,类型为 List<GanttTask>
  • startDate: 甘特图的开始日期。
  • endDate: 甘特图的结束日期。
  • onTaskTap: 当用户点击任务时触发的回调函数。
  • taskHeight: 每个任务的高度。
  • dayWidth: 每个日期的宽度。
  • headerHeight: 甘特图头部的高度。
  • taskColor: 任务的默认颜色。
  • taskTextStyle: 任务文本的样式。

5. 运行应用

现在你可以运行你的 Flutter 应用,并查看甘特图的效果。你可以根据需要调整 SmartGanttLayoutView 的属性来创建符合你需求的甘特图。

6. 进一步定制

smart_gantt_layout_view 提供了灵活的 API,允许你进一步定制甘特图的外观和行为。你可以查看插件的文档或源码,了解更多详细信息。

7. 处理动态数据

如果你的甘特图数据是动态的,你可以使用 setState 或状态管理工具(如 ProviderRiverpod)来动态更新 SmartGanttLayoutViewtasks 属性。

setState(() {
  tasks.add(GanttTask(
    id: 'task4',
    title: 'Task 4',
    startDate: DateTime(2023, 10, 8),
    endDate: DateTime(2023, 10, 12),
  ));
});

8. 处理交互

你可以通过 onTaskTap 回调来处理用户与任务的交互。例如,你可以在用户点击任务时显示任务的详细信息。

onTaskTap: (task) {
  showDialog(
    context: context,
    builder: (context) => AlertDialog(
      title: Text(task.title),
      content: Text('Start Date: ${task.startDate}\nEnd Date: ${task.endDate}'),
      actions: [
        TextButton(
          onPressed: () => Navigator.pop(context),
          child: Text('OK'),
        ),
      ],
    ),
  );
},
回到顶部