颤振如何删除滚动条列表视图顶部的填充

时间:2020-09-14 06:53:10

标签: flutter dart flutter-layout scrollbar

首先,对不起我的英语不好。所以我的问题是,为什么滚动条顶部有一些填充,如何将其删除?我认为代码中的覆盖小部件存在问题,但我找不到问题以及如何解决。请帮助,谢谢。 PS:我不想使用默认的下拉窗口小部件,因为我不太喜欢这种设计,因此我在其他代码的帮助下制作了一个自定义的下拉按钮。

这是我的代码:

import 'package:flutter/material.dart';

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool dropdownOpen = false;
  OverlayEntry dropdown;
  dynamic actionKey = LabeledGlobalKey("actionKey");
  ScrollController sc = ScrollController();
  
  OverlayEntry createDropdown() {
    RenderBox renderBox = actionKey.currentContext.findRenderObject();
    Offset offset = renderBox.localToGlobal(Offset.zero);
    return OverlayEntry(
      builder: (context) {
        return Positioned(
          left: offset.dx,
          width: renderBox.size.width,
          top: offset.dy + renderBox.size.height,
          height: 230,
          child: Material(
            elevation: 20,
            child: Scrollbar(
              isAlwaysShown: true,
              controller: sc,
              child: ListView.builder(
                controller: sc,
                padding: EdgeInsets.all(0),
                itemCount: 5,
                itemBuilder: (BuildContext context, int index){
                  return Container(
                    height: 50,
                    alignment: Alignment.center,
                    child: Text((index+1).toString())
                  );
                }
              )
            )
          )
        );
      }
    );
  }
  
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(primaryColor: Colors.red),
      home: Scaffold(
        body: Center(
          child: GestureDetector(
            key: actionKey,
            child: Container(
              height: 50,
              width: 100,
              color: Colors.yellow,
              alignment: Alignment.center,
              child: Text("Click")
            ),
            onTap: (){
              setState((){
                if(dropdownOpen){
                  dropdown.remove();
                  dropdownOpen = false;
                } else {
                  dropdown = createDropdown();
                  Overlay.of(context).insert(dropdown);
                  dropdownOpen = true;
                }
              });
            }
          )
        )
      ),
    );
  }
}

看起来是这样的: image

0 个答案:

没有答案