代码之家  ›  专栏  ›  技术社区  ›  Omar Awamry

颤振式长条堆垛

  •  2
  • Omar Awamry  · 技术社区  · 6 年前

    我正在尝试使用创建滚动视图 CustomScrollView this one

    我需要 SliverList SliverAppbar ,列表不会占据整个屏幕并隐藏滑动条。 我之所以要这样做,是因为我需要在列表顶部附加一个持久定位的小部件,除非列表堆叠在滑动应用条上方,否则它不会出现。

    这是我的 code

    0 回复  |  直到 6 年前
        1
  •  9
  •   Kherel    6 年前

    enter image description here

    第一步: 在SliveAppbar小部件中使用ListView。使css溢出:隐藏效果。

    将控制器添加到NestedScrollView,并在堆栈中滚动时移动按钮。加上计算你想要停止按钮移动的位置。

    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      ScrollController scrollController;
      final double expandedHight = 150.0;
    
      @override
      void initState() {
        super.initState();
        scrollController = new ScrollController();
        scrollController.addListener(() => setState(() {}));
      }
    
      @override
      void dispose() {
        scrollController.dispose();
        super.dispose();
      }
    
      double get top {
        double res = expandedHight;
        if (scrollController.hasClients) {
          double offset = scrollController.offset;
          if (offset < (res - kToolbarHeight)) {
            res -= offset;
          } else {
            res = kToolbarHeight;
          }
        }
        return res;
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          backgroundColor: Colors.black,
          body: Stack(
            children: [
              NestedScrollView(
                controller: scrollController,
                headerSliverBuilder: (context, value) {
                  return [
                    SliverAppBar(
                      pinned: true,
                      expandedHeight: expandedHight,
                      flexibleSpace: ListView(
                        physics: const NeverScrollableScrollPhysics(),
                        children: [
                          AppBar(
                            title: Text('AfroJack'),
                            elevation: 0.0,
                          ),
                          Container(
                            color: Colors.blue,
                            height: 100,
                            alignment: Alignment.center,
                            child: RaisedButton(
                              child: Text('folow'),
                              onPressed: () => print('folow pressed'),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ];
                },
                body: ListView.builder(
                  physics: const NeverScrollableScrollPhysics(),
                  itemCount: 80,
                  itemBuilder: (BuildContext context, int index) {
                    return Text(
                      'text_string'.toUpperCase(),
                      style: TextStyle(
                        color: Colors.white,
                      ),
                    );
                  },
                ),
              ),
              Positioned(
                top: top,
                width: MediaQuery.of(context).size.width,
                child: Align(
                  child: RaisedButton(
                    onPressed: () => print('shuffle pressed'),
                    child: Text('Suffle'),
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
    
    推荐文章