rrousselGit / flutter_hooks

React hooks for Flutter. Hooks are a new kind of object that manages a Widget life-cycles. They are used to increase code sharing between widgets and as a complete replacement for StatefulWidget.
MIT License
3.06k stars 175 forks source link

Implement useTabControllerList for List<TabController> #406

Closed dodatw closed 5 months ago

dodatw commented 6 months ago

I have a requirement. There are an indefinite number of Tabs on my UI, so I need to have an indefinite number of TabControllers. How do I implement in HookWidget ?

It seem bad idea that add useTabController in a for loop.

List<TabController> subControllers;
for(...) {
   subControllers.add(useTabController(initialLength: length));
}

I try to use useRef and handle TabController myself, but it cannot set vsync in hookwidget.

final subControllers = useRef<List<TabController>>([]);
for(...) {
   subControllers.value.add(TabController(length: lenght, vsync: xxx))
}

is there any good idea ?

rrousselGit commented 6 months ago

That would be too specific. I don't think this is a good fit for the core package.

In any cases, chances are you want to use useTabController, but move the hook inside your tab widget. This way no matter how many tabs you have, they would all have a unique controller.

dodatw commented 6 months ago

We need to control each tab from parent, so we need to create tabcontroller at outside. For Other controller, (ex, scrollController), I can use useRef + list view handle ourself. But for TabController, it need TickerProvider, but useTickerProvider can only assign in one of TabController.

Another solution, is it possiable provide hook for List ?

dodatw commented 6 months ago

I think my question should be how to handle indefinite number of TickerProvider in Hook. In useSingleTickerProvider, it say

      throw FlutterError(
          '${context.widget.runtimeType} attempted to use a useSingleTickerProvider multiple times.\n'
          'A SingleTickerProviderStateMixin can only be used as a TickerProvider once. '
          'If you need multiple Ticker, consider using useSingleTickerProvider multiple times '
          'to create as many Tickers as needed.');

How it work in indefinite case ?

rrousselGit commented 6 months ago

SingleTickerProviders cannot be passed to different objects. Use a different ticker provider instead. To do so, call useSingleTickerProvider multiple times.

dodatw commented 6 months ago

The problem is I don't know how many useSingleTickerProvider I need when coding. It is runtime number.

Is that mean I cannot use hookWidget at this case ?

rrousselGit commented 5 months ago

I have no plan for adding a useTabControllerList, so closing.