首页 > 解决方案 > 颤振:步进器不滚动

问题描述

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

这就是我想要的 在这里输入图像描述

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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}

标签: flutterstepper

解决方案


您只需要添加

physics: ClampingScrollPhysics(),

Stepper 中的属性,因为 Listview 是一个可滚动的小部件。

我希望它能解决你的问题


推荐阅读