Flutter页面导航插件flutter_page_stepper的使用
Flutter页面导航插件flutter_page_stepper的使用
特性
- 可定制:可以自定义进度条和指示器的外观。
- 灵活:支持任意数量的步骤/页面。
使用方法
安装依赖并导入包
在你的 pubspec.yaml
文件中添加以下依赖:
dependencies:
flutter_page_stepper: ^x.y.z
然后,在你的 Dart 文件中导入该包:
import 'package:flutter_page_stepper/flutter_page_stepper.dart';
添加FlutterPageStepper到你的代码中
你可以通过设置 activeColor
(激活颜色)、stepperLength
(步骤数)、currentIndex
(当前索引)以及你希望显示的页面列表来添加 FlutterPageStepper
。
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) => MaterialApp(
title: 'FlutterPageStepper Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const FlutterPageStepperExample(),
);
}
class FlutterPageStepperExample extends StatefulWidget {
const FlutterPageStepperExample({super.key});
[@override](/user/override)
State<FlutterPageStepperExample> createState() => _FlutterPageStepperExampleState();
}
class _FlutterPageStepperExampleState extends State<FlutterPageStepperExample> {
int currentPage = 0;
void moveToNext() {
if (currentPage < 3) {
setState(() {
currentPage++;
});
}
}
void moveToPrevious() {
if (currentPage > 0) {
setState(() {
currentPage--;
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FlutterPageStepper(
headingVerticalPadding: 30,
stepperLength: 3,
currentIndex: currentPage,
activeColor: Colors.brown,
textStyle: const TextStyle(
color: Colors.white,
),
children: [
StepperPage(
content: 1,
onNext: () {
moveToNext();
},
),
StepperPage(
content: 2,
onPrevious: () {
moveToPrevious();
},
onNext: () {
moveToNext();
},
),
StepperPage(
content: 3,
nextButtonTitle: "提交",
onPrevious: () {
moveToPrevious();
},
onNext: () {},
),
],
),
),
);
}
}
参数
这些属性用于自定义进度条小部件的行为和外观。
属性名 | 类型 | 是否必需 | 描述 |
---|---|---|---|
stepperLength |
int |
是 | 进度条中的步骤数。 |
currentIndex |
int |
是 | 当前进度条的索引。 |
children |
List<Widget> |
是 | 每个步骤关联的页面或小部件。 |
activeColor |
Color |
是 | 进度条激活时的背景色。 |
inactiveColor |
Color? |
否 | 进度条非激活时的背景色,默认为 activeColor 的 0.5 透明度。 |
textStyle |
TextStyle? |
否 | 指示器中数字的文本样式。 |
headingVerticalPadding |
double? |
否 | 进度条头部的垂直填充。 |
dividerWidth |
double? |
否 | 每个指示器之间的分隔线宽度。 |
indicatorSize |
double? |
否 | 指示器的大小。 |
示例代码
以下是完整的示例代码:
import 'package:flutter/material.dart';
import 'package:flutter_page_stepper/flutter_page_stepper.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
[@override](/user/override)
Widget build(BuildContext context) => MaterialApp(
title: 'FlutterPageStepper Example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const FlutterPageStepperExample(),
);
}
class FlutterPageStepperExample extends StatefulWidget {
const FlutterPageStepperExample({super.key});
[@override](/user/override)
State<FlutterPageStepperExample> createState() => _FlutterPageStepperExampleState();
}
class _FlutterPageStepperExampleState extends State<FlutterPageStepperExample> {
int currentPage = 0;
void moveToNext() {
if (currentPage < 3) {
setState(() {
currentPage++;
});
}
}
void moveToPrevious() {
if (currentPage > 0) {
setState(() {
currentPage--;
});
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: FlutterPageStepper(
headingVerticalPadding: 30,
stepperLength: 3,
currentIndex: currentPage,
activeColor: Colors.brown,
textStyle: const TextStyle(
color: Colors.white,
),
children: [
StepperPage(
content: 1,
onNext: () {
moveToNext();
},
),
StepperPage(
content: 2,
onPrevious: () {
moveToPrevious();
},
onNext: () {
moveToNext();
},
),
StepperPage(
content: 3,
nextButtonTitle: "提交",
onPrevious: () {
moveToPrevious();
},
onNext: () {},
),
],
),
),
);
}
}
class StepperPage extends StatelessWidget {
final int content;
final Function()? onPrevious;
final Function() onNext;
final String? nextButtonTitle;
const StepperPage({
super.key,
required this.content,
this.onPrevious,
required this.onNext,
this.nextButtonTitle,
});
[@override](/user/override)
Widget build(BuildContext context) {
return Column(
children: [
const Spacer(),
Text(
"Page: $content",
style: const TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
const Spacer(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
onPrevious != null
? MaterialButton(
onPressed: onPrevious,
child: const Text(
"上一页",
style: TextStyle(
color: Colors.brown,
fontWeight: FontWeight.bold,
),
),
)
: const SizedBox(),
MaterialButton(
onPressed: onNext,
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 15, vertical: 6),
decoration: BoxDecoration(
color: Colors.brown,
borderRadius: BorderRadius.circular(20)),
child: Text(
nextButtonTitle ?? "下一页",
style: const TextStyle(
fontWeight: FontWeight.bold, color: Colors.white),
),
),
)
],
),
],
);
}
}
更多关于Flutter页面导航插件flutter_page_stepper的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
更多关于Flutter页面导航插件flutter_page_stepper的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
当然,以下是如何在Flutter应用中使用flutter_page_stepper
插件来实现页面导航的一个示例。flutter_page_stepper
插件允许你创建分步向导页面,非常适合表单填写或者引导用户完成多步骤流程。
首先,确保你已经在pubspec.yaml
文件中添加了flutter_page_stepper
依赖:
dependencies:
flutter:
sdk: flutter
flutter_page_stepper: ^x.y.z # 请替换为最新版本号
然后,运行flutter pub get
来安装依赖。
接下来,在你的Flutter应用中,你可以按照以下步骤使用flutter_page_stepper
:
示例代码
import 'package:flutter/material.dart';
import 'package:flutter_page_stepper/flutter_page_stepper.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Page Stepper Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: StepperDemo(),
);
}
}
class StepperDemo extends StatefulWidget {
@override
_StepperDemoState createState() => _StepperDemoState();
}
class _StepperDemoState extends State<StepperDemo> {
final List<Step> steps = List.generate(
3,
(index) => Step(
title: Text('Step ${index + 1} title'),
content: Container(
alignment: Alignment.center,
child: Text('This is the content of step ${index + 1}.'),
),
isActive: index == 0, // 只有第一个步骤是活跃的
),
);
int currentStep = 0;
void _submit() {
// 在这里处理提交逻辑,比如验证表单字段
setState(() {
if (currentStep < steps.length - 1) {
currentStep += 1;
} else {
// 完成所有步骤后的逻辑
print('All steps completed!');
}
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Stepper Demo'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
PageStepper(
steps: steps,
currentStep: currentStep,
onStepTapped: (int stepIndex) {
setState(() {
currentStep = stepIndex;
});
},
onStepContinue: _submit,
onStepCancel: () {
setState(() {
if (currentStep > 0) {
currentStep -= 1;
} else {
// 取消所有步骤的逻辑
print('Cancelled all steps.');
}
});
},
),
],
),
),
);
}
}
代码说明
-
依赖添加:在
pubspec.yaml
中添加flutter_page_stepper
依赖。 -
创建步骤:在
StepperDemo
类中,我们创建了一个包含三个步骤的列表。每个步骤都有一个标题和内容。 -
管理当前步骤:我们用一个
currentStep
变量来跟踪当前活动的步骤。 -
页面导航:使用
PageStepper
组件来显示步骤导航。steps
:传递步骤列表。currentStep
:当前活动的步骤索引。onStepTapped
:当用户点击某个步骤时触发。onStepContinue
:当用户点击继续按钮时触发,在这里处理步骤前进的逻辑。onStepCancel
:当用户点击取消按钮时触发,处理步骤回退的逻辑。
这个示例展示了如何使用flutter_page_stepper
插件创建一个简单的分步向导。你可以根据需求进一步自定义步骤的内容和样式。