skip to Main Content

The container always displays an error like that

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
        padding:  EdgeInsets.symmetric(vertical: 30),
        width: double.infinity,
        decoration: const BoxDecoration(
          gradient: LinearGradient(
            begin: Alignment.topCenter,
            colors: [
              Color.fromARGB(255, 9, 106, 185),
              Color.fromARGB(255, 56, 145, 217),
              Color.fromARGB(255, 66, 161, 239),
              Color.fromARGB(255, 109, 179, 237)
            ]
          )
        ),
        child: const Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 80,),
            Padding(
              padding: EdgeInsets.all(20),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  Text("Login", style: TextStyle(color: Colors.white, fontSize: 40),),
                  SizedBox(height: 5,),
                  Text("Welcome Back", style: TextStyle(color: Colors.white, fontSize: 18),)
                ],
              ),      
            ),
            Expanded(
              child: Container(
              decoration:  BoxDecoration(
               color: Colors.white,
                borderRadius: BorderRadius.only(topLeft: Radius.circular(50), topRight: Radius.circular(50))
              ),
              child: Padding(
              padding: EdgeInsets.all(20),
              child: Column(
              children: <Widget>[
              Container(
                decoration: BoxDecoration(
                  boxShadow:  [BoxShadow(
                    color:  Color.fromARGB(255, 5, 97, 173),
                    blurRadius: 20,
                    offset: Offset(0,10)
                  )]
                ),
              )
            ],
          ),
        ),
      ));
          ],
        )
      ),
    );
  }
}

whereas I have deleted the const

2

Answers


  1. I fixed your code, take a look

    class HomePage extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Container(
              padding:  EdgeInsets.symmetric(vertical: 30),
              width: double.infinity,
              decoration: const BoxDecoration(
                  gradient: LinearGradient(
                      begin: Alignment.topCenter,
                      colors: [
                        Color.fromARGB(255, 9, 106, 185),
                        Color.fromARGB(255, 56, 145, 217),
                        Color.fromARGB(255, 66, 161, 239),
                        Color.fromARGB(255, 109, 179, 237)
                      ]
                  )
              ),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                SizedBox(height: 80,),
              Padding(
                padding: EdgeInsets.all(20),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text("Login", style: TextStyle(color: Colors.white, fontSize: 40),),
                    SizedBox(height: 5,),
                    Text("Welcome Back", style: TextStyle(color: Colors.white, fontSize: 18),)
                  ],
                ),
              ),
              Expanded(
                  child: Container(
                    decoration:  BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(topLeft: Radius.circular(50), topRight: Radius.circular(50))
                    ),
                    child: Padding(
                      padding: EdgeInsets.all(20),
                      child: Column(
                        children: <Widget>[
                          Container(
                            decoration: BoxDecoration(
                                boxShadow:  [BoxShadow(
                                    color:  Color.fromARGB(255, 5, 97, 173),
                                    blurRadius: 20,
                                    offset: Offset(0,10)
                                )]
                            ),
                          )
                        ],
                      ),
                    ),
                  )
              )
              ],
            )
        ),
        );
      }
    }
    

    for more Flutter Constant constructors information, please check link.

    Login or Signup to reply.
  2. Working code:

    class HomePage extends StatelessWidget {
      const HomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
              padding: const EdgeInsets.symmetric(vertical: 30),
              width: double.infinity,
              decoration: const BoxDecoration(
                  gradient: LinearGradient(begin: Alignment.topCenter, colors: [
                Color.fromARGB(255, 9, 106, 185),
                Color.fromARGB(255, 56, 145, 217),
                Color.fromARGB(255, 66, 161, 239),
                Color.fromARGB(255, 109, 179, 237)
              ])),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  const SizedBox(
                    height: 80,
                  ),
                  const Padding(
                    padding: EdgeInsets.all(20),
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: <Widget>[
                        Text(
                          "Login",
                          style: TextStyle(color: Colors.white, fontSize: 40),
                        ),
                        SizedBox(
                          height: 5,
                        ),
                        Text(
                          "Welcome Back",
                          style: TextStyle(color: Colors.white, fontSize: 18),
                        )
                      ],
                    ),
                  ),
                  Expanded(
                      child: Container(
                    decoration: const BoxDecoration(
                        color: Colors.white,
                        borderRadius: BorderRadius.only(
                            topLeft: Radius.circular(50),
                            topRight: Radius.circular(50))),
                    child: Padding(
                      padding: const EdgeInsets.all(20),
                      child: Column(
                        children: <Widget>[
                          Container(
                            decoration: const BoxDecoration(boxShadow: [
                              BoxShadow(
                                  color: Color.fromARGB(255, 5, 97, 173),
                                  blurRadius: 20,
                                  offset: Offset(0, 10))
                            ]),
                          )
                        ],
                      ),
                    ),
                  )),
                ],
              )),
        );
      }
    }
    

    What I did is: Removed const in front of Column widget and added const on children’s of Column as per the lint.

    Explanation: We can’t make constant Container widget due to that you are getting. If you see the Container widget, we do not have a constant constructor, due to which we are unable to use const keyword in Container widget.

    I hope this is helpful. Thank you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search