未引用初始化状态抖动问题

时间:2020-10-27 01:17:57

标签: flutter builder statefulwidget

我的代码遇到了一个新问题,老实说,我不知道我的错误在哪里,我在另一个文件中制作了完全相同的小部件,并且运行正常。 我开始相信某些小部件可能存在一个问题。

我粘贴了我的代码,以便您可以检查出来并告诉我我的错误在哪里(很常见),或者是某些小部件/代码行破坏了代码。

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 个答案:

答案 0 :(得分:0)

使用了以下代码样式

enter image description here

enter image description here

快乐编码:)

答案 1 :(得分:-1)

尝试将initState函数从构建函数中删除

喜欢

 Class _ProductsState extends State<Products> {

bool valoractual;

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

  @override

  Widget build(BuildContext context) {

return Scaffold(