【问题标题】:how to get a full size height of customscrollview widget in flutter?如何在颤动中获得customscrollview小部件的全尺寸高度?
【发布时间】:2021-04-22 09:23:41
【问题描述】:

我想获取 CustomScrollView 小部件的整个高度。所以我做了一个下面的代码,但它不工作。

@override
void initState(){
 super.initState();
 WidgetsBinding.instance.addPostFrameCallback((_) => getSizeAndPosition());
}

getSizeAndPosition() {
    RenderBox _customScrollBox =
        _customScrollKey.currentContext.findRenderObject();
    _customScrollSize = _customScrollBox.size;
    _customScrollPosition = _customScrollBox.localToGlobal(Offset.zero);
    print(_customScrollSize.height);
    setState(() {});
  }

@override
  Widget build(BuildContext context) {
    return Scaffold(
      key: _customScrollKey,
      appBar: _appbar(),
      body: CustomScrollView(
        controller: _controller,
        slivers: [
          SliverList(
              delegate: SliverChildListDelegate([
            _titleSection(),
            _thumnail(),
            SizedBox(
              height: 40,
            ),
          ])),
        ],
      ),
    );
  }

这段代码得到的高度没有考虑到customScrollview中列表的高度。我的意思是,_customScrollSize.heightMediaQuery.of(context).size.width 是一样的。

I want this function

_controller.addListener(() {
      setState(() {
        if (_controller.offset < 0) {
          scrollHeight = 0;
        } else {
          scrollHeight = _controller.offset;
        }
      });
    });


 Container(
   width: size.width * (scrollHeight / _customScrollSize.height),
   decoration: BoxDecoration(
   border: Border(
           bottom: BorderSide(
           width: 1)),

使用上面的代码,'_customScrollSize.height'并不能反映整体高度,因此该功能没有正确实现。在这种情况下有什么好的方法可以使用它吗?

【问题讨论】:

    标签: flutter height customscrollview


    【解决方案1】:

    CustomScrollView

    使用 slivers 创建自定义滚动效果的 ScrollView。

    CustomScrollView 可让您直接提供条子以创建各种滚动效果,例如列表、网格和扩展标题。例如,要创建包含扩展应用栏、后跟列表和网格的滚动视图,请使用包含三个条子的列表:SliverAppBarSliverListSliverGrid

    在您的情况下,请删除 appBar 并使用带有自定义滚动视图的 sliverAppBar ,您可以为其他孩子使用 sliverfillRemaining 小部件

    例子

    CustomScrollView(
      slivers: <Widget>[
        const SliverAppBar(
          pinned: true,
          expandedHeight: 250.0,
          flexibleSpace: FlexibleSpaceBar(
            title: Text('Demo'),
          ),
        ),
    SliverList(
                  delegate: SliverChildListDelegate([
                _titleSection(),
                _thumnail(),
                SizedBox(
                  height: 40,
                ),
              ])),
    ...
    

    【讨论】:

      猜你喜欢
      • 2019-06-01
      • 2020-11-25
      • 2020-03-24
      • 2020-11-02
      • 2017-10-23
      • 1970-01-01
      • 2021-01-14
      • 2011-04-14
      • 1970-01-01
      相关资源
      最近更新 更多