Creating your own flutter widgets best practice
up vote
3
down vote
favorite
I am trying to understand the best style for creating your own widgets in flutter, and here are 2 very simplified examples
With the code at the bottom, I can use 1)
new SomeWidget("Some title", someFunction);
or 2)
SomeWidget.widget("Some title", someFunction);
or 3) Some other way I'm not aware of
Method 1) feels more correct (if I've not made some mistakes), however method 2) actually has less code (as I don't need to declare the object variables earlier, assuming I don't need access to context), but I'm wary of static methods.
Is 1) preferred, and why ?
class SomeWidget extends StatelesssWidget
String title;
Function callback;
SomeWidget( this.title, this.callback );
//method 1
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
)
//method 2
static Widget widget(String title, Function callback)
return GestureDetector(
onTap: callback,
child: ....some widget
)
dart flutter
add a comment |
up vote
3
down vote
favorite
I am trying to understand the best style for creating your own widgets in flutter, and here are 2 very simplified examples
With the code at the bottom, I can use 1)
new SomeWidget("Some title", someFunction);
or 2)
SomeWidget.widget("Some title", someFunction);
or 3) Some other way I'm not aware of
Method 1) feels more correct (if I've not made some mistakes), however method 2) actually has less code (as I don't need to declare the object variables earlier, assuming I don't need access to context), but I'm wary of static methods.
Is 1) preferred, and why ?
class SomeWidget extends StatelesssWidget
String title;
Function callback;
SomeWidget( this.title, this.callback );
//method 1
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
)
//method 2
static Widget widget(String title, Function callback)
return GestureDetector(
onTap: callback,
child: ....some widget
)
dart flutter
1
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
1
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to understand the best style for creating your own widgets in flutter, and here are 2 very simplified examples
With the code at the bottom, I can use 1)
new SomeWidget("Some title", someFunction);
or 2)
SomeWidget.widget("Some title", someFunction);
or 3) Some other way I'm not aware of
Method 1) feels more correct (if I've not made some mistakes), however method 2) actually has less code (as I don't need to declare the object variables earlier, assuming I don't need access to context), but I'm wary of static methods.
Is 1) preferred, and why ?
class SomeWidget extends StatelesssWidget
String title;
Function callback;
SomeWidget( this.title, this.callback );
//method 1
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
)
//method 2
static Widget widget(String title, Function callback)
return GestureDetector(
onTap: callback,
child: ....some widget
)
dart flutter
I am trying to understand the best style for creating your own widgets in flutter, and here are 2 very simplified examples
With the code at the bottom, I can use 1)
new SomeWidget("Some title", someFunction);
or 2)
SomeWidget.widget("Some title", someFunction);
or 3) Some other way I'm not aware of
Method 1) feels more correct (if I've not made some mistakes), however method 2) actually has less code (as I don't need to declare the object variables earlier, assuming I don't need access to context), but I'm wary of static methods.
Is 1) preferred, and why ?
class SomeWidget extends StatelesssWidget
String title;
Function callback;
SomeWidget( this.title, this.callback );
//method 1
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
)
//method 2
static Widget widget(String title, Function callback)
return GestureDetector(
onTap: callback,
child: ....some widget
)
dart flutter
dart flutter
edited Nov 10 at 0:02
Rémi Rousselet
20.6k23068
20.6k23068
asked Nov 9 at 19:45
Ian
11.2k23252
11.2k23252
1
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
1
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05
add a comment |
1
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
1
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05
1
1
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
1
1
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
I don't know actual guildelines, but I would prefer something like
class SomeWidget extends StatelesssWidget
SomeWidget(this.title, this.callback);
final String title;
final VoidCallback callback;
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
);
or you can do like this
SomeWidget(this.title = '', @required this.callback)
for default values or if some value is reqired
P.S. All this is not guideline - it's just an IMHO )
2
Andrey is right with this one. Anyway, If you have aStatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use aStatefulWidget
.
– Niklas
Nov 10 at 8:14
1
You're right, I just missed this moment. Actually, fields inStatefulWidget
have to be immutable too )
– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in theState
of theStatefulWidget
.
– Niklas
Nov 10 at 9:17
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
I don't know actual guildelines, but I would prefer something like
class SomeWidget extends StatelesssWidget
SomeWidget(this.title, this.callback);
final String title;
final VoidCallback callback;
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
);
or you can do like this
SomeWidget(this.title = '', @required this.callback)
for default values or if some value is reqired
P.S. All this is not guideline - it's just an IMHO )
2
Andrey is right with this one. Anyway, If you have aStatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use aStatefulWidget
.
– Niklas
Nov 10 at 8:14
1
You're right, I just missed this moment. Actually, fields inStatefulWidget
have to be immutable too )
– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in theState
of theStatefulWidget
.
– Niklas
Nov 10 at 9:17
add a comment |
up vote
2
down vote
accepted
I don't know actual guildelines, but I would prefer something like
class SomeWidget extends StatelesssWidget
SomeWidget(this.title, this.callback);
final String title;
final VoidCallback callback;
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
);
or you can do like this
SomeWidget(this.title = '', @required this.callback)
for default values or if some value is reqired
P.S. All this is not guideline - it's just an IMHO )
2
Andrey is right with this one. Anyway, If you have aStatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use aStatefulWidget
.
– Niklas
Nov 10 at 8:14
1
You're right, I just missed this moment. Actually, fields inStatefulWidget
have to be immutable too )
– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in theState
of theStatefulWidget
.
– Niklas
Nov 10 at 9:17
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
I don't know actual guildelines, but I would prefer something like
class SomeWidget extends StatelesssWidget
SomeWidget(this.title, this.callback);
final String title;
final VoidCallback callback;
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
);
or you can do like this
SomeWidget(this.title = '', @required this.callback)
for default values or if some value is reqired
P.S. All this is not guideline - it's just an IMHO )
I don't know actual guildelines, but I would prefer something like
class SomeWidget extends StatelesssWidget
SomeWidget(this.title, this.callback);
final String title;
final VoidCallback callback;
Widget build(context)
return GestureDetector(
onTap: callback,
child: ....some widget
);
or you can do like this
SomeWidget(this.title = '', @required this.callback)
for default values or if some value is reqired
P.S. All this is not guideline - it's just an IMHO )
edited Nov 10 at 9:08
Niklas
2697
2697
answered Nov 9 at 19:52
Andrey Turkovsky
1,342515
1,342515
2
Andrey is right with this one. Anyway, If you have aStatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use aStatefulWidget
.
– Niklas
Nov 10 at 8:14
1
You're right, I just missed this moment. Actually, fields inStatefulWidget
have to be immutable too )
– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in theState
of theStatefulWidget
.
– Niklas
Nov 10 at 9:17
add a comment |
2
Andrey is right with this one. Anyway, If you have aStatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use aStatefulWidget
.
– Niklas
Nov 10 at 8:14
1
You're right, I just missed this moment. Actually, fields inStatefulWidget
have to be immutable too )
– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in theState
of theStatefulWidget
.
– Niklas
Nov 10 at 9:17
2
2
Andrey is right with this one. Anyway, If you have a
StatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use a StatefulWidget
.– Niklas
Nov 10 at 8:14
Andrey is right with this one. Anyway, If you have a
StatelessWidget
all fields of the Widget should be immutable e.g. final. If the State of the Widget should be able to change, use a StatefulWidget
.– Niklas
Nov 10 at 8:14
1
1
You're right, I just missed this moment. Actually, fields in
StatefulWidget
have to be immutable too )– Andrey Turkovsky
Nov 10 at 9:15
You're right, I just missed this moment. Actually, fields in
StatefulWidget
have to be immutable too )– Andrey Turkovsky
Nov 10 at 9:15
Oh, I meant fields in the
State
of the StatefulWidget
.– Niklas
Nov 10 at 9:17
Oh, I meant fields in the
State
of the StatefulWidget
.– Niklas
Nov 10 at 9:17
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%2f53232345%2fcreating-your-own-flutter-widgets-best-practice%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
1
Your second version doesn't even need or use SomeWidget. It could be a top level function, making SomeWidget redundant.
– Richard Heap
Nov 9 at 22:35
1
Ignore the pointless edit. After some thoughts it should have been asked in another question, which I made here: stackoverflow.com/questions/53234825/…. This question should answer yours too, go take a look ! :)
– Rémi Rousselet
Nov 10 at 0:05