При наличии всего кода в _createProfileStepper()
виджет становится без сохранения состояния, даже если он используется в виджете с состоянием.Это происходит потому, что всякий раз, когда метод build
для виджета с состоянием перезапускается, он будет вызывать _createProfileStepper()
, что приведет к повторной инициализации всего виджета Stepper, т. Е. Воссоздание Stepper, и, таким образом, continue не работает.
Почему бы не создать виджет с сохранением состояния для одного шага и использовать этот виджет вместо того, который вы получаете от _createProfileStepper()
.Например:
class _SimpleWidgetState extends State<SimpleWidget> {
int currentStep = 0;
List<Step> steps = [
Step(
title: Text("Step One"),
content: Text("This is the first step"),
isActive: true
),
Step(
title: Text("Step Two"),
content: Text("This is the second step"),
isActive: true,
),
Step(
title: Text("Step Three"),
content: Text("This is the third step"),
isActive: true,
),
Step(
title: Text("Step Four"),
content: Text("This is the fourth step"),
isActive: false,
),
];
@override
Widget build(BuildContext context) {
return Container(
child: Stepper(
steps: steps,
currentStep: currentStep,
type: StepperType.vertical,
onStepTapped: (step) {
setState(() {
currentStep = step;
print(step);
});
},
onStepCancel: () {
setState(() {
currentStep > 0 ? currentStep -= 1 : currentStep = 0;
});
},
onStepContinue: () {
setState(() {
currentStep < steps.length - 1 ? currentStep += 1 : currentStep = 0;
});
},
),
);
}
}
class SimpleWidget extends StatefulWidget {
@override
_SimpleWidgetState createState() {
// TODO: implement createState
return _SimpleWidgetState();
}
}
Затем используйте SimpleWidget()
, где вы бы _createProfileStepper()
Во-вторых Что касается вашего вопроса о доступе к списку currentStep, потому что только static
члены могут быть использованы для инициализации