Giter Site home page Giter Site logo

Comments (5)

rrousselGit avatar rrousselGit commented on June 5, 2024

The issue is most definitely unrelated to hooks. The controller was correctly created, and the error isn't thrown by flutter_hooks but Flutter.
My guess is that you're trying to use the controller before it is associated with a widget, and therefore trigger this assert.

Also note that your controller isn't listened. So your UI won't update when the value changes.

Two things:

  • Consider raising an issue on the Flutter repo to have them improve their error message
  • Try wrapping your leading in a builder, such as HooKBuilder or ValueListenableBuilder.
    This would delay the isExpanded usage to a point where the controller is linked with a widget
    leading: HookBuilder(
      builder: (context) {
        useListenable(expansionTileController);
        return  expansionTileController.isExpanded
              ? Assets.icons.minus.svg()
              : Assets.icons.plus.svg()
      },

from flutter_hooks.

kovaccc avatar kovaccc commented on June 5, 2024

Firstly, thank you for the prompt response and your suggestions. I understand your point that the issue might be due to the ExpansionTileController being used before it's associated with a widget and the controller not being listened to.

Following your advice, I attempted to use HookBuilder along with useListenable for the ExpansionTileController. However, I've run into a type inference issue. When I use useListenable with ExpansionTileController, I encounter the following error:

Couldn't infer type parameter 'T'.
Tried to infer 'ExpansionTileController' for 'T' which doesn't work:
  Type parameter 'T' is declared to extend 'Listenable?' producing 'Listenable?'.
  The type 'ExpansionTileController' was inferred from:
  Parameter 'listenable' declared as 'T' but argument is 'ExpansionTileController'.
Consider passing explicit type argument(s) to the generic.

This seems to suggest a type mismatch between ExpansionTileController and Listenable?, which useListenable expects.

Is there a workaround or a different approach you would recommend for ensuring that the ExpansionTileController is correctly used and listened to within the context of flutter_hooks? Any further guidance would be greatly appreciated. Thank you for your time and help.

from flutter_hooks.

rrousselGit avatar rrousselGit commented on June 5, 2024

My bad, it's looking like ExpansionTileController isn't a Listenable.

Looks like you have to use ExpansionTileController.of instead;

leading: Builder(
  builder: (context) {
    final controller = ExpansionTileController.of(context);
    return  expansionTileController.isExpanded
          ? Assets.icons.minus.svg()
          : Assets.icons.plus.svg()
  },

from flutter_hooks.

kovaccc avatar kovaccc commented on June 5, 2024

No worries. I tried do it your way, it does not throw the error but also not changing the icon

class CustomExpansionTile extends HookWidget {
  final String title;
  final List<Widget> children;
  final EdgeInsets? tilePadding;
  final bool showDivider;

  const CustomExpansionTile({
    required this.title,
    required this.children,
    this.tilePadding,
    this.showDivider = true,
    super.key,
  });

  @override
  Widget build(BuildContext context) {
    final expansionTileController = useExpansionTileController();
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Theme(
          data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
          child: ExpansionTile(
            controller: expansionTileController,
            tilePadding: tilePadding,
            expandedAlignment: Alignment.topLeft,
            expandedCrossAxisAlignment: CrossAxisAlignment.start,
            trailing: HookBuilder(
              builder: (context) {
                return expansionTileController.isExpanded
                    ? Assets.icons.minus.svg()
                    : Assets.icons.plus.svg();
              },
            ),
            title: Text(
              title,
              style: context.appTextStyles.paragraph2SemiBold,
            ),
            children: children,
          ),
        ),
        if (showDivider) const CustomDivider(),
      ],
    );
  }
}

I will stick to useState instead.

  @override
  Widget build(BuildContext context) {
    final isExpanded = useState(false);
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Theme(
          data: Theme.of(context).copyWith(dividerColor: Colors.transparent),
          child: ExpansionTile(
            tilePadding: tilePadding,
            expandedAlignment: Alignment.topLeft,
            expandedCrossAxisAlignment: CrossAxisAlignment.start,
            trailing: isExpanded.value
                ? Assets.icons.minus.svg()
                : Assets.icons.plus.svg(),
            title: Text(
              title,
              style: context.appTextStyles.paragraph2SemiBold,
            ),
            onExpansionChanged: (bool expanded) => isExpanded.value = expanded,
            children: children,
          ),
        ),
        if (showDivider) const CustomDivider(),
      ],
    );
  }

from flutter_hooks.

rrousselGit avatar rrousselGit commented on June 5, 2024

It's ultimately still unrelated to flutter_hooks. So I'd redirect you to Reddit/StackOverflow/Discord for further help requests :)

from flutter_hooks.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.