Cannot set height and width using flutter_screenutil package

Viewed 173

I have used the flutter_screenutil package. But when I set width to a widget using ".w" and font size which is "18.sp" is not working. Below is the code

Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20.w),
        child: Column(
          children: [
            Text(
              "Sign Up to Surfyard",
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.w700,
                color: darkTextColor,
              ),
            ),
          ],
        ),

In this, I cannot set the horizontal padding. "20.w" is not working and the following error shows

Invalid constant value.dart(invalid_constant)

These are the packages which I have installed

dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^1.0.2
  flutter_screenutil: ^5.0.0+2
  google_fonts: ^2.1.0
2 Answers

Remove the const keyword as 20.w is not constant

Padding(
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        child: Column(
          children: [
            Text(
              "Sign Up to Surfyard",
              style: TextStyle(
                fontSize: 18.sp,
                fontWeight: FontWeight.w700,
                color: darkTextColor,
              ),
            ),
          ],
        ),

const means the field evaluated at compile time so can not be changed while the app working. .w tag is calculated whenever your widget tree has been built so it can not be const. Remove the const keyword and it will work

Related