颤动:步进器不滚动

问题描述 投票:0回答:2

我正试图制作一个按钮下面的步进器。问题是,如果我将它包装在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,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
flutter stepper
2个回答
1
投票

你只需要添加

physics: ClampingScrollPhysics(),

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

我希望它能解决你的问题


0
投票

如果按钮应该在屏幕底部持久,您可以使用Scaffold小部件的persistentFooterButtons属性,或者使用带有自定义小部件的bottomNavigationBar属性。

© www.soinside.com 2019 - 2024. All rights reserved.