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
What is the best way to optionally include a widget in a list of children #3783
Comments
This is a re-post from flutter-users. One suggestion there was to allow |
Option 1 is the currently optimal path. Supporting nulls in child lists seems reasonable. It actually might work already, since we use nulls internally to represent children that have been removed by GlobalKey regrafting. |
The following exception is thrown with --no-checked
|
With this question I'm less concerned about efficiency and more concerned with readability. |
Ok, looks like we have to add null checks in more places then. |
@yjbanov has a proposal that would make the moral equivalent of this code work: Widget build() {
return new Column(
children: [
new Title(),
new Body(),
if (shouldShowFooter) { new Footer() }
]
);
} However, that would require changes to the language, which means it's a bit further out than allowing nulls. |
I think we should just go with option 1 for now. You can use bool notNull(Object o) => o != null;
Widget build() {
return new Column(
children: <Widget>[
new Title(),
new Body(),
shouldShowFooter ? new Footer() : null
].where(notNull).toList(),
);
} |
Issue is still here and I have to use method from my BaseState class @protected
widgets(List<Widget> widgets) {
return widgets..removeWhere((widget) => widget == null);
} and then in the code Column(
children: widgets ([
widget1,
widget2,
(a != null) ? Text(...) : null
]),
) Do you have plans add support nulls in child lists? |
@pavel-ismailov you might want to upvote #17862 |
#3783 (comment) In Kotlin you'd just do something like fun build(): Widget =
Column(
children: buildArray {
add(Title())
add(Body())
if(shouldShowFooter) add(Footer())
}
) where your And crazier DSLs overrode the |
@Zhuinden you might be interested in checking out this proposal: https://github.com/dart-lang/language/blob/master/accepted/future-releases/control-flow-collections/feature-specification.md. It supports it more directly, and it also composes well with other proposals, such as https://github.com/dart-lang/language/blob/master/working/0125-static-immutability/feature-specification.md. |
Check out the Visibility widget. Just found it after reading all this. :) |
Concerning |
Is there a performance difference between doing this
and doing something like this ?
I mean, the simplest solution is short
could be replaced by
The problem i find here is that everytime the build method is called, is gonna rebuild everything , in every solution. Maybe we should have an "empty" widget that works like a null , that way we can include the second option and instead of calling Container(),or SizedBox() , we could call the "null" widget ex : WNull()? |
The canonical way to write this as long as your Dart SDK is at least Widget build() {
return Column(
children: <Widget>[
Title(),
Body(),
if(shouldShowFooter) Footer(),
],
);
} |
:O didn't know that was possible !!!!!!!!!!! Flutter ftw! :D i love you xd <3 |
Yeah, but what if I don't want to use the beta channels and want to keep everything inline? How about using yield? @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: (() sync* {
yield Text( 'You have pushed the button this many times:' );
yield Text('$_counter', style: Theme.of(context).textTheme.display1);
if(this._counter>5){ yield FlatButton(child: Text('Go For It'), onPressed: ()=> launch('https://www.google.com')); }
})().toList(),
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
} It works for me, but I don't have any idea of how much that affects performance. @drewwarren - can this be your option 3? |
This really worked and I think it's the best way. |
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of |
I often find myself optionally wanting a child in a column component. For example let's say I have a Column with a title, body, and optional footer. What is the best way to achieve this?
Option 1: explicitly build the children, omitting the optional child
Option 2: ternary operator using an empty widget when the optional child should not be shown
Option 3...: You show me!
I have been using option 2 but referencing a
final Widget emptyWidget = new Container(width: 0, height: 0)
to make it more readable.What approach would you suggest?
The text was updated successfully, but these errors were encountered: