颤振:步进器不滚动

时间:2019-02-18 19:05:32

标签: flutter stepper

我正在尝试制作一个按钮式步进器。问题是,如果我将其包装在Column或ListView中,则无法在Stepper中滚动。我试图通过NestedScrollView将它们包装起来,滚动正常,但是问题是在Stepper上方发布了按钮。代码中有_MyHomePageState的两个示例,第一个使用ListView,第二个使用NestedView,两者都不适合我。如何实现带有按钮的Stepper?

这就是我想要的 enter image description here

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

// 1 case with ListView (doesn't scroll)
class _MyHomePageState extends State<MyHomePage> {
  int _currentStep = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.red),
      ),
      body: Form(
        child: ListView(
          children: <Widget>[
            Stepper(
              type: StepperType.vertical,
              currentStep: _currentStep,
              onStepTapped: (int index) {
                setState(() {
                  _currentStep = index;
                });
              },
              steps: [
                Step(
                  title: Text('Step 1'),
                  content: Column(
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(
                            labelText: 'City', border: UnderlineInputBorder()),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Name'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Address'),
                      ),
                    ],
                  ),
                  isActive: true,
                ),
                Step(
                  title: Text('Step 2'),
                  content: Column(
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(labelText: 'City'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Name'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Address'),
                      ),
                    ],
                  ),
                  isActive: true,
                ),
                Step(
                  title: Text('Step 3'),
                  content: Column(
                    children: <Widget>[
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Width'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Length'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Height'),
                      ),
                      TextFormField(
                        decoration: InputDecoration(labelText: 'Weigth'),
                      ),
                    ],
                  ),
                  isActive: true,
                ),
              ],
            ),
            RaisedButton(
              child: Text('Button'),
              onPressed: () {},
            )
          ],
        ),
      ),
    );
  }
} 

// 2 case with NestedScrollView
class _MyHomePageState extends State<MyHomePage> {
  int _currentStep = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        iconTheme: IconThemeData(color: Colors.red),
      ),
      body: Form(
        child: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            return <Widget>[
              SliverList(
                delegate: SliverChildListDelegate(<Widget>[
                  RaisedButton(
                    child: Text('Button'),
                    onPressed: () {},
                  )
                ]),
              ),
            ];
          },
          body: Stepper(
            type: StepperType.vertical,
            currentStep: _currentStep,
            onStepTapped: (int index) {
              setState(() {
                _currentStep = index;
              });
            },
            steps: [
              Step(
                title: Text('Step 1'),
                content: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(
                          labelText: 'City', border: UnderlineInputBorder()),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Name'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Address'),
                    ),
                  ],
                ),
                isActive: true,
              ),
              Step(
                title: Text('Step 2'),
                content: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(labelText: 'City'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Name'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Address'),
                    ),
                  ],
                ),
                isActive: true,
              ),
              Step(
                title: Text('Step 3'),
                content: Column(
                  children: <Widget>[
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Width'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Length'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Height'),
                    ),
                    TextFormField(
                      decoration: InputDecoration(labelText: 'Weigth'),
                    ),
                  ],
                ),
                isActive: true,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

如果该按钮应持久显示在屏幕底部,则可以使用“脚手架”小部件的persistentFooterButtons属性或带有自定义小部件的bottomNavigationBar属性。

答案 1 :(得分:0)

您只需要添加

physics: ClampingScrollPhysics(),

由于Listview是可滚动的窗口小部件,因此Stepper内部具有属性。

希望它能解决您的问题