Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add hook "useFixedExtentScrollController" #433

Closed
ThomasAunvik opened this issue Jun 11, 2024 · 1 comment · Fixed by #437
Closed

Add hook "useFixedExtentScrollController" #433

ThomasAunvik opened this issue Jun 11, 2024 · 1 comment · Fixed by #437
Assignees
Labels
enhancement New feature or request

Comments

@ThomasAunvik
Copy link

ThomasAunvik commented Jun 11, 2024

Is your feature request related to a problem? Please describe.
Add a hook for FixedExtentScrollController

Describe the solution you'd like

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

/// Creates [FixedExtentScrollController] that will be disposed automatically.
///
/// See also:
/// - [FixedExtentScrollController]
FixedExtentScrollController useFixedExtentScrollController({
  int initialItem = 0,
  List<Object?>? keys,
}) {
  return use(
    _FixedExtentScrollControllerHook(
      initialItem: initialItem,
      keys: keys,
    ),
  );
}

class _FixedExtentScrollControllerHook
    extends Hook<FixedExtentScrollController> {
  const _FixedExtentScrollControllerHook({
    required this.initialItem,
    super.keys,
  });

  final int initialItem;

  @override
  HookState<FixedExtentScrollController, Hook<FixedExtentScrollController>>
      createState() => _FixedExtentScrollControllerHookState();
}

class _FixedExtentScrollControllerHookState extends HookState<
    FixedExtentScrollController, _FixedExtentScrollControllerHook> {
  late final controller = FixedExtentScrollController(
    initialItem: hook.initialItem,
  );

  @override
  FixedExtentScrollController build(BuildContext context) => controller;

  @override
  void dispose() => controller.dispose();

  @override
  String get debugLabel => 'useFixedExtentScrollController';
}

Additional context

Example:

ListWheelScrollView(
  controller: scrollController,
  itemExtent: 25,
  squeeze: 0.8,
  onSelectedItemChanged: (value) {
    selected.value = value;
  },
  children: counts.map(
       (index) => OnClick(
           onTap: () {
                 selected.value = index;
                 scrollController.jumpToItem(index);
            },

https://dartpad.dev/?id=a8d241f3fcc2f73a43b77860e6b5f066

@rrousselGit
Copy link
Owner

Sure. Feel free to raise a PR with the relevant tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants