MVVM Design Pattern in Flutter

后端 未结 4 1017
时光说笑
时光说笑 2021-02-06 05:26

we try to develop a flutter app and we create a stateful widget as a page .
we want to separate build function from other state variable and state function in 2 different fi

4条回答
  •  一个人的身影
    2021-02-06 06:19

    That's not the proper approach. You shouldn't split State and it's build method. The thing is, don't extend widgets. Compose them.

    A correct way to achieve something similar is to use InheritedWidget. These will hold you data but do nothing else. And it's children will be able to request those datas using a MyInherited.of(context).

    You could also create a builder. Something like :

    typedef Widget MyStateBuilder(BuildContext context, MyStateState state);
    
    class MyState extends StatefulWidget {
      final MyStateState builder;
    
      const MyState({this.builder}) : assert(builder != null);
    
      @override
      MyStateState createState() => new MyStateState();
    }
    
    class MyStateState extends State {
      String name;
    
      @override
      Widget build(BuildContext context) {
        return widget.builder(context, this);
      }
    }
    

提交回复
热议问题