Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate brn_horizontal_steps、brn_step_line to null safety #89

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 144 additions & 30 deletions example/lib/sample/components/step/brn_horizontal_step_example.dart
Original file line number Diff line number Diff line change
@@ -1,62 +1,176 @@
// @dart=2.9

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

class BrnHorizontalStepExamplePage extends StatefulWidget {
final String title;

BrnHorizontalStepExamplePage({this.title});
const BrnHorizontalStepExamplePage({Key? key, required this.title})
: super(key: key);

@override
State<StatefulWidget> createState() {
return BrnHorizontalStepExamplePageState();
}
}

class BrnHorizontalStepExamplePageState extends State<BrnHorizontalStepExamplePage> {
int _index;
BrnHorizontalStepsManager _stepsManager = BrnHorizontalStepsManager();
class BrnHorizontalStepExamplePageState
extends State<BrnHorizontalStepExamplePage> {
late int _index;
double sliderValue = 2;
late BrnStepsController _controller;
late ValueNotifier<double> valueNotifier;

@override
void initState() {
_index = 1;
super.initState();
_index = 0;
_controller = BrnStepsController(currentIndex: _index);
valueNotifier = ValueNotifier(sliderValue);
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: BrnAppBar(
title: widget.title,
actions: controlSteps(context),
),
body: _stepsManager.buildSteps(currentIndex: _index, isCompleted: false, steps: <BrunoStep>[
BrunoStep(
stepContentText: "文案步骤",
appBar: BrnAppBar(title: widget.title),
body: Column(
children: [
SliverBrnHorizontalStep(
controller: _controller,
valueNotifier: valueNotifier,
),
BrunoStep(
stepContentText: "文案步骤",
const Text('步骤个数:'),
SliderWidget(
initValue: sliderValue,
valueNotifier: valueNotifier,
),
BrunoStep(
stepContentText: "文案步骤",
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text('上一步'),
onPressed: () {
_controller.backStep();
},
),
ElevatedButton(
child: const Text('下一步'),
onPressed: () {
_controller.forwardStep();
},
),
],
),
BrunoStep(
stepContentText: "文案步骤",
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
ElevatedButton(
child: const Text('跳至第3步'),
onPressed: () {
_controller.setCurrentIndex(2);
},
),
ElevatedButton(
child: const Text('完成'),
onPressed: () {
_controller.setCompleted();
},
),
],
),
]));
],
),
);
}
}

/// 自定义 widget
class SliderWidget extends StatefulWidget {
const SliderWidget({
Key? key,
required this.initValue,
required this.valueNotifier,
}) : super(key: key);
final double initValue;
final ValueNotifier<double> valueNotifier;

List<Widget> controlSteps(BuildContext context) {
List<Widget> result = List<Widget>();
@override
_SliderWidgetState createState() => _SliderWidgetState();
}

result.add(BrnTextAction("上一步", iconPressed: () {
_stepsManager.backStep();
}));
class _SliderWidgetState extends State<SliderWidget> {
late double sliderValue;

result.add(BrnTextAction("下一步", iconPressed: () {
_stepsManager.forwardStep();
}));
return result;
@override
void initState() {
super.initState();
sliderValue = widget.initValue;
}

@override
Widget build(BuildContext context) {
return Slider(
value: sliderValue,
min: 2,
max: 5,
divisions: 3,
onChanged: (value) {
setState(() {
sliderValue = value;
widget.valueNotifier.value = value;
});
},
);
}
}

/// 自定义 widget
class SliverBrnHorizontalStep extends StatefulWidget {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mark 一下,后面修改实现

const SliverBrnHorizontalStep({
Key? key,
required this.controller,
required this.valueNotifier,
}) : super(key: key);
final BrnStepsController controller;
final ValueNotifier<double> valueNotifier;

@override
_SliverBrnHorizontalStepsState createState() => _SliverBrnHorizontalStepsState();
}

class _SliverBrnHorizontalStepsState extends State<SliverBrnHorizontalStep> {
List<BrunoStep> brunoSteps() {
final List<BrunoStep> _list = [];
final int value = widget.valueNotifier.value.toInt();
for (int i = 0; i < value; i++) {
_list.add(BrunoStep(stepContentText: ('第${i + 1}步')));
}
return _list;
}

void _onChange() {
setState(() {
brunoSteps();
widget.controller.setCurrentIndex(0);
});
}

@override
void initState() {
super.initState();
widget.valueNotifier.addListener(_onChange);
}

@override
void dispose() {
widget.valueNotifier.removeListener(_onChange);
super.dispose();
}

@override
Widget build(BuildContext context) {
return BrnHorizontalSteps(
steps: brunoSteps(),
controller: widget.controller,
);
}
}
Loading