octomato / preload_page_view

Flutter pre-load PageView widget
Apache License 2.0
170 stars 49 forks source link

How to add indicators? #10

Closed wailashraf71 closed 4 years ago

wailashraf71 commented 4 years ago

I'm using smooth_page_indicator and it needs PageController to work how can I use PreloadPageController ?

ThinkSimple commented 4 years ago

I achieved this behavior by manually editing smooth_page_indicator.dart. Add this to the top: import 'package:preload_page_view/preload_page_view.dart';, after that change the type of the controller from PageController to PreloadPageController.

wailashraf71 commented 4 years ago

@ThinkSimple can you please share the code?

ThinkSimple commented 4 years ago

Just replace this inside smooth_page_indicator.dart

import 'package:flutter/material.dart';

import 'effects/indicator_effect.dart';
import 'effects/worm_effect.dart';
import 'package:preload_page_view/preload_page_view.dart';
import 'painters/indicator_painter.dart';

class SmoothPageIndicator extends AnimatedWidget {
  // a PageView controller to listen for page offset updates
  final PreloadPageController controller;

  /// Holds effect configuration to be used in the [IndicatorPainter]
  final IndicatorEffect effect;

  /// The number of children in [PageView]
  final int count;

  /// If [textDirection] is [TextDirection.rtl], page offset will be reversed
  final TextDirection textDirection;

  SmoothPageIndicator({
    @required this.controller,
    @required this.count,
    this.textDirection,
    this.effect = const WormEffect(),
    Key key,
  })  : assert(controller != null),
        assert(effect != null),
        assert(count != null),
        super(listenable: controller, key: key);

  @override
  Widget build(BuildContext context) {
    // if textDirection is not provided use the nearest directionality up the widgets tree;
    final isRTL =
        (textDirection ?? Directionality.of(context)) == TextDirection.rtl;
    return CustomPaint(
      // different effects have different sizes
      // so we calculate size based on the provided effect
      size: effect.calculateSize(count),
      // rebuild the painter with the new offset every time it updates
      painter: effect.buildPainter(
        count,
        _currentPage,
        isRTL,
      ),
    );
  }

  double get _currentPage {
    try {
      return controller.page ?? controller.initialPage.toDouble();
    } catch (Exception) {
      return controller.initialPage.toDouble();
    }
  }
}