flutter3.0学习笔记

SliverFixedExtentList 固定高度

Preview

SliverFixedExtentList用来生成一个带有固定的垂直滚动列表itemExtent,这意味着每个子节点都有一个固定的高度,这将有助于我们提高滚动性能,其构造:

 const SliverFixedExtentList({
    super.key,
    required super.delegate,
    required this.itemExtent,
  });

完整例子:

import 'package:flutter/material.dart';

class CustomScrollViewPage extends StatefulWidget {
  const CustomScrollViewPage({Key? key}) : super(key: key);

  @override
  State<CustomScrollViewPage> createState() => _CustomScrollViewPageState();
}

class _CustomScrollViewPageState extends State<CustomScrollViewPage> {
  @override
  Widget build(BuildContext context) {
    return  Scaffold(
      body: CustomScrollView(
        slivers: [
          SliverFixedExtentList(
              delegate: SliverChildBuilderDelegate(
                  (BuildContext context,int index){
                    return Container(
                      alignment: Alignment.center,
                      color: Colors.lightBlue[100*(index%9)],
                      child: Text('$index'),
                    );
                  }
              ),
              itemExtent: 50
          )
        ],
      ),
    );
  }
}