Flutter步骤进度指示插件step_progress_indicator的使用

发布于 1周前 作者 htzhanglong 来自 Flutter

Flutter步骤进度指示插件step_progress_indicator的使用

Step Progress Indicator 插件介绍

Step Progress Indicator 是一个开源的Flutter包,由Sandro Maglione开发,它提供了一系列可选和未选中的步骤来构成进度条。这个插件可以帮助开发者轻松地在应用中创建直观的进度指示器。

插件特性

  • Horizontal and Vertical Indicators 支持水平和垂直两种方向的进度条。
  • Customizable Steps 可以自定义每个步骤的颜色、大小等属性。
  • Gradient Support 支持渐变色效果。
  • Circular Indicators 提供圆形进度条选项。
  • Interactive 用户可以点击进度条上的步骤触发事件。

安装与配置

在您的pubspec.yaml文件中添加依赖:

dependencies:
  flutter:
    sdk: flutter
  step_progress_indicator: ^1.0.2

然后运行命令 flutter pub get 来安装此包。

示例代码

以下是完整的示例demo,展示了如何使用StepProgressIndicatorCircularStepProgressIndicator:

main.dart - 应用入口

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

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  int _bottomBarIndex = 0;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Step Progress Bar',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      debugShowCheckedModeBanner: false,
      home: SafeArea(
        child: Scaffold(
          appBar: AppBar(
            title: Text('Step Progress Indicator Demo'),
          ),
          body: _bottomBarIndex == 0 
              ? HorizontalBarExample() 
              : VerticalBarExample(),
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: _bottomBarIndex,
            onTap: (index) => setState(() => _bottomBarIndex = index),
            items: [
              BottomNavigationBarItem(
                icon: Icon(Icons.horizontal_split),
                label: 'Horizontal'
              ),
              BottomNavigationBarItem(
                icon: Icon(Icons.vertical_split),
                label: 'Vertical'
              ),
            ],
          ),
        ),
      ),
    );
  }
}

horizontal_bar_example.dart - 水平进度条示例

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

class HorizontalBarExample extends StatelessWidget {
  final List<String> steps = ["Step 1", "Step 2", "Step 3", "Step 4", "Step 5"];

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[
        Text("Horizontal Step Progress Indicator"),
        SizedBox(height: 20),
        StepProgressIndicator(
          totalSteps: steps.length,
          currentStep: 3, // 假设当前为第3步
          size: 30,
          padding: 8,
          selectedColor: Colors.blue,
          unselectedColor: Colors.grey,
          roundedEdges: Radius.circular(10),
          customStep: (int index, Color color, double width) {
            return Container(
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: color,
              ),
              child: Center(
                child: Text(
                  "${index + 1}",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            );
          },
        ),
        SizedBox(height: 20),
        ...steps.asMap().entries.map((entry) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: Row(
              children: [
                CircleAvatar(
                  backgroundColor: entry.key < 3 ? Colors.blue : Colors.grey,
                  child: Text("${entry.key + 1}"),
                ),
                SizedBox(width: 10),
                Text(entry.value),
              ],
            ),
          );
        }).toList(),
      ],
    );
  }
}

vertical_bar_example.dart - 垂直进度条示例

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

class VerticalBarExample extends StatelessWidget {
  final List<String> steps = ["Step 1", "Step 2", "Step 3", "Step 4", "Step 5"];

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Text("Vertical Step Progress Indicator"),
        SizedBox(height: 20),
        StepProgressIndicator(
          totalSteps: steps.length,
          currentStep: 3, // 假设当前为第3步
          direction: Axis.vertical,
          size: 30,
          padding: 8,
          selectedColor: Colors.green,
          unselectedColor: Colors.grey,
          roundedEdges: Radius.circular(10),
          customStep: (int index, Color color, double height) {
            return Container(
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: color,
              ),
              child: Center(
                child: Text(
                  "${index + 1}",
                  style: TextStyle(color: Colors.white),
                ),
              ),
            );
          },
        ),
        SizedBox(height: 20),
        Expanded(
          child: ListView.builder(
            itemCount: steps.length,
            itemBuilder: (context, index) {
              return ListTile(
                leading: CircleAvatar(
                  backgroundColor: index < 3 ? Colors.green : Colors.grey,
                  child: Text("${index + 1}"),
                ),
                title: Text(steps[index]),
              );
            },
          ),
        ),
      ],
    );
  }
}

以上代码展示了如何在一个简单的Flutter应用程序中集成并使用step_progress_indicator插件。您可以根据需要调整参数来自定义进度条的外观和行为。希望这些信息对您有所帮助!


更多关于Flutter步骤进度指示插件step_progress_indicator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter步骤进度指示插件step_progress_indicator的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


当然,以下是如何在Flutter项目中使用step_progress_indicator插件的详细代码示例。这个插件可以帮助你创建一个步骤进度指示器,非常适合用于引导用户完成多步骤流程。

1. 添加依赖

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

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

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

2. 导入插件

在你的Dart文件中导入插件:

import 'package:step_progress_indicator/step_progress_indicator.dart';

3. 使用步骤进度指示器

下面是一个完整的示例,展示如何在Flutter应用中使用step_progress_indicator

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

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

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

class StepProgressIndicatorScreen extends StatefulWidget {
  @override
  _StepProgressIndicatorScreenState createState() => _StepProgressIndicatorScreenState();
}

class _StepProgressIndicatorScreenState extends State<StepProgressIndicatorScreen> {
  int currentStep = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Step Progress Indicator Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            StepProgressIndicator(
              totalSteps: 5,
              currentStep: currentStep,
              size: 24.0,
              padding: 8.0,
              stepDoneColor: Colors.green,
              stepUndoneColor: Colors.grey,
              stepActiveColor: Colors.blue,
              lineDoneColor: Colors.green.withOpacity(0.5),
              lineUndoneColor: Colors.grey.withOpacity(0.5),
              lineActiveColor: Colors.blue.withOpacity(0.5),
              customStepWidget: (step, isActive) {
                return Container(
                  alignment: Alignment.center,
                  decoration: BoxDecoration(
                    border: Border.all(
                      color: isActive ? Colors.blue : Colors.transparent,
                      width: 2.0,
                    ),
                    borderRadius: BorderRadius.circular(12.0),
                  ),
                  child: Text(
                    'Step ${step + 1}',
                    style: TextStyle(
                      color: isActive ? Colors.blue : Colors.black,
                      fontSize: 16.0,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                );
              },
              onStepTapped: (step) {
                setState(() {
                  currentStep = step;
                });
              },
            ),
            SizedBox(height: 24.0),
            Expanded(
              child: ListView.builder(
                itemCount: 5,
                itemBuilder: (context, index) {
                  return Padding(
                    padding: const EdgeInsets.symmetric(vertical: 16.0),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          'Step ${index + 1} Content',
                          style: TextStyle(
                            fontSize: 20.0,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(height: 8.0),
                        Text(
                          'This is the content for step ${index + 1}.',
                          style: TextStyle(
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
          ],
        ),
      ),
      bottomNavigationBar: BottomNavigationBar(
        items: [
          BottomNavigationBarItem(
            icon: Icon(Icons.arrow_back_ios),
            label: 'Previous',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.arrow_forward_ios),
            label: 'Next',
          ),
        ],
        currentIndex: 0,
        onTap: (index) {
          if (index == 0 && currentStep > 0) {
            setState(() {
              currentStep -= 1;
            });
          } else if (index == 1 && currentStep < 4) {
            setState(() {
              currentStep += 1;
            });
          }
        },
      ),
    );
  }
}

解释

  1. 依赖添加:在pubspec.yaml中添加step_progress_indicator依赖。
  2. 导入插件:在Dart文件中导入step_progress_indicator
  3. 构建UI
    • 使用StepProgressIndicator小部件来显示步骤进度。
    • totalSteps:总步骤数。
    • currentStep:当前步骤。
    • customStepWidget:自定义每个步骤的显示。
    • onStepTapped:点击步骤时的回调。
  4. 内容展示:使用ListView.builder来展示每个步骤的内容。
  5. 底部导航:使用BottomNavigationBar来提供“上一步”和“下一步”的功能。

这个示例展示了如何创建一个基本的步骤进度指示器,并允许用户通过底部导航栏在步骤之间切换。你可以根据需要进一步自定义和扩展这个示例。

回到顶部