1

你能帮我解决这个问题吗setState()

错误:
没有为“MyApp”类型定义方法“setState”。
尝试将名称更正为现有方法的名称,或定义名为“setState”的方法。

代码:

return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: Text('Gestion Produit', ),
          backgroundColor: color1,
        ),
        backgroundColor: color2,
        body: SingleChildScrollView(
          child: Column(
            children: < Widget > [

              Container(
                padding: EdgeInsets.all(20),
                margin: EdgeInsets.fromLTRB(20, 20, 20, 20),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(20),
                  color: color3,
                ),
                child: Column(
                  children: [
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: refCon,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Votre Reference",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: qteCon,
                        keyboardType: TextInputType.number,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Votre Quantite",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    Container(
                      width: double.infinity,
                      height: 30,
                      child: TextFormField(
                        controller: puCon,
                        keyboardType: TextInputType.number,
                        decoration: new InputDecoration(
                          disabledBorder: InputBorder.none,
                          contentPadding:
                          EdgeInsets.only(left: 15, bottom: 11, top: 11, right: 15),
                          hintText: "Prix Unitaire",
                          hintStyle: TextStyle(color: color1),
                        ),
                      ),
                    ),
                    SizedBox(height: 20),
                    RaisedButton(
                      onPressed: () {
                          setState((){
                            ref = refCon;
                            qte = qteCon;
                            pu = puCon;
                          });
                      },
                      color: color2,
                      textColor: color3,
                      disabledColor: Colors.grey,
                      disabledTextColor: Colors.black,
                      padding: EdgeInsets.all(8.0),
                      splashColor: color1,
                      child: Text(
                        "Ajouter Un Produit",
                        style: TextStyle(fontSize: 20.0),
                      ),
                    )
                  ],
                ),
              ),
4

2 回答 2

1

Flutter 小部件有两种类型 -

  1. StatelessWidget- 他们没有State与之关联的对象。一旦创建/渲染,它们就不能被改变。他们将不得不再次重建

  2. StatefulWidget- 一个State对象与它们相关联。您必须创建另一个扩展该类的State类。

class YellowBird extends StatefulWidget {
  @override
  _YellowBirdState createState() => _YellowBirdState();
}

class _YellowBirdState extends State<YellowBird> {
  @override
  Widget build(BuildContext context) { }
}

查看这篇中等文章

于 2021-01-03T10:53:24.483 回答
1

你的小部件不是有状态的。SetState 方法只能在有状态的小部件中使用。

于 2021-01-03T09:54:41.587 回答