Flutter表单步进器插件stepper_flutter_form的使用

Flutter表单步进器插件stepper_flutter_form的使用

使用此库,您可以设置表单的状态,并跳转到任意阶段,直接到达该阶段。此外,您还可以自定义其颜色。

图像描述

平台支持

Android iOS MacOS Web Linux Windows
✔️ ✔️ ✔️ ✔️ ✔️ ✔️

开始使用

在Flutter中添加依赖项

$ flutter pub add stepper_flutter_form

这将在您的包的pubspec.yaml文件中添加如下行(并隐式运行flutter pub get):

dependencies:
  stepper_flutter_form: ^1.0.2+1

或者,您的编辑器可能支持flutter pub get。请查阅您的编辑器文档以了解更多信息。

示例

首先在您的类中导入该库:

import 'package:stepper_flutter/stepper_flutter_form.dart';

示例代码:

import 'package:stepper_flutter/stepper_flutter_form.dart';

// 此小部件是您的应用的根。
[@override](/user/override)
Widget build(BuildContext context) {
  /// 美观按钮及其属性
  return FormStepper(
    items: widget.items, 
    selectedColor: Colors.green, 
    unSelectedColor: Colors.red, 
    inProcess: 5, 
    menuTitleSize: 12, 
    height: 80, 
    onItemClicked: (index) {
      print('Clicked Index: ' + index.toString());
    },
  );
}

完整示例代码

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:stepper_flutter_form/stepper_flutter_form.dart';

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

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

  // 此小部件是您的应用的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    List<String> item = [];
    item.add("注册");
    item.add("填写详细信息");
    item.add("上传文档");
    item.add("电子签名");
    item.add("上传文档");
    item.add("电子签名");
    item.add("上传文档");
    item.add("电子签名");

    return MaterialApp(
      title: 'Flutter Stepper Form Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(
        title: 'Flutter Stepper Form Demo', 
        items: item, 
        selectedColor: Colors.green, 
        unSelectedColor: Colors.red, 
        inProcess: 5, 
        menuTitleSize: 15,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  int inProcess = 1;
  Color selectedColor;
  Color unSelectedColor;
  final String title;
  List<String> items;
  double menuTitleSize;

  MyHomePage({
    super.key,
    required this.title,
    required this.items,
    required this.inProcess,
    required this.selectedColor,
    required this.unSelectedColor,
    required this.menuTitleSize,
  });

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

class _MyHomePageState extends State<MyHomePage> {
  List<String> item2 = [];
  List<String> item3 = [];

  [@override](/user/override)
  void initState() {
    super.initState();

    item2.add("基本信息");
    item2.add("联系信息");
    item2.add("地址");
    item2.add("文档");

    item3.add("PAN");
    item3.add("Aadhar");
    item3.add("银行信息");
    item3.add("文档");
    item3.add("电子签名");
  }

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Container(
        color: Colors.white,
        child: Column(
          children: [
            SizedBox(height: 20),
            Text("可滚动的步进器"),
            SizedBox(height: 10),
            FormStepper(
              items: widget.items, 
              selectedColor: Colors.green, 
              unSelectedColor: Colors.red, 
              inProcess: 5, 
              menuTitleSize: 12, 
              height: 80, 
              onItemClicked: (index) {
                print('Clicked Index: ' + index.toString());
              },
            ),
            SizedBox(height: 20),
            Text("具有自定义颜色的步进器"),
            SizedBox(height: 10),
            FormStepper(
              items: item2, 
              selectedColor: Colors.blue, 
              unSelectedColor: Colors.black, 
              inProcess: 1, 
              menuTitleSize: 12, 
              height: 80, 
              onItemClicked: (index) {
                print('Clicked Index: ' + index.toString());
              },
            ),
            SizedBox(height: 20),
            Text("具有自定义颜色的步进器"),
            SizedBox(height: 10),
            FormStepper(
              items: item3, 
              selectedColor: Colors.green, 
              unSelectedColor: Colors.pinkAccent, 
              inProcess: 0, 
              menuTitleSize: 12, 
              height: 80, 
              onItemClicked: (index) {
                print('Clicked Index: ' + index.toString());
              },
            ),
            SizedBox(height: 20),
            Text("具有自定义颜色的步进器"),
            SizedBox(height: 10),
            FormStepper(
              items: item3, 
              selectedColor: Colors.green, 
              unSelectedColor: Colors.pinkAccent, 
              inProcess: 2, 
              menuTitleSize: 12, 
              height: 80, 
              onItemClicked: (index) {
                print('Clicked Index: ' + index.toString());
              },
            ),
          ],
        ),
      ),
    );
  }
}

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

1 回复

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


stepper_flutter_form 是一个用于 Flutter 的插件,用于创建带有表单的步进器(Stepper)。它允许用户通过多个步骤来完成表单填写,每个步骤可以包含不同的表单字段。

以下是如何使用 stepper_flutter_form 插件的基本步骤:

1. 添加依赖

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

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

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

2. 导入插件

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

import 'package:stepper_flutter_form/stepper_flutter_form.dart';

3. 创建步进器表单

你可以使用 StepperFlutterForm 组件来创建一个步进器表单。以下是一个简单的示例:

class MyForm extends StatefulWidget {
  @override
  _MyFormState createState() => _MyFormState();
}

class _MyFormState extends State<MyForm> {
  final _formKey = GlobalKey<FormState>();
  int _currentStep = 0;

  List<Step> _steps = [
    Step(
      title: Text('Step 1'),
      content: TextFormField(
        decoration: InputDecoration(labelText: 'Name'),
        validator: (value) {
          if (value == null || value.isEmpty) {
            return 'Please enter your name';
          }
          return null;
        },
      ),
    ),
    Step(
      title: Text('Step 2'),
      content: TextFormField(
        decoration: InputDecoration(labelText: 'Email'),
        validator: (value) {
          if (value == null || value.isEmpty) {
            return 'Please enter your email';
          }
          return null;
        },
      ),
    ),
    Step(
      title: Text('Step 3'),
      content: TextFormField(
        decoration: InputDecoration(labelText: 'Password'),
        obscureText: true,
        validator: (value) {
          if (value == null || value.isEmpty) {
            return 'Please enter your password';
          }
          return null;
        },
      ),
    ),
  ];

  void _submitForm() {
    if (_formKey.currentState!.validate()) {
      // 表单验证通过,处理表单数据
      print('Form submitted successfully');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stepper Form'),
      ),
      body: Form(
        key: _formKey,
        child: StepperFlutterForm(
          steps: _steps,
          currentStep: _currentStep,
          onStepContinue: () {
            if (_currentStep < _steps.length - 1) {
              setState(() {
                _currentStep += 1;
              });
            } else {
              _submitForm();
            }
          },
          onStepCancel: () {
            if (_currentStep > 0) {
              setState(() {
                _currentStep -= 1;
              });
            }
          },
        ),
      ),
    );
  }
}

4. 运行应用

在你的 main.dart 文件中运行应用:

void main() {
  runApp(MaterialApp(
    home: MyForm(),
  ));
}
回到顶部