Flutter步进器插件another_stepper的使用
Flutter步进器插件another_stepper的使用
another_stepper
是一个Flutter包,用于创建可高度自定义的水平和垂直步进器。本文将详细介绍如何使用该插件,并提供完整的示例代码。
Getting started
添加依赖
在 pubspec.yaml
文件的 dependencies
部分添加以下内容:
dependencies:
another_stepper: <latest_version>
请确保用最新版本号替换 <latest_version>
。
导入包
在你的Dart文件中导入 another_stepper
包:
import 'package:another_stepper/another_stepper.dart';
示例 AnotherStepper
下面是一个完整的示例,展示了如何使用 AnotherStepper
创建一个步进器。
Dummy StepperData
首先,我们定义一些示例数据:
List<StepperData> stepperData = [
StepperData(
title: StepperText(
"Order Placed",
textStyle: const TextStyle(
color: Colors.grey,
),
),
subtitle: StepperText("Your order has been placed"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_one, color: Colors.white),
)),
StepperData(
title: StepperText("Preparing"),
subtitle: StepperText("Your order is being prepared"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_two, color: Colors.white),
)),
StepperData(
title: StepperText("On the way"),
subtitle: StepperText(
"Our delivery executive is on the way to deliver your item"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_3, color: Colors.white),
)),
StepperData(
title: StepperText("Delivered",
textStyle: const TextStyle(color: Colors.grey)),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.all(Radius.circular(30))),
),
),
];
创建步进器
接下来,我们将这些数据用于创建一个步进器:
import 'package:another_stepper/another_stepper.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
List<StepperData> stepperData = [
StepperData(
title: StepperText(
"Order Placed",
textStyle: const TextStyle(
color: Colors.grey,
),
),
subtitle: StepperText("Your order has been placed"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_one, color: Colors.white),
)),
StepperData(
title: StepperText("Preparing"),
subtitle: StepperText("Your order is being prepared"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_two, color: Colors.white),
)),
StepperData(
title: StepperText("On the way"),
subtitle: StepperText(
"Our delivery executive is on the way to deliver your item"),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.green,
borderRadius: BorderRadius.all(Radius.circular(30))),
child: const Icon(Icons.looks_3, color: Colors.white),
)),
StepperData(
title: StepperText("Delivered",
textStyle: const TextStyle(color: Colors.grey)),
iconWidget: Container(
padding: const EdgeInsets.all(8),
decoration: const BoxDecoration(
color: Colors.redAccent,
borderRadius: BorderRadius.all(Radius.circular(30))),
)),
];
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
children: [
Padding(
padding: const EdgeInsets.only(top: 20, left: 20),
child: AnotherStepper(
stepperList: stepperData,
stepperDirection: Axis.horizontal,
iconWidth: 40,
iconHeight: 40,
activeBarColor: Colors.green,
inActiveBarColor: Colors.grey,
inverted: true,
verticalGap: 30,
activeIndex: 1,
barThickness: 8,
),
),
],
),
),
);
}
}
自定义配置
你可以通过修改参数来自定义步进器的外观和行为。例如:
-
Horizontal(Default)
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.horizontal, iconWidth: 40, iconHeight: 40, )
-
Vertical(Default)
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, iconWidth: 40, iconHeight: 40, )
-
Horizontal Inverted
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.horizontal, inverted: true, )
-
Vertical Inverted
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, inverted: true, )
-
Active index
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, activeIndex: 2, )
-
Vertical Gap
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, activeIndex: 2, gap: 60, )
-
Horizontal Gap
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.horizontal, activeIndex: 2, gap: 60, )
-
Bar Thickness
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, activeIndex: 2, barThickness: 8, )
-
Custom Dot and Bar
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, dotWidget: Container( padding: EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.all(Radius.circular(30)) ), child: Icon(Icons.navigate_next_sharp, color: Colors.white), ), activeBarColor: Colors.red, inActiveBarColor: Colors.grey, activeIndex: 1, )
-
Custom Dot, Bar, Dot thickness and long text
AnotherStepper( stepperList: stepperData, stepperDirection: Axis.vertical, dotWidget: Container( padding: EdgeInsets.all(8), decoration: BoxDecoration( color: Colors.red, borderRadius: BorderRadius.all(Radius.circular(30)) ), child: Icon(Icons.fastfood, color: Colors.white), ), activeBarColor: Colors.red, inActiveBarColor: Colors.grey, activeIndex: 2, barThickness: 8, )
通过这些配置,你可以轻松地创建出符合需求的步进器。希望这个指南对你有所帮助!
更多关于Flutter步进器插件another_stepper的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复