Flutter:Stepper onStepContinue不触发

时间:2018-10-16 05:23:41

标签: dart flutter stepper

您能告诉我我的代码有什么问题吗?

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步进机的所有示例,但仍然没有运气。我可以点击继续按钮,但是它没有移到另一步。我忘记了什么吗?我创建了一个有状态的Widget类,然后有一个按钮将带我调用此_createProfileStepper()。谢谢。

2 个答案:

答案 0 :(得分:1)

,因此您无法从列表内访问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
),

答案 1 :(得分:1)

通过将整个代码包含在_createProfileStepper()中,即使在有状态小部件中使用小部件,小部件也将变为无状态。这是因为每当有状态窗口小部件的build方法重新运行时,它将调用_createProfileStepper(),这将导致整个步进器窗口小部件重新初始化,即重新创建步进器,因此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个成员可以用于初始化