-1

i am facing a new problem with my code, and honestly, I cant figure out where is my mistake , i have made the exact same widget in another file, and runs perfectly. I'm starting to believe that there is one problem with some widgets maybe.

I paste my code so you can check it out and tell me where is my mistake (very common ) or maybe is some widget/ line of code that is breaking the code.

import 'package:flutter/material.dart';

void main(List<String> args) {
    runApp(MaterialApp(
     home: Scaffold(
      body: Products(),
     ),
    ));
}

class Products extends StatefulWidget {
  Products({Key key}) : super(key: key);

  @override
  _ProductsState createState() => _ProductsState();
}

  

Class _ProductsState extends State<Products> {
  @override
  Widget build(BuildContext context) {
   bool valoractual;

    @override
    void initState() {
      super.initState();
      valoractual = false;
    }

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Color.fromRGBO(239, 180, 185, 1),
        actions: <Widget>[
          Icon(
            Icons.search,
            size: 25,
          ),
          Switch(
            activeColor: Colors.white,
            inactiveThumbColor: Colors.blue[900],
            value: valoractual,
            onChanged: (bool cambio) {
              setState(() {
                valoractual = cambio;
              });
              //cambiovalor();
              if (valoractual) {
                showDialog(
                  context: context,
                  barrierDismissible: false,
                  builder: (context) => AlertDialog(
                    content: Text(" delete option"),
                    actions: [
                      FlatButton(
                        onPressed: () {
                          print("****************");
                          print(valoractual);
                          Navigator.of(context).pop();
                          return valoractual;
                        },
                        child: Text("Continue"),
                      )
                    ],
                   
                  ),
                );
              } else {
                showDialog(
                  context: context,
                  builder: (context) => AlertDialog(
                    content:
                        Text("view option"),
                    actions: [
                      FlatButton(
                        onPreenter code heressed: () {
                          print("****************");
                          print(valoractual);
                          Navigator.of(context).pop();
                          return valoractual;
                        },
                        child: Text("Aceptar"),
                      )
                    ],
                  ),
                );
              }
            },
          ),
          
          Icon(Icons.delete, size: 20),
        ],
      ),
     
      body: Container(
        margin: EdgeInsets.only(top: 10),
        child: Text("this is sample text"),
      ),
    );
  }
}

2 Answers2

1

try to put your initState function out of the build function

like

 Class _ProductsState extends State<Products> {

bool valoractual;

@override
void initState() {
  super.initState();
  valoractual = false;
}

  @override

  Widget build(BuildContext context) {

return Scaffold(
hyobbb
  • 332
  • 1
  • 9
1

used the following code style

enter image description here

enter image description here

Happy Coding :)

Tushar Nikam
  • 1,445
  • 1
  • 14
  • 21