Flutter教学

Flutter(35):Material组件之Stepper

2020-10-05  本文已影响0人  starryxp

Flutter教学目录持续更新中

Github源代码持续更新中

1.Stepper介绍

步骤指示器,显示一系列步骤的过程

2.Stepper属性

3.Step属性

4.注意事项

5.Stepper

1601808454(1).png
class _StepperPageState extends State<StepperPage> {
  var _currentStep = 0;
  List<Step> _widgetList = [];

  _myStepper() {
    return Stepper(
      steps: _getStepList(),
      physics: AlwaysScrollableScrollPhysics(),
      type: StepperType.vertical,
      currentStep: _currentStep,
      onStepTapped: (index) {
        print('onStepTapped index = $index');
        setState(() {
          _currentStep = index;
        });
      },
      onStepContinue: () {
        print('onStepContinue');
        setState(() {
          if (_currentStep < _widgetList.length - 1) {
            _currentStep++;
          }
        });
      },
      onStepCancel: () {
        print('onStepCancel');
        setState(() {
          if (_currentStep > 0) {
            _currentStep--;
          }
        });
      },
    );
  }

  _myStep(int i) {
    return Step(
      title: Text('Step title$i'),
      subtitle: Text('Step subtitle$i'),
      content: Text('Step content$i'),
      state: _getStepState(i),
      isActive: _currentStep == i ? true : false,
    );
  }

  _getStepState(int i) {
    switch (i) {
      case 1:
        return StepState.editing;
      case 2:
        return StepState.disabled;
      case 3:
        return StepState.complete;
      case 4:
        return StepState.error;
      default:
        return StepState.indexed;
    }
  }

  _getStepList() {
    _widgetList.clear();
    for (var i = 0; i < 10; i++) {
      _widgetList.add(_myStep(i));
    }
    return _widgetList;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Stepper'),
      ),
      body: _myStepper(),
    );
  }
}

6.自定义控制器

如果不想要按钮可以直接返回空的Container


1601808604(1).png
      controlsBuilder: (BuildContext context,
          {VoidCallback onStepContinue, VoidCallback onStepCancel}) {
        return Row(
          children: [
            RaisedButton(
              child: Text('上一步'),
              onPressed: onStepCancel,
            ),
            RaisedButton(
              child: Text('下一步'),
              onPressed: onStepContinue,
            ),
          ],
        );
        return Container();
      },

7.横向

横向的时候如果内容过多会溢出,这个是跟纵向有区别的,这时候要么减少内容,要么使用滑动组件嵌套,但还还需要嵌套一个已知宽度的父空间


1601808720(1).png
      body: SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Container(
          color: Colors.grey,
          width: 2000,
          height: 200,
          child: _myStepper(),
        ),
      ),

下一节:Cupertino组件之CupertinoActivityIndicator

Flutter(36):Cupertino组件之CupertinoActivityIndicator

Flutter教学目录持续更新中

Github源代码持续更新中

上一篇 下一篇

猜你喜欢

热点阅读