Flutter步进器组件插件simple_stepper_flutter的使用

Flutter步进器组件插件simple_stepper_flutter的使用

简介

这是一个简单的包,帮助你更快更方便地创建水平步进器。

安装

在你的项目 pubspec.yaml 文件中添加以下依赖:

dependencies:
  simple_stepper_flutter: ^1.0.0

然后运行以下命令来获取依赖:

flutter pub get

使用示例

SimpleStepperFlutter(
    children: const [
    "Step 1",
    "Step 2",
    "Step 3",
    "Step 4",
    "Step 5",
    ],   // 列表中的每个元素代表一个步骤的标题
    currentStep: 0, // 当前选中的步骤
    activeColor: Colors.blue, // 当前选中的步骤的颜色
    inactiveColor: Colors.grey, // 未选择的步骤的颜色
    completeColor: Colors.green, // 已完成步骤的颜色
    completeIcon: Icons.check, // 完成步骤时显示的图标
    dividerColor: Colors.grey, // 水平分隔线的颜色
    iconSize: 20, // 图标的大小
    padding: const EdgeInsets.symmetric(
        horizontal: 4,
        vertical: 4,
    ), // 步进器的内边距
)

示例代码

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

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

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

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

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {

  int _currentIndex = 0; // 当前选中的步骤索引

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: SimpleStepperFlutter(
          children: const [
            "Step 1",
            "Step 2",
            "Step 3",
            "Step 4",
            "Step 5",
          ],
          currentStep: _currentIndex,
          activeColor: Colors.blue,
          inactiveColor: Colors.grey,
          completeColor: Colors.green,
          completeIcon: Icons.check,
          dividerColor: Colors.grey,
          iconSize: 20,
          padding: const EdgeInsets.symmetric(
            horizontal: 4,
            vertical: 4,
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          setState(() {
            if (_currentIndex < 4) {
              _currentIndex++;
            } else if (_currentIndex == 4) {
              _currentIndex = 0;
            }
          });
        },
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}

更多关于Flutter步进器组件插件simple_stepper_flutter的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

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


simple_stepper_flutter 是一个用于 Flutter 的步进器组件插件,它允许你轻松地创建和管理多步流程。这个插件非常适合用于引导用户完成一系列步骤的场景,比如注册流程、设置向导等。

安装

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

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

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

基本用法

以下是一个简单的示例,展示如何使用 simple_stepper_flutter 创建一个步进器:

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: StepperExample(),
    );
  }
}

class StepperExample extends StatefulWidget {
  @override
  _StepperExampleState createState() => _StepperExampleState();
}

class _StepperExampleState extends State<StepperExample> {
  int _currentStep = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Simple Stepper Example'),
      ),
      body: SimpleStepper(
        currentStep: _currentStep,
        steps: [
          Step(
            title: Text('Step 1'),
            content: Text('This is the first step.'),
          ),
          Step(
            title: Text('Step 2'),
            content: Text('This is the second step.'),
          ),
          Step(
            title: Text('Step 3'),
            content: Text('This is the third step.'),
          ),
        ],
        onStepContinue: () {
          if (_currentStep < 2) {
            setState(() {
              _currentStep++;
            });
          }
        },
        onStepCancel: () {
          if (_currentStep > 0) {
            setState(() {
              _currentStep--;
            });
          }
        },
      ),
    );
  }
}

参数说明

  • currentStep: 当前步骤的索引。
  • steps: 一个 List<Step>,表示步进器的每个步骤。
  • onStepContinue: 当用户点击“继续”按钮时调用的回调函数。
  • onStepCancel: 当用户点击“取消”按钮时调用的回调函数。

Step 组件

每个 Step 组件包含以下属性:

  • title: 步骤的标题。
  • content: 步骤的内容。
  • state: 步骤的状态(可选),可以是 StepState.indexedStepState.editingStepState.completeStepState.disabled
  • isActive: 步骤是否为当前活动步骤(可选)。

自定义样式

你可以通过传递 stepperTheme 参数来自定义步进器的样式:

SimpleStepper(
  currentStep: _currentStep,
  steps: [
    // 步骤定义
  ],
  stepperTheme: StepperThemeData(
    activeStepColor: Colors.blue,
    inactiveStepColor: Colors.grey,
    lineColor: Colors.grey,
    stepTextColor: Colors.white,
  ),
  onStepContinue: () {
    // 继续逻辑
  },
  onStepCancel: () {
    // 取消逻辑
  },
);
回到顶部