实现步骤:
lib/common/components/slider_indicator.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import 'package:flutter/material.dart'; import 'package:flutter_woo_commerce_getx_learn/common/index.dart';
class SliderIndicatorWidget extends StatelessWidget { final int length;
final int currentIndex;
final Color color;
final bool isCircle;
final MainAxisAlignment alignment;
SliderIndicatorWidget({ Key? key, required this.length, required this.currentIndex, Color? color, this.isCircle = false, this.alignment = MainAxisAlignment.center, }) : color = color ?? AppColors.primary, super(key: key);
@override Widget build(BuildContext context) { return Row( mainAxisAlignment: alignment,
children: List.generate(length, (index) { return Container( margin: const EdgeInsets.symmetric(horizontal: 3), width: !isCircle ? currentIndex == index ? 15.0 : 8 : 6, height: !isCircle ? 4 : 6, decoration: BoxDecoration( borderRadius: const BorderRadius.all(Radius.circular(4)), color: currentIndex == index ? color : color.withOpacity(0.3), ), ); }), ); } }
|
第 2 步:控制器
lib/pages/system/welcome/controller.dart
1 2 3 4 5 6 7 8
| int currentIndex = 0;
void onPageChanged(int index) { currentIndex = index; update(['slider', 'bar']); }
|
第 3 步:视图
lib/pages/system/welcome/view.dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| Widget _buildBar() { return GetBuilder<WelcomeController>( id: "bar", init: controller, builder: (controller) { return <Widget>[ SliderIndicatorWidget( length: 3, currentIndex: controller.currentIndex, ), ].toRow( mainAxisAlignment: MainAxisAlignment.spaceAround, ); }, ); }
Widget _buildView() { return <Widget>[ buildSlider(), buildBar(), ] .toColumn( mainAxisAlignment: MainAxisAlignment.spaceAround, ) .paddingAll(AppSpace.page); }
|
最后:运行
提交代码到 git