如何检测ListView项的位置? [英] How to detect the position of a ListView Item?

查看:91
本文介绍了如何检测ListView项的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据其在屏幕上的位置将填充添加到listView项.例如,如果listView项位于屏幕中间,我想将其填充增加10点,如果listView项位于屏幕顶部附近,我希望将其填充增加15点.

How do I add padding to a listView item based on its position on the screen. For example, If a listView item is in the middle of the screen, I would like to increase its padding by 10 points, If the listView item is near the top of the screen I would like to increase its padding by 15 points.

推荐答案

您可以通过在ListView上附加ScrollController并创造性地使用它来实现此目的:

You can achieve this by attaching a ScrollController to your ListView and use it creatively :

首先,您需要定义一个ScrollController,它将用于获取ScrollController.offset以确定列表当前位置.然后,我添加了一堆变量来调整此动态列表,同时保留其预期的功能:

First you need to define a ScrollController which will be used to get the ScrollController.offset to determine the list current position. Then I added a bunch of variable to adjust this dynamic list while keeping its intended functionality:

  class DynamicPadding extends StatefulWidget {
    DynamicPadding({Key key,}) : super(key: key);

    @override
    _DynamicPaddingState createState() => _DynamicPaddingState();
  }


  class  _DynamicPaddingState extends State<DynamicPadding> {

   ScrollController _controller;

   var _middlePadding = 10.0 ; // padding of centered items

   var _edgesPadding = 15.0 ; // padding of non-centered items

   var _itemSize = 100.0 ; 

   int _centeredItems = 3 ;

   int _numberOfEdgesItems ; // number of items which aren't centered at any moment

   int _aboveItems ; // number of items above the centered ones

   int _belowItems ; // number of items below the centered ones


   @override
   void initState() {
     _controller = ScrollController(); // Initializing ScrollController
     _controller.addListener(_scrollListener); add a listener to ScrollController to update padding
     super.initState();
   }

   _scrollListener() {
     setState(() {});
   }

   @override
   Widget build(BuildContext context) {
     return new Scaffold(
         backgroundColor: Colors.grey.shade200,
         appBar: new AppBar(title: new Text('Dynamic Padding Example'),),
     body: ListView.builder(
     controller: _controller ,
     itemCount: 20,
     itemBuilder: (context, index) {

       // This is how to calculate number of non-centered Items
       _numberOfEdgesItems = ( (MediaQuery.of(context).size.height - _centeredItems*(_itemSize + 2*(_middlePadding))) ~/ (_itemSize + 2*(_edgesPadding)) ) ; 

       _aboveItems = ( ( (_controller.offset) / (_itemSize + 2*(_edgesPadding)) ) + _numberOfEdgesItems/2 ).toInt() ;

       _belowItems = _aboveItems + _centeredItems ;

     return Container(
     padding:  index >= _aboveItems && index < _belowItems ? EdgeInsets.all(_middlePadding) : EdgeInsets.all(_edgesPadding) ,
     child: Card(
     child: Container(
       height: _itemSize,
         child: new Row(
             mainAxisAlignment: MainAxisAlignment.center,
             children: <Widget>[
               Text(index.toString(), style: TextStyle(fontSize: 36.0, fontWeight: FontWeight.bold)),
               ]
             ),
           ),
         ),
       );
       }),
     );
   }
 }

这篇关于如何检测ListView项的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆