如何获得条子的 Y 偏移量

Sep*_*ika 5 flutter

我需要获取 Flutter 中 Sliver 的 Y 偏移量。之所以要获取Y偏移量,与这个问题有关。

然后我试图从这个答案中得到 Y 偏移量。但我得到了错误type '_RenderSliverPinnedPersistentHeaderForWidgets' is not a subtype of type 'RenderBox' in type cast

我得到一条线索,sliver 不能使用 renderbox,因为它们使用 renderSliv​​er

示例代码:

import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: MyStatelessWidget(),
    );
  }
}

class MyStatelessWidget extends StatelessWidget {
  MyStatelessWidget({Key? key}) : super(key: key);
  final GlobalKey keySliverAppBar = GlobalKey();

  @override
  Widget build(BuildContext context) {
    final List<String> tabs = <String>['Tab 1', 'Tab 2'];
    return DefaultTabController(
      length: tabs.length, // This is the number of tabs.
      child: Scaffold(
        body: NestedScrollView(
          headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
            // These are the slivers that show up in the "outer" scroll view.
            return <Widget>[
              SliverToBoxAdapter(child: Container(height: 80, color: Colors.green)),
              // How to get Y offset of this SliverToBoxAdapter

              SliverToBoxAdapter(child: Container(height: 50, color: Colors.yellow)),

              SliverOverlapAbsorber(
                handle:
                    NestedScrollView.sliverOverlapAbsorberHandleFor(context),
                sliver: SliverAppBar(
                  title:
                      const Text('Books'), // This is the title in the app bar.
                  pinned: true,
                  expandedHeight: 150.0,
                  forceElevated: innerBoxIsScrolled,
                  bottom: TabBar(
                    onTap: (int index){
                      RenderBox box = keySliverAppBar.currentContext?.findRenderObject() as RenderBox;
                      Offset position = box.localToGlobal(Offset.zero); //this is global position
                      double y = position.dy; 
                      print('this Y Offset: $y'); // How to get Y offset of this SliverAppBar
                    },
                    // These are the widgets to put in each tab in the tab bar.
                    tabs: tabs.map((String name) => Tab(text: name)).toList(),
                  ),
                ),
              ),
            ];
          },
          body: TabBarView(
            // These are the contents of the tab views, below the tabs.
            children: tabs.map((String name) {
              return SafeArea(
                top: false,
                bottom: false,
                child: Builder(
                  builder: (BuildContext context) {
                    return CustomScrollView(
                      key: PageStorageKey<String>(name),
                      slivers: <Widget>[
                        SliverOverlapInjector(
                          handle:
                              NestedScrollView.sliverOverlapAbsorberHandleFor(
                                  context),
                        ),
                        SliverPadding(
                          padding: const EdgeInsets.all(8.0),
                          sliver: SliverFixedExtentList(
                            itemExtent: 48.0,
                            delegate: SliverChildBuilderDelegate(
                              (BuildContext context, int index) {
                                return ListTile(
                                  title: Text('Item $index'),
                                );
                              },
                              childCount: 30,
                            ),
                          ),
                        ),
                      ],
                    );
                  },
                ),
              );
            }).toList(),
          ),
        ),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

这些问题有什么解决办法吗?

注意:我需要里面所有的 Y 偏移条子headerSliverBuilder