我在TextFormField上添加了SliverPersistentHeaderDelegate。滚动到中间/底部,然后在textField上键入后,SliverList自动到达顶部。如何禁用此功能?

完整代码。

class MyWidget extends StatelessWidget {
  static const String route = '/myWidget';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: Container(
            child: CustomScrollView(
              slivers: <Widget>[
                SliverPersistentHeader(
                  pinned: true,
                  delegate: MyDynamicHeader(),
                ),
                SliverList(
                    delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
                      return Container(
                        height: 200,
                        color: Color(Random().nextInt(0xffffffff)),
                      );
                    },
                    )
                )
              ],
            )
        )
    );
  }
}

class MyDynamicHeader extends SliverPersistentHeaderDelegate {
  int index = 0;

  @override
  Widget build(BuildContext context, double shrinkOffset, bool overlapsContent) {
    return LayoutBuilder(
        builder: (context, constraints) {
          final Color color = Colors.primaries[index];
          final double percentage = (constraints.maxHeight - minExtent)/(maxExtent - minExtent);

          if (++index > Colors.primaries.length-1)
            index = 0;

          return Container(
            decoration: BoxDecoration(
                boxShadow: [BoxShadow(blurRadius: 4.0, color: Colors.black45)],
                gradient: LinearGradient(
                    colors: [Colors.blue, color]
                )
            ),
            height: constraints.maxHeight,
            child: SafeArea(
                child: Center(
                  child: Row(
                    children: <Widget>[
                      CircularProgressIndicator(
                        value: percentage,
                        valueColor: AlwaysStoppedAnimation<Color>(Colors.white),
                      ),
                      InkWell(
                        onTap: (){
                          print('is working');
                        },
                        child: Container(
                          height: 50,
                          width: 100,
                          color: Palette.orelPay,
                        ),
                      ),
                      Expanded(child: TextFormField(
                        //controller: searchController,
                        decoration: InputDecoration(
                            contentPadding:
                            EdgeInsets.fromLTRB(20.0, 15.0, 20.0, 15.0),
                            hintText: "Search on OrelBuy",
                            border: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0)),
                            fillColor: Palette.background,
                            filled: true,
                            enabledBorder: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0)),
                            focusedBorder: OutlineInputBorder(
                                borderSide:
                                BorderSide(color: Palette.background, width: 32.0),
                                borderRadius: BorderRadius.circular(50.0))),
                      ),)
                    ],
                  ),
                )
            ),
          );
        }
    );
  }

  @override
  bool shouldRebuild(SliverPersistentHeaderDelegate old) => false;

  @override
  double get maxExtent => 250.0;

  @override
  double get minExtent => 80.0;
}

最佳答案

您可以使用CustomScrollView小部件的物理特性来控制滚动。

CustomScrollView(
      physics: PageScrollPhysics(), // added
      slivers: <Widget>[

关于flutter - 在Flutter中键入TextField时滚动到顶部,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61723307/

10-12 01:51