What is the difference between functions and classes to create widgets?
up vote
8
down vote
favorite
I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this:
Widget function( String title, VoidCallback callback )
return GestureDetector(
onTap: callback,
child: // some widget
);
This is interesting because it requires far less code than a full-blown class. Example:
class SomeWidget extends StatelessWidget
final VoidCallback callback;
final String title;
const SomeWidget(Key key, this.callback, this.title) : super(key: key);
@override
Widget build(BuildContext context)
return GestureDetector(
onTap: callback,
child: // some widget
);
So I've been wondering: Is there any difference besides syntax between functions and classes to create widgets? And is it a good practice to use functions?
dart flutter
add a comment |
up vote
8
down vote
favorite
I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this:
Widget function( String title, VoidCallback callback )
return GestureDetector(
onTap: callback,
child: // some widget
);
This is interesting because it requires far less code than a full-blown class. Example:
class SomeWidget extends StatelessWidget
final VoidCallback callback;
final String title;
const SomeWidget(Key key, this.callback, this.title) : super(key: key);
@override
Widget build(BuildContext context)
return GestureDetector(
onTap: callback,
child: // some widget
);
So I've been wondering: Is there any difference besides syntax between functions and classes to create widgets? And is it a good practice to use functions?
dart flutter
add a comment |
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this:
Widget function( String title, VoidCallback callback )
return GestureDetector(
onTap: callback,
child: // some widget
);
This is interesting because it requires far less code than a full-blown class. Example:
class SomeWidget extends StatelessWidget
final VoidCallback callback;
final String title;
const SomeWidget(Key key, this.callback, this.title) : super(key: key);
@override
Widget build(BuildContext context)
return GestureDetector(
onTap: callback,
child: // some widget
);
So I've been wondering: Is there any difference besides syntax between functions and classes to create widgets? And is it a good practice to use functions?
dart flutter
I have realized that it is possible to create widgets using plain functions instead of subclassing StatelessWidget. An example would be this:
Widget function( String title, VoidCallback callback )
return GestureDetector(
onTap: callback,
child: // some widget
);
This is interesting because it requires far less code than a full-blown class. Example:
class SomeWidget extends StatelessWidget
final VoidCallback callback;
final String title;
const SomeWidget(Key key, this.callback, this.title) : super(key: key);
@override
Widget build(BuildContext context)
return GestureDetector(
onTap: callback,
child: // some widget
);
So I've been wondering: Is there any difference besides syntax between functions and classes to create widgets? And is it a good practice to use functions?
dart flutter
dart flutter
asked Nov 10 at 0:03
Rémi Rousselet
21.3k23371
21.3k23371
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
13
down vote
accepted
TDLR: Never ever use functions over classes to make reusable widget-tree. Always extract these into a StatelessWidget instead.
There is a huge difference between using functions instead of classes, that is: The framework is unaware of functions, but can see classes.
Consider the following "widget" function:
Widget functionWidget( Widget child)
return Container(child: child);
used this way:
functionWidget(
child: functionWidget(),
);
And it's class equivalent:
class ClassWidget extends StatelessWidget
final Widget child;
const ClassWidget(Key key, this.child) : super(key: key);
@override
Widget build(BuildContext context)
return Container(
child: child,
);
used like that:
new ClassWidget(
child: new ClassWidget(),
);
On paper, both seems to do exactly the same thing: Create 2 Container
, with one nested into the other. But the reality is slightly different.
In the case of functions, the generated widget tree looks like this:
Container
Container
While with classes, the widget tree is:
ClassWidget
Container
ClassWidget
Container
This is very important because it radically changes how the framework behaves when updating a widget. Here's a curated list of the differences:
- Since
ClassWidget
is inserted into the widget tree, it gets associated with an Element and can, therefore, have a BuildContext and specify aKey
. This leads to better performances overall, asClassWidget
s can update independently from other widgets. ClassWidget
can be hot-reloaded,functionWidget
cannot. Since the framework doesn't seefunctionWidget
, then when its code change, the framework doesn't know that it has anything to hot-reload.- With
functionWidget
you could end up with very weird bug if you ever wanted to use a different function after some updates. This happens because since the framework doesn't see thefunctionWidget
, it might reuse some old widget state instead of creating a new one. - By using classes, each part of your layout is always used in the same way: Using constructor. This abstracts the implementation details. It's important because with functions if you ever wanted to change it to a StatefulWidget, it'd be a breaking change.
The conclusion should be pretty clear already:
Do not use functions to create widgets.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
13
down vote
accepted
TDLR: Never ever use functions over classes to make reusable widget-tree. Always extract these into a StatelessWidget instead.
There is a huge difference between using functions instead of classes, that is: The framework is unaware of functions, but can see classes.
Consider the following "widget" function:
Widget functionWidget( Widget child)
return Container(child: child);
used this way:
functionWidget(
child: functionWidget(),
);
And it's class equivalent:
class ClassWidget extends StatelessWidget
final Widget child;
const ClassWidget(Key key, this.child) : super(key: key);
@override
Widget build(BuildContext context)
return Container(
child: child,
);
used like that:
new ClassWidget(
child: new ClassWidget(),
);
On paper, both seems to do exactly the same thing: Create 2 Container
, with one nested into the other. But the reality is slightly different.
In the case of functions, the generated widget tree looks like this:
Container
Container
While with classes, the widget tree is:
ClassWidget
Container
ClassWidget
Container
This is very important because it radically changes how the framework behaves when updating a widget. Here's a curated list of the differences:
- Since
ClassWidget
is inserted into the widget tree, it gets associated with an Element and can, therefore, have a BuildContext and specify aKey
. This leads to better performances overall, asClassWidget
s can update independently from other widgets. ClassWidget
can be hot-reloaded,functionWidget
cannot. Since the framework doesn't seefunctionWidget
, then when its code change, the framework doesn't know that it has anything to hot-reload.- With
functionWidget
you could end up with very weird bug if you ever wanted to use a different function after some updates. This happens because since the framework doesn't see thefunctionWidget
, it might reuse some old widget state instead of creating a new one. - By using classes, each part of your layout is always used in the same way: Using constructor. This abstracts the implementation details. It's important because with functions if you ever wanted to change it to a StatefulWidget, it'd be a breaking change.
The conclusion should be pretty clear already:
Do not use functions to create widgets.
add a comment |
up vote
13
down vote
accepted
TDLR: Never ever use functions over classes to make reusable widget-tree. Always extract these into a StatelessWidget instead.
There is a huge difference between using functions instead of classes, that is: The framework is unaware of functions, but can see classes.
Consider the following "widget" function:
Widget functionWidget( Widget child)
return Container(child: child);
used this way:
functionWidget(
child: functionWidget(),
);
And it's class equivalent:
class ClassWidget extends StatelessWidget
final Widget child;
const ClassWidget(Key key, this.child) : super(key: key);
@override
Widget build(BuildContext context)
return Container(
child: child,
);
used like that:
new ClassWidget(
child: new ClassWidget(),
);
On paper, both seems to do exactly the same thing: Create 2 Container
, with one nested into the other. But the reality is slightly different.
In the case of functions, the generated widget tree looks like this:
Container
Container
While with classes, the widget tree is:
ClassWidget
Container
ClassWidget
Container
This is very important because it radically changes how the framework behaves when updating a widget. Here's a curated list of the differences:
- Since
ClassWidget
is inserted into the widget tree, it gets associated with an Element and can, therefore, have a BuildContext and specify aKey
. This leads to better performances overall, asClassWidget
s can update independently from other widgets. ClassWidget
can be hot-reloaded,functionWidget
cannot. Since the framework doesn't seefunctionWidget
, then when its code change, the framework doesn't know that it has anything to hot-reload.- With
functionWidget
you could end up with very weird bug if you ever wanted to use a different function after some updates. This happens because since the framework doesn't see thefunctionWidget
, it might reuse some old widget state instead of creating a new one. - By using classes, each part of your layout is always used in the same way: Using constructor. This abstracts the implementation details. It's important because with functions if you ever wanted to change it to a StatefulWidget, it'd be a breaking change.
The conclusion should be pretty clear already:
Do not use functions to create widgets.
add a comment |
up vote
13
down vote
accepted
up vote
13
down vote
accepted
TDLR: Never ever use functions over classes to make reusable widget-tree. Always extract these into a StatelessWidget instead.
There is a huge difference between using functions instead of classes, that is: The framework is unaware of functions, but can see classes.
Consider the following "widget" function:
Widget functionWidget( Widget child)
return Container(child: child);
used this way:
functionWidget(
child: functionWidget(),
);
And it's class equivalent:
class ClassWidget extends StatelessWidget
final Widget child;
const ClassWidget(Key key, this.child) : super(key: key);
@override
Widget build(BuildContext context)
return Container(
child: child,
);
used like that:
new ClassWidget(
child: new ClassWidget(),
);
On paper, both seems to do exactly the same thing: Create 2 Container
, with one nested into the other. But the reality is slightly different.
In the case of functions, the generated widget tree looks like this:
Container
Container
While with classes, the widget tree is:
ClassWidget
Container
ClassWidget
Container
This is very important because it radically changes how the framework behaves when updating a widget. Here's a curated list of the differences:
- Since
ClassWidget
is inserted into the widget tree, it gets associated with an Element and can, therefore, have a BuildContext and specify aKey
. This leads to better performances overall, asClassWidget
s can update independently from other widgets. ClassWidget
can be hot-reloaded,functionWidget
cannot. Since the framework doesn't seefunctionWidget
, then when its code change, the framework doesn't know that it has anything to hot-reload.- With
functionWidget
you could end up with very weird bug if you ever wanted to use a different function after some updates. This happens because since the framework doesn't see thefunctionWidget
, it might reuse some old widget state instead of creating a new one. - By using classes, each part of your layout is always used in the same way: Using constructor. This abstracts the implementation details. It's important because with functions if you ever wanted to change it to a StatefulWidget, it'd be a breaking change.
The conclusion should be pretty clear already:
Do not use functions to create widgets.
TDLR: Never ever use functions over classes to make reusable widget-tree. Always extract these into a StatelessWidget instead.
There is a huge difference between using functions instead of classes, that is: The framework is unaware of functions, but can see classes.
Consider the following "widget" function:
Widget functionWidget( Widget child)
return Container(child: child);
used this way:
functionWidget(
child: functionWidget(),
);
And it's class equivalent:
class ClassWidget extends StatelessWidget
final Widget child;
const ClassWidget(Key key, this.child) : super(key: key);
@override
Widget build(BuildContext context)
return Container(
child: child,
);
used like that:
new ClassWidget(
child: new ClassWidget(),
);
On paper, both seems to do exactly the same thing: Create 2 Container
, with one nested into the other. But the reality is slightly different.
In the case of functions, the generated widget tree looks like this:
Container
Container
While with classes, the widget tree is:
ClassWidget
Container
ClassWidget
Container
This is very important because it radically changes how the framework behaves when updating a widget. Here's a curated list of the differences:
- Since
ClassWidget
is inserted into the widget tree, it gets associated with an Element and can, therefore, have a BuildContext and specify aKey
. This leads to better performances overall, asClassWidget
s can update independently from other widgets. ClassWidget
can be hot-reloaded,functionWidget
cannot. Since the framework doesn't seefunctionWidget
, then when its code change, the framework doesn't know that it has anything to hot-reload.- With
functionWidget
you could end up with very weird bug if you ever wanted to use a different function after some updates. This happens because since the framework doesn't see thefunctionWidget
, it might reuse some old widget state instead of creating a new one. - By using classes, each part of your layout is always used in the same way: Using constructor. This abstracts the implementation details. It's important because with functions if you ever wanted to change it to a StatefulWidget, it'd be a breaking change.
The conclusion should be pretty clear already:
Do not use functions to create widgets.
edited Nov 13 at 14:47
answered Nov 10 at 0:03
Rémi Rousselet
21.3k23371
21.3k23371
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234825%2fwhat-is-the-difference-between-functions-and-classes-to-create-widgets%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown