Flutter自定义步骤器插件cool_stepper_custom的使用

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

Flutter自定义步骤器插件cool_stepper_custom的使用

CoolStepperCustom 是一个基于 CoolStepper 的小部件,用于显示逐步操作序列,并修复了一些 bug 并增加了额外的定制功能。

使用

要使用此包,请在 pubspec.yaml 文件中添加 cool_stepper_custom 作为依赖项。然后,在你的文件中导入它:

import 'package:cool_stepper_custom/cool_stepper_custom.dart';

示例

以下是一个完整的示例,展示了如何使用 CoolStepperCustom 插件。

示例代码

import 'package:cool_stepper_custom/cool_stepper_custom.dart';
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';

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

class MyApp extends StatelessWidget {
  // 这个小部件是您的应用的根。
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Cool Stepper',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
        fontFamily: GoogleFonts.poppins().fontFamily,
      ),
      debugShowCheckedModeBanner: false,
      home: MyHomePage(title: 'Cool Stepper'),
    );
  }
}

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

  final String? title;

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

class _MyHomePageState extends State<MyHomePage> {
  final _formKey = GlobalKey<FormState>();
  String? selectedRole = 'Writer';
  final TextEditingController _nameCtrl = TextEditingController();
  final TextEditingController _emailCtrl = TextEditingController();

  [@override](/user/override)
  Widget build(BuildContext context) {
    final steps = [
      CoolStepCustom(
        title: 'Basic Information',
        subtitle: 'Please fill some of the basic information to get started',
        content: Form(
          key: _formKey,
          child: Column(
            children: [
              _buildTextField(
                labelText: 'Name',
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Name is required';
                  }
                  return null;
                },
                controller: _nameCtrl,
              ),
              _buildTextField(
                labelText: 'Email Address',
                validator: (value) {
                  if (value!.isEmpty) {
                    return 'Email address is required';
                  }
                  return null;
                },
                controller: _emailCtrl,
              ),
            ],
          ),
        ),
        validation: () {
          if (!_formKey.currentState!.validate()) {
            return 'Fill form correctly';
          }
          return null;
        },
      ),
      CoolStepCustom(
        title: 'Select your role',
        subtitle: 'Choose a role that better defines you',
        content: Container(
          child: Row(
            children: <Widget>[
              _buildSelector(
                context: context,
                name: 'Writer',
              ),
              SizedBox(width: 5.0),
              _buildSelector(
                context: context,
                name: 'Editor',
              ),
            ],
          ),
        ),
        validation: () {
          return null;
        },
      ),
    ];

    final stepper = CoolStepperCustom(
      showErrorSnackbar: false,
      onCompleted: () {
        print('Steps completed!');
      },
      steps: steps,
      config: CoolStepperCustomConfig(
        backText: 'Previous',
        backTextStyle: Theme.of(context)
            .primaryTextTheme
            .button!
            .copyWith(color: Colors.grey),
        backButtonStyle: TextButton.styleFrom(backgroundColor: Colors.white),
        backButtonPadding: EdgeInsets.all(10.0),
        nextText: 'Next',
        nextTextStyle: Theme.of(context)
            .primaryTextTheme
            .button!
            .copyWith(color: Colors.white),
        nextButtonStyle: TextButton.styleFrom(
            backgroundColor: Theme.of(context).primaryColor),
        nextButtonPadding: EdgeInsets.all(10.0),
        finalText: 'Finish',
        finalTextStyle: Theme.of(context)
            .primaryTextTheme
            .button!
            .copyWith(color: Colors.white),
        finalButtonStyle: TextButton.styleFrom(backgroundColor: Colors.green),
        finalButtonPadding: EdgeInsets.all(10.0),
      ),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title!),
      ),
      body: Container(
        width: 800.0,
        child: stepper,
      ),
    );
  }

  Widget _buildTextField({
    String? labelText,
    FormFieldValidator<String>? validator,
    TextEditingController? controller,
  }) {
    return Padding(
      padding: const EdgeInsets.only(bottom: 20.0),
      child: TextFormField(
        decoration: InputDecoration(
          labelText: labelText,
        ),
        validator: validator,
        controller: controller,
      ),
    );
  }

  Widget _buildSelector({
    BuildContext? context,
    required String name,
  }) {
    final isActive = name == selectedRole;

    return Expanded(
      child: AnimatedContainer(
        duration: Duration(milliseconds: 200),
        curve: Curves.easeInOut,
        decoration: BoxDecoration(
          color: isActive ? Theme.of(context!).primaryColor : null,
          border: Border.all(
            width: 0,
          ),
          borderRadius: BorderRadius.circular(8.0),
        ),
        child: RadioListTile(
          value: name,
          activeColor: Colors.white,
          groupValue: selectedRole,
          onChanged: (String? v) {
            setState(() {
              selectedRole = v;
            });
          },
          title: Text(
            name,
            style: TextStyle(
              color: isActive ? Colors.white : null,
            ),
          ),
        ),
      ),
    );
  }
}

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

1 回复

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


cool_stepper_custom 是一个 Flutter 插件,用于创建自定义步骤器(stepper)。它允许你根据需求自定义每个步骤的样式和行为。以下是如何使用 cool_stepper_custom 插件的基本步骤。

1. 添加依赖

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

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

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

2. 导入包

在你的 Dart 文件中导入 cool_stepper_custom 包:

import 'package:cool_stepper_custom/cool_stepper_custom.dart';

3. 创建步骤

cool_stepper_custom 使用 CoolStep 类来定义每个步骤。每个步骤可以包含标题、内容、以及自定义的按钮等。

final steps = [
  CoolStep(
    title: 'Step 1',
    subtitle: 'Enter your name',
    content: Column(
      children: [
        TextFormField(
          decoration: InputDecoration(labelText: 'Name'),
        ),
      ],
    ),
  ),
  CoolStep(
    title: 'Step 2',
    subtitle: 'Enter your email',
    content: Column(
      children: [
        TextFormField(
          decoration: InputDecoration(labelText: 'Email'),
        ),
      ],
    ),
  ),
  CoolStep(
    title: 'Step 3',
    subtitle: 'Confirmation',
    content: Column(
      children: [
        Text('Please confirm your information.'),
      ],
    ),
  ),
];

4. 使用 CoolStepper 组件

在你的页面中使用 CoolStepper 组件,并传入上面定义的步骤。

class MyStepperPage extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Stepper'),
      ),
      body: CoolStepper(
        steps: steps,
        onCompleted: () {
          // 当所有步骤完成时执行
          print('All steps completed!');
        },
      ),
    );
  }
}

5. 自定义主题和样式

cool_stepper_custom 允许你自定义步骤器的样式。你可以通过 CoolStepperTheme 来设置主题。

CoolStepper(
  steps: steps,
  onCompleted: () {
    print('All steps completed!');
  },
  theme: CoolStepperTheme(
    primaryColor: Colors.blue,
    iconColor: Colors.white,
    titleTextStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
    subtitleTextStyle: TextStyle(fontSize: 16, color: Colors.grey),
  ),
);

6. 控制步骤导航

你可以通过 CoolStepperController 来控制步骤导航。

final _controller = CoolStepperController();

CoolStepper(
  controller: _controller,
  steps: steps,
  onCompleted: () {
    print('All steps completed!');
  },
);

// 在某个事件中跳转到下一个步骤
_controller.next();

7. 完整示例

以下是一个完整的示例,展示了如何使用 cool_stepper_custom

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

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

class MyApp extends StatelessWidget {
  [@override](/user/override)
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyStepperPage(),
    );
  }
}

class MyStepperPage extends StatelessWidget {
  final _controller = CoolStepperController();

  final steps = [
    CoolStep(
      title: 'Step 1',
      subtitle: 'Enter your name',
      content: Column(
        children: [
          TextFormField(
            decoration: InputDecoration(labelText: 'Name'),
          ),
        ],
      ),
    ),
    CoolStep(
      title: 'Step 2',
      subtitle: 'Enter your email',
      content: Column(
        children: [
          TextFormField(
            decoration: InputDecoration(labelText: 'Email'),
          ),
        ],
      ),
    ),
    CoolStep(
      title: 'Step 3',
      subtitle: 'Confirmation',
      content: Column(
        children: [
          Text('Please confirm your information.'),
        ],
      ),
    ),
  ];

  [@override](/user/override)
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Custom Stepper'),
      ),
      body: CoolStepper(
        controller: _controller,
        steps: steps,
        onCompleted: () {
          print('All steps completed!');
        },
        theme: CoolStepperTheme(
          primaryColor: Colors.blue,
          iconColor: Colors.white,
          titleTextStyle: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
          subtitleTextStyle: TextStyle(fontSize: 16, color: Colors.grey),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _controller.next();
        },
        child: Icon(Icons.arrow_forward),
      ),
    );
  }
}
回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!