2

I wrote an extension function to add SizedBox between every child in Column or Row to add space between child, instead of putting SizedBox between every child, for which I didn't find any other way around.

Column(
   children: [
      // widgets
   ].setSpace(height: 10),
)

Row(
   children: [
      // widgets
   ].setSpace(width: 10),
)

So here List<Widget> setSpace({double? height, double? width}) takes height or width for the SizedBox used between child. But since height and width are not const I cannot use const SizedBox. So is there any way in dart to say that both the parameters and the return type will ALWAYS be cosnt? like const List<Widget> setSpace({const double? height, const double? width}) like C/C++?

Alraj
  • 170
  • 9
  • 1
    No, it's not possible. Dart does not have a way to indicate that function calls can be constant expressions. – jamesdlin Jul 29 '22 at 17:40

3 Answers3

1

I don't think that's possible, mostly because const can be applied only on constructors and fields, not on generic functions. Maybe you can achieve that by creating your own widget that adds the SizedBox in its build method, and create a const constructor.

EDIT: here's a piece of code of mine of a custom widget with a const constructor.

class UnlockPage extends StatefulWidget {
  final String pcData;
  const UnlockPage({Key? key, required this.pcData}) : super(key: key);


  @override
  Widget build(BuildContext context) {
  [...]
  }
}

EDIT 2: here's a piece of code tested in DartPad. I don't think it can get better than this.

class SpacedColumn extends StatelessWidget {
  final double height;
  final List<Widget> children;

  const SpacedColumn({required this.height, required this.children});

  @override
  Widget build(BuildContext context) {
    var actualChildren = <Widget>[];
    for (var child in children) {
      actualChildren.add(child);
      actualChildren.add(SizedBox(height: height));
    }

    return Column(
      children: actualChildren,
    );
  }
}
il_boga
  • 1,305
  • 1
  • 13
  • 21
  • This is not possible right?, cause even if i create a custom widget which return a `SizedBox` in `build()`, I still cannot use `const` in my `CustomWidget` which would use the same non-const height and width from `setSpace({double? height, double? width?})`, right? – Alraj Jul 29 '22 at 11:55
  • The constructor can be set as `const` , provided that the arguments it takes are defined as `final`. I edited my answer to add an example. You could put height and width as nullable fields for your class, and then use the value to set the size of the `SizexBox` between each child. – il_boga Jul 29 '22 at 12:41
  • To use `const CustomWidget(height: height, width: width)`, both height and width variable should be `const`, which cannot be since it comes from the `setSpace({double? height, double? width})` parameter. So we go back to my real question. Can I make function parameter as `const` so that I can use it in a `const` widget constructor? – Alraj Jul 29 '22 at 13:03
  • You have to give up on the `setSpace` method, and get height and width directly from the constructor. See the second edit, I think that's the best you can achieve. – il_boga Jul 29 '22 at 13:30
  • 1
    It is an alternative, but it would be hassle to implement things Column and Row should have, like main axis, cross axis, etc. in the class. But with setSpace we can use it anywhere where List is the param. So let the SizedBox rebuild then. But thanks for the time. – Alraj Jul 30 '22 at 05:16
1

You can't. As you pass a value this one can be different from one call to others.

Notice that const as not the same signification on Flutter than on other languages.

With Flutter it indicates to the rendering engine that the widget or the method is always the same and that the rendering engine is not obliged to rebuild this Widget when rebuilding the screen.

The keyword that act as const in other languages is final

Alaindeseine
  • 3,260
  • 1
  • 11
  • 21
0

In Dart language const doesn't mean the same as in other languages. You should use final if you don't want to change the values later.

Usama Karim
  • 1,100
  • 4
  • 15
  • I don't need `const` for non-changeable variable, i need it so i can use `const` on `SizedBox', which is only possible if every value passed to the widget is `const` – Alraj Jul 29 '22 at 11:36