Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: why I cannot use the final member field as the padding of widget?

Tags:

flutter

dart

I write the following widget:

class Bottombar extends StatelessWidget {
  final double padding;
  Bottombar({@required this.padding});
  @override
  Widget build(BuildContext context) {
    return new Container(
      padding: const EdgeInsets.all(padding), child: new Text('Test'),);
  }
}

However, it reports compiling error that the padding parameter must be constant expressions. Why cannot use the final member field as the parameter of widget padding? For what reason?

The flutter version is beta 0.3.1

like image 488
mianlaoshu Avatar asked May 19 '18 12:05

mianlaoshu


People also ask

How do you add padding to widget on Flutter?

But in Flutter, if you want add some extra space around a widget, then you wrap it in a Padding widget. Now to add padding, wrap the Text widget with a Padding widget. In Android Studio this can be accomplished by placing your cursor on the widget and pressing Option+Enter (or Alt+Enter in Windows/Linux).

What is the difference between margin and padding in Flutter?

The main difference between the padding and margin is: Padding provides the space between the border and the content of an element. Margin provides the space between the border and outer elements.

How do you ignore padding in Flutter?

You can create a seperate class that creates the buttons and specify the padding once. Then you can call that class again and again to create as many buttons as you want in the Column. Now you don't need to specify the padding for the Column and the Row will not have any padding. Save this answer.


1 Answers

The problem is you're using const to create the EdgeInsets but then passing a variable padding.

Remove const and it should work.

like image 115
Edman Avatar answered Sep 22 '22 14:09

Edman