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
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);
}
}