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

Hooks don't match flutter Widget #452

Closed
lwj1994 opened this issue Jan 1, 2025 · 3 comments
Closed

Hooks don't match flutter Widget #452

lwj1994 opened this issue Jan 1, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request needs triage

Comments

@lwj1994
Copy link

lwj1994 commented Jan 1, 2025

Is your feature request related to a problem? Please describe.
Flutter widget is base on class extends. this Widget System is not match with hooks. SwiftUI and Compose, by contrast, have completely discarded class. their hooks no sense of violation. https://github.com/ra1028/swiftui-hooks
https://github.com/junerver/ComposeHooks
Trying to shoehorn hooks into an class inherited Widget is awkward and doesn't work well. Classes provide their own clear lifecycle, such as initState and dispose, and adding hooks on top only creates a tangled mess, driving up development costs. Users will find it very confusing.

Hooks work best when used in functions; we should completely move away from inheritance.

Describe the solution you'd like
this is a Compose-Style, only use method to define widget instead of class

Widget TestWidget({int initValue = 0}) {
  return HookBuilder(
    builder: (BuildContext context) {
      final c = useState(initValue);
      
      // equals State.didUpdate
      if(c.value != initValue){
        // trigger rebuild
        c.value = initValue;
      }   


      useEffect(() {
        print("TestWidget useEffect");
        return () {
          print("TestWidget dispose");
        };
      }, []);
      return GestureDetector(
        onTap: () {
          c.value++;
        },
        child: Container(
          child: Column(
            children: [
              Text("TestWidget"),
              Text("c: ${c.value}"),
            ],
          ),
        ),
      );
    },
  );
}


///Class-Widget just as A Wrapper when necessary. example: when you need a route page.
@RoutePage()
class TestWidgetPage extends StatelessWidget {
  const TestWidgetPage({super.key});

  @override
  Widget build(BuildContext context) {
    return TestWidget();
  }
}

Maybe we can expose HookBuilder only. force user write method-widget.

in #441, The real reason is just that he's using hooks in a class, which is causing some confusion. he changed controller, but the widget not rebuilding, if he use method-widget. he must define controller like this. Because method-widgets require useState for controllers, controller changes will always cause a rebuild.

Widget PageViewPage({Key? key}) {
  return HookBuilder(
    key: key,
    builder: (context) {
      final pageController = useState(PageController());
      return PageView(
        controller: pageController.value,
        //  ...
      );
      ;
    },
  );
}
@lwj1994 lwj1994 added enhancement New feature or request needs triage labels Jan 1, 2025
@lwj1994 lwj1994 changed the title move away from inheritance. move away from inheritance-widget. Jan 1, 2025
@lwj1994
Copy link
Author

lwj1994 commented Jan 2, 2025

That's a bad idea. https://stackoverflow.com/questions/53234825/what-is-the-difference-between-functions-and-classes-to-create-reusable-widgets

At least for hooks, the combination of the two is messy.

class Example extends HookWidget {
  const Example({super.key});


  @override
  Widget build(BuildContext context) {
    final innerState = useState("name");
    return Text(innerState.value);
  }
}

Example is Stateless, why it hold a inner state. Stateless Widget should pure and only dependency constructor args.

The fact that method-widget might not enable a full widget-tree with associated system optimizations is, in my view, an acceptable tradeoff for using hooks. If we're not comfortable with that, then we should avoid using hooks. The design philosophies of these two are contradictory.

Maybe hooks not match with flutter

@lwj1994
Copy link
Author

lwj1994 commented Jan 2, 2025

Hooks smooth out the differences between stateless and stateful. So it is very confusing whether your Widget has state or not.
What is the significance of distinguishing states in Flutter Widget?
so i say it don't match flutter widget system

hooks's definition of state is completely out of the flutter Widget system

@lwj1994 lwj1994 changed the title move away from inheritance-widget. Hooks don't match flutter Widget Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

2 participants