Search code examples
fluttersetstate

Instance member 'setState' can't be accessed using static access


I am trying to use CheckBox in flutter, but I am facing a problem using setState inside of it. This the code of the checkbox.

Checkbox(
                    value: check1,
                    checkColor: Colors.white,
                    activeColor: Colors.green,
                    onChanged: (bool? value1) {
                      State.setState(() {
                        check1 = value1;
                      });
                    },
                  ),

And this is the whole code

// ignore_for_file: sized_box_for_whitespace, duplicate_ignore, avoid_unnecessary_containers, avoid_print

// ignore: unnecessary_import
import 'dart:ui';

import 'package:flutter/material.dart';

// ignore: must_be_immutable
class Register extends StatelessWidget {
  var emailController = TextEditingController();
  var passwordController = TextEditingController();
  var firstNameController = TextEditingController();
  var lastNameController = TextEditingController();
  var userNameController = TextEditingController();
  var confirmPasswordController = TextEditingController();
  bool? check1 = false;

  Register({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: const Icon(
          Icons.menu,
        ),
        actions: const [
          IconButton(onPressed: sSearch, icon: Icon(Icons.search)),
          IconButton(
            onPressed: noNotification,
            icon: Icon(Icons.notification_important_rounded),
          )
        ],
        centerTitle: true,
        backgroundColor: const Color.fromARGB(255, 80, 146, 4),
        elevation: 15,
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Center(
          child: SingleChildScrollView(
            child:
                Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
              Container(
                alignment: AlignmentDirectional.center,
                child: const Text(
                  'Register',
                  style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
                ),
              ),
              const SizedBox(
                height: 40,
              ),
              Row(
                children: [
                  Expanded(
                    child: TextFormField(
                      controller: firstNameController,
                      keyboardType: TextInputType.text,
                      onFieldSubmitted: (String value) {
                        print(value);
                      },
                      onChanged: (String value) {
                        print(value);
                      },
                      decoration: const InputDecoration(
                          prefixIcon: Icon(Icons.person),
                          labelText: 'First Name',
                          border: OutlineInputBorder()),
                    ),
                  ),
                  Expanded(
                    child: TextFormField(
                      controller: lastNameController,
                      keyboardType: TextInputType.text,
                      onFieldSubmitted: (String value) {
                        print(value);
                      },
                      onChanged: (String value) {
                        print(value);
                      },
                      decoration: const InputDecoration(
                          prefixIcon: Icon(Icons.person),
                          labelText: 'Last Name',
                          border: OutlineInputBorder()),
                    ),
                  ),
                ],
              ),
              const SizedBox(
                height: 10,
              ),
              TextFormField(
                controller: userNameController,
                keyboardType: TextInputType.text,
                onFieldSubmitted: (String value) {
                  print(value);
                },
                onChanged: (String value) {
                  print(value);
                },
                decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.supervised_user_circle),
                    labelText: 'UserName',
                    border: OutlineInputBorder()),
              ),
              const SizedBox(
                height: 10,
              ),
              TextFormField(
                controller: emailController,
                keyboardType: TextInputType.emailAddress,
                onFieldSubmitted: (String value) {
                  print(value);
                },
                onChanged: (String value) {
                  print(value);
                },
                decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.email),
                    labelText: 'Email Address',
                    border: OutlineInputBorder()),
              ),
              const SizedBox(
                height: 10,
              ),
              TextFormField(
                controller: passwordController,
                keyboardType: TextInputType.visiblePassword,
                obscureText: true,
                onFieldSubmitted: (String value) {
                  print(value);
                },
                onChanged: (String value) {
                  print(value);
                },
                decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.lock),
                    suffixIcon: Icon(Icons.remove_red_eye),
                    labelText: 'Password',
                    border: OutlineInputBorder()),
              ),
              const SizedBox(
                height: 10,
              ),
              TextFormField(
                controller: confirmPasswordController,
                keyboardType: TextInputType.visiblePassword,
                obscureText: true,
                onFieldSubmitted: (String value) {
                  print(value);
                },
                onChanged: (String value) {
                  print(value);
                },
                decoration: const InputDecoration(
                    prefixIcon: Icon(Icons.lock),
                    suffixIcon: Icon(Icons.remove_red_eye),
                    labelText: 'Confirm Password',
                    border: OutlineInputBorder()),
              ),
              const SizedBox(
                height: 20,
              ),
              Container(
                width: double.infinity,
                color: Colors.green,
                child: MaterialButton(
                  onPressed: () {
                    print(emailController.text);
                    print(passwordController.text);
                  },
                  child: const Text(
                    'LOGIN',
                    style: TextStyle(
                        fontSize: 20,
                        fontWeight: FontWeight.bold,
                        color: Colors.white),
                  ),
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Checkbox(
                    value: check1,
                    checkColor: Colors.white,
                    activeColor: Colors.green,
                    onChanged: (bool? value1) {
                      State.setState(() {
                        check1 = value1;
                      });
                    },
                  ),
                ],
              )
            ]),
          ),
        ),
      ),
    );
  }
}

void noNotification() {
  // ignore: avoid_print
  print('Notification Clicked');
}

void sSearch() {
// ignore: avoid_print
  print('Search Clicked');
}

I didn't understand what was the problem, I would be appreciated if someone could explain it to me.


Solution

  • for using setstate your widget should be stateful widget check the code below

    // ignore_for_file: sized_box_for_whitespace, duplicate_ignore, avoid_unnecessary_containers, avoid_print
    
    import 'package:flutter/material.dart';
    
    class Register extends StatefulWidget {
      const Register({super.key});
    
      @override
      State<Register> createState() => _RegisterState();
    }
    
    class _RegisterState extends State<Register> {
      var emailController = TextEditingController();
    
      var passwordController = TextEditingController();
    
      var firstNameController = TextEditingController();
    
      var lastNameController = TextEditingController();
    
      var userNameController = TextEditingController();
    
      var confirmPasswordController = TextEditingController();
    
      bool? check1 = false;
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            leading: const Icon(
              Icons.menu,
            ),
            actions: const [
              IconButton(onPressed: sSearch, icon: Icon(Icons.search)),
              IconButton(
                onPressed: noNotification,
                icon: Icon(Icons.notification_important_rounded),
              )
            ],
            centerTitle: true,
            backgroundColor: const Color.fromARGB(255, 80, 146, 4),
            elevation: 15,
          ),
          body: Padding(
            padding: const EdgeInsets.all(20.0),
            child: Center(
              child: SingleChildScrollView(
                child:
                    Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
                  Container(
                    alignment: AlignmentDirectional.center,
                    child: const Text(
                      'Register',
                      style: TextStyle(fontSize: 40, fontWeight: FontWeight.bold),
                    ),
                  ),
                  const SizedBox(
                    height: 40,
                  ),
                  Row(
                    children: [
                      Expanded(
                        child: TextFormField(
                          controller: firstNameController,
                          keyboardType: TextInputType.text,
                          onFieldSubmitted: (String value) {
                            print(value);
                          },
                          onChanged: (String value) {
                            print(value);
                          },
                          decoration: const InputDecoration(
                              prefixIcon: Icon(Icons.person),
                              labelText: 'First Name',
                              border: OutlineInputBorder()),
                        ),
                      ),
                      Expanded(
                        child: TextFormField(
                          controller: lastNameController,
                          keyboardType: TextInputType.text,
                          onFieldSubmitted: (String value) {
                            print(value);
                          },
                          onChanged: (String value) {
                            print(value);
                          },
                          decoration: const InputDecoration(
                              prefixIcon: Icon(Icons.person),
                              labelText: 'Last Name',
                              border: OutlineInputBorder()),
                        ),
                      ),
                    ],
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    controller: userNameController,
                    keyboardType: TextInputType.text,
                    onFieldSubmitted: (String value) {
                      print(value);
                    },
                    onChanged: (String value) {
                      print(value);
                    },
                    decoration: const InputDecoration(
                        prefixIcon: Icon(Icons.supervised_user_circle),
                        labelText: 'UserName',
                        border: OutlineInputBorder()),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    controller: emailController,
                    keyboardType: TextInputType.emailAddress,
                    onFieldSubmitted: (String value) {
                      print(value);
                    },
                    onChanged: (String value) {
                      print(value);
                    },
                    decoration: const InputDecoration(
                        prefixIcon: Icon(Icons.email),
                        labelText: 'Email Address',
                        border: OutlineInputBorder()),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    controller: passwordController,
                    keyboardType: TextInputType.visiblePassword,
                    obscureText: true,
                    onFieldSubmitted: (String value) {
                      print(value);
                    },
                    onChanged: (String value) {
                      print(value);
                    },
                    decoration: const InputDecoration(
                        prefixIcon: Icon(Icons.lock),
                        suffixIcon: Icon(Icons.remove_red_eye),
                        labelText: 'Password',
                        border: OutlineInputBorder()),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  TextFormField(
                    controller: confirmPasswordController,
                    keyboardType: TextInputType.visiblePassword,
                    obscureText: true,
                    onFieldSubmitted: (String value) {
                      print(value);
                    },
                    onChanged: (String value) {
                      print(value);
                    },
                    decoration: const InputDecoration(
                        prefixIcon: Icon(Icons.lock),
                        suffixIcon: Icon(Icons.remove_red_eye),
                        labelText: 'Confirm Password',
                        border: OutlineInputBorder()),
                  ),
                  const SizedBox(
                    height: 20,
                  ),
                  Container(
                    width: double.infinity,
                    color: Colors.green,
                    child: MaterialButton(
                      onPressed: () {
                        print(emailController.text);
                        print(passwordController.text);
                      },
                      child: const Text(
                        'LOGIN',
                        style: TextStyle(
                            fontSize: 20,
                            fontWeight: FontWeight.bold,
                            color: Colors.white),
                      ),
                    ),
                  ),
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Checkbox(
                        value: check1,
                        checkColor: Colors.white,
                        activeColor: Colors.green,
                        onChanged: (bool? value1) {
                          setState(() {
                            check1 = value1;
                          });
                        },
                      ),
                    ],
                  )
                ]),
              ),
            ),
          ),
        );
      }
    }
    
    void noNotification() {
      // ignore: avoid_print
      print('Notification Clicked');
    }
    
    void sSearch() {
    // ignore: avoid_print
      print('Search Clicked');
    }