Flutter:步进器 onStepContinue 未触发
Flutter: Stepper onStepContinue not firing
你能告诉我我的代码有什么问题吗?
Widget _createProfileStepper() {
int currentStep = 0;
List<Step> createAccSteps = [
new Step(
title: Container(),
content: new Text('This is the first step.'),
isActive: currentStep >= 0,
state: currentStep >= 0 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the second step.'),
isActive: currentStep >= 0,
state: currentStep >= 1 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the third step.'),
isActive: currentStep >= 0,
state: currentStep >= 2 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the second step.'),
isActive: currentStep >= 0,
state: currentStep >= 3 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the third step.'),
isActive: currentStep >= 0,
state: currentStep >= 4 ? StepState.editing : StepState.disabled,
),
];
return Scaffold(
appBar: AppBar(
title: Text("Create Profile"),
),
body: Stepper(
type: StepperType.horizontal,
currentStep: currentStep,
onStepTapped: (step) {
setState(() {
currentStep = step;
});
},
onStepContinue: () {
setState(() {
if (currentStep < createAccSteps.length - 1) {
currentStep = currentStep + 1;
} else {}
});
},
onStepCancel: () {
setState(() {
if(currentStep > 0){
currentStep = currentStep - 1;
}
else {
currentStep = 0;
}
});
},
steps: createAccSteps,
),
);
}
我遵循了 Flutter 步进器的所有示例,但仍然没有成功。我可以点击继续按钮,但它不会移动到另一步。我忘了什么吗?我创建了一个 Stateful Widget class 然后一个按钮会带我调用这个 _createProfileStepper()。谢谢
因此您无法从开始列表中访问您的 currentStep。
"isActive" 也应该是一个布尔值(并且只影响样式 https://docs.flutter.io/flutter/material/Step/isActive.html )
还有一个空的 Container() 作为标题似乎有点奇怪,您可以将其删除或将步骤编号放在那里
尝试将步数更改为
Step(
title: Text("Step One"),
content: new Text("This is the first step."),
isActive: true
),
通过在 _createProfileStepper()
中包含整个代码,即使在有状态的小部件中使用小部件,它也会变得无状态。这是因为每当状态小部件的 build
方法重新 运行 时,它会调用 _createProfileStepper()
这将导致整个步进器小部件重新初始化,即重新创建步进器,因此继续不起作用。
为什么不单独为该步骤创建一个有状态小部件并使用该小部件而不是您从 _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
成员可用于初始化
你能告诉我我的代码有什么问题吗?
Widget _createProfileStepper() {
int currentStep = 0;
List<Step> createAccSteps = [
new Step(
title: Container(),
content: new Text('This is the first step.'),
isActive: currentStep >= 0,
state: currentStep >= 0 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the second step.'),
isActive: currentStep >= 0,
state: currentStep >= 1 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the third step.'),
isActive: currentStep >= 0,
state: currentStep >= 2 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the second step.'),
isActive: currentStep >= 0,
state: currentStep >= 3 ? StepState.editing : StepState.disabled,
),
new Step(
title: Container(),
content: new Text('This is the third step.'),
isActive: currentStep >= 0,
state: currentStep >= 4 ? StepState.editing : StepState.disabled,
),
];
return Scaffold(
appBar: AppBar(
title: Text("Create Profile"),
),
body: Stepper(
type: StepperType.horizontal,
currentStep: currentStep,
onStepTapped: (step) {
setState(() {
currentStep = step;
});
},
onStepContinue: () {
setState(() {
if (currentStep < createAccSteps.length - 1) {
currentStep = currentStep + 1;
} else {}
});
},
onStepCancel: () {
setState(() {
if(currentStep > 0){
currentStep = currentStep - 1;
}
else {
currentStep = 0;
}
});
},
steps: createAccSteps,
),
);
}
我遵循了 Flutter 步进器的所有示例,但仍然没有成功。我可以点击继续按钮,但它不会移动到另一步。我忘了什么吗?我创建了一个 Stateful Widget class 然后一个按钮会带我调用这个 _createProfileStepper()。谢谢
因此您无法从开始列表中访问您的 currentStep。
"isActive" 也应该是一个布尔值(并且只影响样式 https://docs.flutter.io/flutter/material/Step/isActive.html )
还有一个空的 Container() 作为标题似乎有点奇怪,您可以将其删除或将步骤编号放在那里
尝试将步数更改为
Step(
title: Text("Step One"),
content: new Text("This is the first step."),
isActive: true
),
通过在 _createProfileStepper()
中包含整个代码,即使在有状态的小部件中使用小部件,它也会变得无状态。这是因为每当状态小部件的 build
方法重新 运行 时,它会调用 _createProfileStepper()
这将导致整个步进器小部件重新初始化,即重新创建步进器,因此继续不起作用。
为什么不单独为该步骤创建一个有状态小部件并使用该小部件而不是您从 _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
成员可用于初始化