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

问题描述

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

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

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"),);
  }
}

解决方法

使用了以下代码样式

enter image description here

enter image description here

快乐编码:)

,

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

喜欢

 Class _ProductsState extends State<Products> {

bool valoractual;

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

  @override

  Widget build(BuildContext context) {

return Scaffold(