【问题标题】:How to modify a Step StepState on continue如何在继续时修改 Step StepState
【发布时间】:2019-03-20 19:47:09
【问题描述】:

我正在关注本教程https://www.youtube.com/watch?v=izbkS2svuq4

还有一个简短的提及将 StepState 修改为 StepState.editing 以便您获得一个铅笔图标。

如何修改我正在执行的步骤的 StepState,以便在我踩/过去时将状态更改为正在编辑(或完成)

class _SimpleWidgetState extends State<SimpleWidget> {
  int _stepCounter = 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: true,
    ),
  ];

  @override
  Widget build(BuildContext context) {
    return Container(
      child: Stepper(
        steps: steps,
        currentStep: this._stepCounter,
        type: StepperType.vertical,
        onStepTapped: (step) {
          setState(() {
            _stepCounter = step;
            steps[step].state = StepState.editing; // this does not work but is what Im trying to accomplish
          });
        },
        onStepCancel: () {
          setState(() {
            _stepCounter > 0 ? _stepCounter -= 1 : _stepCounter = 0;
          });
        },
        onStepContinue: () {
          setState(() {
            _stepCounter < steps.length - 1 ? _stepCounter += 1 : _stepCounter = 0;
          });
        },
      ),
    );
  }
}

【问题讨论】:

    标签: flutter stepper


    【解决方案1】:

    移动步骤时具有 3 个状态的完整示例:

    class _State extends State<MyApp> {
    
      int _current;
    
      List<StepState> _listState;
    
      @override
      void initState() {
        _current = 0;
        _listState = [
          StepState.indexed,
          StepState.editing,
          StepState.complete,
        ];
        super.initState();
      }
    
      List<Step> _createSteps(BuildContext context) {
        List<Step> _steps = <Step>[
          new Step(
            state: _current == 0
                ? _listState[1]
                : _current > 0 ? _listState[2] : _listState[0],
            title: new Text('Step 1'),
            content: new Text('Do Something'),
            isActive: true,
          ),
          new Step(
            state: _current == 1
                ? _listState[1]
                : _current > 1 ? _listState[2] : _listState[0],
            title: new Text('Step 2'),
            content: new Text('Do Something'),
            isActive: true,
          ),
          new Step(
            state: _current == 2
                ? _listState[1]
                : _current > 2 ? _listState[2] : _listState[0],
            title: new Text('Step 3'),
            content: new Text('Do Something'),
            isActive: true,
          ),
        ];
        return _steps;
      }
    
      @override
      Widget build(BuildContext context) {
        List<Step> _stepList = _createSteps(context);
        return new Scaffold(
          appBar: new AppBar(
            title: new Text('Stepper Example'),
          ),
          body: new Container(
            padding: new EdgeInsets.all(20.0),
            child: new Center(
              child: new Column(
                children: <Widget>[
                  Expanded(
                    child: Stepper(
                      type: StepperType.vertical,
                      steps: _stepList,
                      currentStep: _current,
                      onStepContinue: () {
                        setState(() {
                          if (_current < _stepList.length - 1) {
                            _current++;
                          } else {
                            _current = _stepList.length - 1;
                          }
                          //_setStep(context);
                        });
                      },
                      onStepCancel: () {
                        setState(() {
                          if (_current > 0) {
                            _current--;
                          } else {
                            _current = 0;
                          }
                          //_setStep(context);
                        });
                      },
                      onStepTapped: (int i) {
                        setState(() {
                          _current = i;
                        });
                      },
                    ),
                  ),
                ],
              ),
            ),
          ),
        );
      }
    }
    

    【讨论】:

      【解决方案2】:

      将步骤列表声明移动到build method 并声明每个步骤的state 字段,例如第一步:_stepCounter == 0 ? StepState.editing : StepState.indexed 并删除此行steps[step].state = StepState.editing;,因为.state 是最终的,因此不能改变。

      【讨论】:

        猜你喜欢
        • 2016-11-03
        • 1970-01-01
        • 1970-01-01
        • 2022-06-21
        • 2015-02-23
        • 2019-12-06
        • 2018-03-15
        • 2016-11-02
        • 1970-01-01
        相关资源
        最近更新 更多