Flutter进度管理插件progress_stepper的使用

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

Flutter进度管理插件progress_stepper的使用

progress_stepper

此包可用于创建自定义的进度条。通过该插件,你可以轻松地在Flutter应用中实现不同风格和功能的进度条。

主要类

描述
ProgressStepper 用于创建自定义进度条的核心类
ProgressStepWithChevron 带箭头形状的自定义步骤
ProgressStepWithArrow 带尖角的自定义步骤
ProgressStepWithBluntChevron 不带尖角的箭头形状自定义步骤

使用方法

方法1:默认配置

只需要初始化ProgressStepper并设置必要的参数,它将创建一个默认样式的进度条(带有箭头样式)。

ProgressStepper(
   width: 300,
   currentStep: 1,
)

钝头两端进度条
ProgressStepper(
   width: 300,
   height: 20,
   padding: 1,
   currentStep: 1,
   bluntHead: true,
   bluntTail: true,
   color: Colors.grey,
   progressColor: Colors.deepPurple,
   labels: const <String>['A', 'B', 'C', 'D', 'E'],
   defaultTextStyle: const TextStyle(
      color: Colors.purple,
      fontWeight: FontWeight.w500,
   ),
   selectedTextStyle: const TextStyle(
      color: Colors.white,
      fontWeight: FontWeight.bold,
   ),         
)

方法2:自定义构建器

可以使用构建器来返回自己的自定义步骤或根据需要使用提供的步骤。

ProgressStepper(
   width: 200,
   height: 25,
   stepCount: 3,
   builder: (int index, double widthOfStep) {
      if (index == 1) {
         return ProgressStepWithArrow(
            width: widthOfStep,
            height: 25,
            defaultColor: Colors.red,
            progressColor: Colors.green,
            borderWidth: 1,
            wasCompleted: true,
            child: Center(
               child: Text(
                  index.toString(),
                  style: const TextStyle(
                     color: Colors.white,
                  ),
               ),
            ),
         );
      }
      return ProgressStepWithChevron(
         width: widthOfStep,
         height: 25,
         defaultColor: Colors.red,
         progressColor: Colors.green,
         borderWidth: 1,
         wasCompleted: false,
         child: Center(
            child: Text(
               index.toString(),
               textAlign: TextAlign.center,
               style: const TextStyle(
                  color: Colors.white,
               ),
            ),
         ),
      );
   },
)

示例Demo

下面是一个完整的示例代码,演示了如何在Flutter项目中使用progress_stepper插件。

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

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

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

  @override
  Widget build(BuildContext context) => MaterialApp(
        title: 'Progress Stepper Demo',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: const MyHomePage(title: 'Progress Stepper'),
      );
}

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

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) => Scaffold(
        appBar: AppBar(
          title: Text(widget.title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              // Chevron Stepper
              ProgressStepper(
                width: 300,
                currentStep: 1,
              ),
              Divider(),
              SizedBox(height: 10),

              // Custom Stepper with Arrow and Chevron
              ProgressStepper(
                width: 200,
                height: 25,
                stepCount: 3,
                builder: (int index, double widthOfStep) {
                  if (index == 1) {
                    return ProgressStepWithArrow(
                      width: widthOfStep,
                      height: 25,
                      defaultColor: Colors.red,
                      progressColor: Colors.green,
                      borderWidth: 1,
                      wasCompleted: true,
                      child: Center(
                        child: Text(
                          index.toString(),
                          style: const TextStyle(color: Colors.white),
                        ),
                      ),
                    );
                  }
                  return ProgressStepWithChevron(
                    width: widthOfStep,
                    height: 25,
                    defaultColor: Colors.red,
                    progressColor: Colors.green,
                    borderWidth: 1,
                    wasCompleted: false,
                    child: Center(
                      child: Text(
                        index.toString(),
                        textAlign: TextAlign.center,
                        style: const TextStyle(color: Colors.white),
                      ),
                    ),
                  );
                },
              ),
              Divider(),
              SizedBox(height: 10),

              // Blunt Stepper
              ProgressStepper(
                width: 300,
                height: 20,
                padding: 1,
                currentStep: 1,
                bluntHead: true,
                bluntTail: true,
                color: Colors.grey,
                progressColor: Colors.deepPurple,
                labels: const <String>['A', 'B', 'C', 'D', 'E'],
                defaultTextStyle: const TextStyle(
                  color: Colors.purple,
                  fontWeight: FontWeight.w500,
                ),
                selectedTextStyle: const TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ],
          ),
        ),
      );
}

以上代码展示了三种不同类型的进度条,包括默认箭头样式、自定义构建器以及钝头两端的进度条。你可以根据实际需求调整这些配置以适应你的应用界面。


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

1 回复

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


当然,下面是一个关于如何在Flutter中使用progress_stepper插件的示例代码。progress_stepper是一个用于显示和管理多步骤流程的UI组件。虽然Flutter官方并没有直接名为progress_stepper的插件,但我们可以使用Material Design中的Stepper组件来实现类似的功能。

首先,确保你的Flutter环境已经设置好,并且你的pubspec.yaml文件中已经添加了必要的依赖(虽然Stepper是Flutter Material组件库的一部分,所以不需要额外添加依赖)。

dependencies:
  flutter:
    sdk: flutter

接下来,我们可以创建一个简单的多步骤表单,使用Stepper组件来管理步骤的进度。

import 'package:flutter/material.dart';

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

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

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
  int _currentStep = 0;
  final _formKey = GlobalKey<FormState>();

  void _handleNext() {
    if (_formKey.currentState?.validate() ?? false) {
      _formKey.currentState?.save();
      setState(() {
        _currentStep = _currentStep + 1;
      });
    }
  }

  void _handleReset() {
    setState(() {
      _currentStep = 0;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stepper Demo'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Stepper(
          currentStep: _currentStep,
          steps: [
            Step(
              title: Text('Step 1: Enter your name'),
              content: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Form(
                  key: _formKey,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Name'),
                        validator: (value) {
                          if (value.isEmpty) {
                            return 'Please enter your name';
                          }
                          return null;
                        },
                        onSaved: (value) {
                          // You can save the value here
                        },
                      ),
                    ],
                  ),
                ),
              ),
              isActive: _currentStep >= 0,
            ),
            Step(
              title: Text('Step 2: Enter your email'),
              content: Padding(
                padding: const EdgeInsets.symmetric(vertical: 8.0),
                child: Form(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Email'),
                        keyboardType: TextInputType.emailAddress,
                        validator: (value) {
                          if (value.isEmpty || !value.contains('@')) {
                            return 'Please enter a valid email';
                          }
                          return null;
                        },
                        onSaved: (value) {
                          // You can save the value here
                        },
                      ),
                    ],
                  ),
                ),
              ),
              isActive: _currentStep >= 1,
            ),
            // Add more steps as needed
          ],
          type: StepperType.vertical, // or StepperType.horizontal
          onStepContinue: _handleNext,
          onStepCancel: _handleReset,
          onStepTapped: (step) {
            setState(() {
              _currentStep = step;
            });
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _currentStep < 1
            ? null
            : () {
                if (_formKey.currentState?.validate() ?? false) {
                  _formKey.currentState?.save();
                  // Perform final action, e.g., navigate to the next screen
                  // Navigator.pushNamed(context, '/nextScreen');
                }
              },
        tooltip: 'Submit',
        child: Icon(Icons.check),
      ),
    );
  }
}

这个示例展示了如何使用Stepper组件来管理一个简单的两步表单。每个步骤都包含一个表单字段,用户需要填写这些信息才能继续到下一步。_handleNext函数用于验证当前步骤的表单,如果验证通过,则移动到下一步。_handleReset函数用于重置到第一步。

你可以根据需要添加更多的步骤,并在每个步骤中添加不同的表单字段或内容。StepperType.verticalStepperType.horizontal允许你选择垂直或水平的步骤显示方式。

回到顶部