Flutter-如何在Stepper控件构建器中填充剩余空间?

时间:2018-12-27 20:17:00

标签: flutter flutter-layout stepper

在屏幕截图中,我想将“下一步”和“后退”按钮移到屏幕底部。

enter image description here

步进器有一个参数controlsBuilder,可让您建立控件的布局。如果只是简单的一行,它就放在内容的正下方。

显然,步进器是一个灵活的包装器。我不确定那是什么意思。我认为这意味着Stepper被视为Flex对象,因为它包含一个可滚动区域(用于内容)。阅读docs后,如果我理解正确,它表示我不能在主轴中使用最大尺寸的ExpandedColumn,因为步进器本质上是可滚动的区域,表示其中的任何RenderBox都具有无限的约束。

那么,控件构建器可以通过什么方式下推到底?

Widget _createEventControlBuilder(BuildContext context, {VoidCallback onStepContinue, VoidCallback onStepCancel}) {
return Row(
    mainAxisAlignment: MainAxisAlignment.spaceBetween,
    children: <Widget>[
      FlatButton(
        onPressed: onStepCancel,
        child: const Text('BACK'),
      ),
      FlatButton(
        onPressed: onStepContinue,
        child: const Text('NEXT'),
      ),
    ]
);
  }

我确实尝试将以上行包装在LayoutBuilder中,以及使用SizedBox进行另一次尝试,将高度设置为MediaQuery.of(context).size.height;。它确实将其推向底部(不尽如人意),但问题在于,控件下方现在有空间,导致屏幕向下滚动到空白区域。

完整代码:

    @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
    title: Text("Create an Event"),
  ),
  body: Form(
    key: _eventFormKey,
    child: Stepper(
        type: StepperType.horizontal,
        currentStep: _currentStep,
        controlsBuilder: _createEventControlBuilder,
        onStepContinue: () {
          if (_currentStep + 1 >= MAX_STEPS)
            return;
          setState(() {
            _currentStep += 1;
          });
          },
        onStepCancel: () {
          if (_currentStep + 1 >= MAX_STEPS)
            return;
          setState(() {
            _currentStep -= 1;
          });
        },
        steps: <Step>[
          Step(
            title: Text("Name"),
            isActive: 0 == _currentStep,
            state: _getStepState(0),
            content: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: 10.0),
                  child: Text(
                    "Give your event a cool name",
                    style: Theme.of(context).textTheme.title,
                  ),
                ),
                TextFormField(
                  maxLines: 1,
                  maxLength: 50,
                  maxLengthEnforced: true,
                  decoration: InputDecoration(
                    hintText: "e.g. Let's eat cheeseburgers!",
                  ),
                  validator: (value) {
                    if (value.trim().isEmpty)
                      return "Event name required.";
                  },
                )
              ],
            )
          ),

          Step(
            title: Text("Type"),
            isActive: 1 == _currentStep,
            state: _getStepState(1),
            content: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(bottom: 10.0),
                  child: Text(
                    "Select an event type",
                    style: Theme.of(context).textTheme.title,
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(bottom: 10.0),
                  child: Row(
                    children: <Widget>[
                      Expanded(
                        child: DropdownButton<int>(
                            items: _stepTwoDropdownItems,
                            hint: Text("Select event type"),
                            isExpanded: true,
                            value: _eventTypeSelectedIndex,
                            onChanged: (selection) {
                              setState(() {
                                _eventTypeSelectedIndex = selection;
                              });
                            }),
                      )
                    ],
                  )
                )
              ],
            )
          ),
      ]
    ),
  ),
);
}

2 个答案:

答案 0 :(得分:3)

我认为您可以创建自己的Stepper,也可以尝试使用此“ hack”:

创建两个变量来存储回调:

      VoidCallback _onStepContinue;
      VoidCallback _onStepCancel;

Form放在Stack中:

        Stack(
                children: <Widget>[
                  Form(
                    child: Stepper(

更改您的createEventControlBuilder方法:

          Widget _createEventControlBuilder(BuildContext context,
              {VoidCallback onStepContinue, VoidCallback onStepCancel}) {
            _onStepContinue = onStepContinue;
            _onStepCancel = onStepCancel;
            return SizedBox.shrink();
          }

添加自定义按钮:

      Widget _bottomBar() {
        return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              FlatButton(
                onPressed: () => _onStepCancel(),
                child: const Text('BACK'),
              ),
              FlatButton(
                onPressed: () => _onStepContinue(),
                child: const Text('NEXT'),
              ),
            ]);
      } 

这就是您的Stack的样子:

    Stack(
                children: <Widget>[
                  Form(
                    child: Stepper(
                    ....
                   ), //Form
                    Align(
                    alignment: Alignment.bottomCenter,
                    child: _bottomBar(),
                    )

我知道这有点脏,但是您可以尝试,否则我建议您创建自己的Widget。

答案 1 :(得分:0)

您还可以将 Steps 内容中的小部件的高度更改为相同的值,并根据需要将其设为 SingleChildScrollView。例如

Step(content: Container(
      height: MediaQuery.of(context).size.height - 250, //IMPORTANT
      child: SingleChildScrollView(
        child: Column(children: <Widget>[
        ...
),