GesturePageView.custom constructor

GesturePageView.custom({
  1. Key? key,
  2. Axis scrollDirection = Axis.horizontal,
  3. bool reverse = false,
  4. ExtendedPageController? controller,
  5. ScrollPhysics? physics,
  6. bool pageSnapping = true,
  7. ValueChanged<int>? onPageChanged,
  8. required SliverChildDelegate childrenDelegate,
  9. DragStartBehavior dragStartBehavior = DragStartBehavior.start,
  10. bool allowImplicitScrolling = false,
  11. String? restorationId,
  12. Clip clipBehavior = Clip.hardEdge,
  13. ScrollBehavior? scrollBehavior,
  14. bool padEnds = true,
  15. int preloadPagesCount = 0,
})

Creates a scrollable list that works page by page with a custom child model.

{@tool snippet}

This GesturePageView uses a custom SliverChildBuilderDelegate to support child reordering.

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

  @override
  State<MyPageView> createState() => _MyPageViewState();
}

class _MyPageViewState extends State<MyPageView> {
  List<String> items = <String>['1', '2', '3', '4', '5'];

  void _reverse() {
    setState(() {
      items = items.reversed.toList();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: PageView.custom(
          childrenDelegate: SliverChildBuilderDelegate(
            (BuildContext context, int index) {
              return KeepAlive(
                data: items[index],
                key: ValueKey<String>(items[index]),
              );
            },
            childCount: items.length,
            findChildIndexCallback: (Key key) {
              final ValueKey<String> valueKey = key as ValueKey<String>;
              final String data = valueKey.value;
              return items.indexOf(data);
            }
          ),
        ),
      ),
      bottomNavigationBar: BottomAppBar(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextButton(
              onPressed: () => _reverse(),
              child: const Text('Reverse items'),
            ),
          ],
        ),
      ),
    );
  }
}

class KeepAlive extends StatefulWidget {
  const KeepAlive({Key? key, required this.data}) : super(key: key);

  final String data;

  @override
  State<KeepAlive> createState() => _KeepAliveState();
}

class _KeepAliveState extends State<KeepAlive> with AutomaticKeepAliveClientMixin{
  @override
  bool get wantKeepAlive => true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Text(widget.data);
  }
}

{@end-tool}

If allowImplicitScrolling is true, the PageView will participate in accessibility scrolling more like a ListView, where implicit scroll actions will move to the next page rather than into the contents of the PageView.

Implementation

GesturePageView.custom({
  Key? key,
  this.scrollDirection = Axis.horizontal,
  this.reverse = false,
  ExtendedPageController? controller,
  this.physics,
  this.pageSnapping = true,
  this.onPageChanged,
  required this.childrenDelegate,
  this.dragStartBehavior = DragStartBehavior.start,
  this.allowImplicitScrolling = false,
  this.restorationId,
  this.clipBehavior = Clip.hardEdge,
  this.scrollBehavior,
  this.padEnds = true,
  this.preloadPagesCount = 0,
})  : assert(childrenDelegate != null),
      assert(allowImplicitScrolling != null),
      assert(clipBehavior != null),
      controller = controller ?? _defaultPageController,
      super(key: key);