搜尋套件、儲存庫與 APP 分類

inview_notifier_list 封面
套件詳情v4.1.0

inview_notifier_list

一個建構 ListView 並在組件出現在螢幕上時通知的 Flutter 套件。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
4380
喜歡數
501
Pub 分數
160

安裝套件

flutter pub add inview_notifier_list

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個高效的 Flutter 套件,用於建構可捲動的清單,並在清單項目進入或離開可視螢幕區域時提供即時通知。它利用 ListView 的強大功能,同時加入反應式機制來偵測可見狀態,進而實現優化渲染、懶加載與效能提升。適用於圖像載入、分析追蹤或由可見性觸發的動畫等情境。此套件支援所有主要平台,包含行動裝置、網頁與桌面端。

適用情境

  • 懶加載圖片或內容
  • 用於分析的視圖可讀性追蹤
  • 觸發可見性動畫
  • 在長列表中優化資源使用
  • 實現帶有可見性檢查的無限滾動

主要特色

  • 即時在視圖內檢測
  • 使用 ListView 實現高效的 widget 重用
  • 跨平台支援
  • 基於回呼的簡單 API
  • 整合所需的最少樣板程式碼

適合對象

  • 開發效能敏感清單的開發者
  • 需要基於可見性操作的應用程式
  • 內容密集型應用,包含動態資料
  • 優先考慮流暢捲動和回應速度的團隊

使用建議

  • 如果有很多監聽器處於活躍狀態,可能會增加記憶體使用量
  • 需要仔細處理回調以避免洩漏
  • 對於非常短的清單來說並不理想,因為開銷超過了效益
  • 最適合頻繁更新或大型資料集的清單

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

inview_notifier_list

pub package CI codecov

A Flutter package that builds a ListView or CustomScrollView and notifies when the widgets are on screen within a provided area.

Example 1 Example 2 Example 3(Auto-play video)
ezgif com-gif-maker (1) ezgif com-video-to-gif (1) ezgif com-video-to-gif (2)
Example 4(Custom Scroll View)
csv_example

Index

Use-cases

  • To auto-play a video when a user scrolls.

  • To add real-time update listeners from database to the posts/content only within an area visible to the user.

    Note: If you have other use cases please update this section and create a PR.

Installation

Just add the package to your dependencies in the pubspec.yaml file:

dependencies:
  inview_notifier_list: ^4.1.0

Basic Usage

Step 1:

Add the InViewNotifierList to your widget tree

import 'package:flutter/material.dart';
import 'package:inview_notifier_list/inview_notifier_list.dart';

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: InViewNotifierList(
		...
      ),
    );
  }
}

Step 2:

Add the required property isInViewPortCondition to the InViewNotifierList widget. This is the function that defines the area which the widgets overlap should be notified as currently in-view.

typedef IsInViewPortCondition = bool Function(
  double deltaTop,
  double deltaBottom,
  double viewPortDimension,
);

It takes 3 parameters:

  1. deltaTop: It is the distance from top of the widget to be notified in the list view to top of the viewport(0.0).

  2. deltaBottom: It is the distance from bottom of the widget to be notified in the list view to top of the viewport(0.0).

  3. viewPortDimension: The height or width of the viewport depending on the srollDirection property provided. The image below showcases the values if the srollDirection is Axis.vertical.

    Untitled Diagram

Here is an example that returns true only when the widget's top is above the halfway of the viewport and the widget's bottom is below the halfway of the viewport. It is shown in example1.

InViewNotifierList(
  isInViewPortCondition:
      (double deltaTop, double deltaBottom, double viewPortDimension) {
    return deltaTop < (0.5 * viewPortDimension) &&
        deltaBottom > (0.5 * viewPortDimension);
  },
),

step 3:

Add the IndexedWidgetBuilder , which builds the children on demand. It is just like the ListView.builder.

InViewNotifierList(
    isInViewPortCondition:(...){...},
    itemCount: 10,
    builder: (BuildContext context, int index) {
      return child;
    }

),
step 4:

Use the InViewNotifierWidget to get notified if the required widget is currently in-view or not.

The InViewNotifierWidget consists of the following properties:

  1. id: a required String property. This should be unique for every widget that wants to get notified.
  2. builder : Signature for a function that creates a widget for a given index. The function that defines and returns the widget that should be notified as inView. See InViewNotifierWidgetBuilder.
  3. child: The child widget to pass to the builder.
InViewNotifierWidget(
  id: 'unique-Id',
  builder: (BuildContext context, bool isInView, Widget child) {
    return Container(
      child: Text(
        isInView ? 'in view' : 'not in view',
      ),
    );
  },
)

That's it, done!

A complete code:

InViewNotifierList(
  isInViewPortCondition:
      (double deltaTop, double deltaBottom, double vpHeight) {
    return deltaTop < (0.5 * vpHeight) && deltaBottom > (0.5 * vpHeight);
  },
  itemCount: 10,
  builder: (BuildContext context, int index) {
    return InViewNotifierWidget(
      id: '$index',
      builder: (BuildContext context, bool isInView, Widget child) {
        return Container(
          height: 250.0,
          color: isInView ? Colors.green : Colors.red,
          child: Text(
            isInView ? 'Is in view' : 'Not in view',
          ),
        );
      },
    );
  },
);

Run the example app provided and check out the folder for complete code.

Types of Notifiers

  1. InViewNotifierList: builds a ListView and notifies when the widgets are on screen within a provided area.
  2. InViewNotifierCustomScrollView: builds a CustomScrollView and notifies when the widgets are on screen within a provided area.

Properties

  • isInViewPortCondition: [Required] The function that defines the area within which the widgets should be notified as in-view.

  • initialInViewIds: The String list of unique ids of the child widgets that should be initialized as in-view when the list view is built for the first time.

  • endNotificationOffset: The distance from the bottom of the list where the onListEndReached should be invoked. Defaults to the end of the list i.e 0.0.

  • onListEndReached: The function that is invoked when the list scroll reaches the end or the endNotificationOffset if provided.

  • throttleDuration: The duration to be used for throttling the scroll notification. Defaults to 200 milliseconds.

  • scrollDirection: The axis along which the scroll view scrolls.

  • scrollViewWrapper: An optional callback that wraps the internal scroll view with an additional widget, such as a pull-to-refresh or shimmer wrapper, while keeping in-view detection working. It receives the fully built scroll view and must return a widget that contains it.

    InViewNotifierList(
      scrollViewWrapper: (scrollView) => RefreshIndicator(
        onRefresh: _onRefresh,
        child: scrollView,
      ),
      // ...
    )
    

    Note: The wrapper must let ScrollNotification events bubble up from the inner scroll view (most widgets do by default). If the wrapper provides its own ScrollController, make sure it does not conflict with the one passed to InViewNotifierList.

Credits:

Thanks to Didier Boelens for the raw solution.

所屬分類

依賴

依賴清單 1 項

原始碼儲存庫

使用此套件的專案