如何在Void函数中使用setstate?

时间:2019-10-14 09:51:53

标签: flutter dart

我还是Flutter的新手。我想问为什么我不能在void函数中使用setState()

这是我的代码:我的目标是滑块可以响应。以前的问题我仍然对使用SetState void函数感到困惑。因此,首先,当按下过滤器按钮时,将出现一个弹出窗口,其中将显示过滤器内容的多个类别,然后在价格类别中有一个显示滑块的功能。而且我的滑块就像一张图片,无法更改。但值已更改

void _onButtonPressed1() {
//Function for Button Press "Sort"
showModalBottomSheet(
    context: context,
    builder: (context) {
      return Container(
        color: Colors.black,
        height: 500,
        child: Container(
          child: column1(),
          decoration: BoxDecoration(
              color: Colors.cyan[400],
              borderRadius: BorderRadius.only(
                topLeft: const Radius.circular(30),
                topRight: const Radius.circular(30),
              )),
        ),
      );
    });}

                        new Expanded(
                        child: new Padding(
                        //padding overall filter include text&square
                        padding: EdgeInsets.fromLTRB(15, 0, 20, 0),

                        child: new Card(
                          color: Colors.transparent,
                          shape: const RoundedRectangleBorder(
                            side: BorderSide(
                              color: Colors.cyanAccent,
                            ),
                            borderRadius: BorderRadius.all(Radius.circular(6.0)),
                          ),
                          child: new RaisedButton(
                              child: new Row(children: <Widget>[
                                new Padding(
                                  padding: EdgeInsets.fromLTRB(5.0, 0, 40, 0),
                                  child: new Icon(Icons.format_align_left, color: Colors.cyanAccent),
                                ),
                                new Text(
                                  'Filter',
                                  style: new TextStyle(
                                    fontSize: 13,
                                    color: Colors.cyanAccent,
                                  ),
                                ),
                              ]),
                              color: Colors.black,
                              padding: EdgeInsets.fromLTRB(1, 1, 1, 1),
                              onPressed: () => {
                                    showModalBottomSheet(
                                      context: context,
                                      builder: (context) {
                                        return Container(
                                          color: Colors.black,
                                          height: 350,
                                          child: Container(
                                            // child: column(),

                                            child: Wrap(
                                              //dropdown button
                                              children: <Widget>[
                                                ListView.builder(
                                                    shrinkWrap: true,
                                                    itemCount: 3,
                                                    itemBuilder: (context, index) {
                                                      if (index < 2) {
                                                        final sort = _sort[index];
                                                        return ExpansionTile(
                                                          title: ListTile(
                                                            title: Text(
                                                              sort.category,
                                                              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                                                            ),
                                                          ),
                                                          children: <Widget>[ListTile(title: sort.item)],
                                                        );
                                                      } else {
                                                        return ExpansionTile(
                                                            title: ListTile(
                                                                title: Text(
                                                              'By Price',
                                                              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                                                            )),
                                                            children: <Widget>[
                                                              new RangeSlider(
                                                                min: 1,
                                                                max: 100,
                                                                divisions: 100,
                                                                values: values,
                                                                labels: labels,
                                                                onChanged: (value) {
                                                                  print('START: ${value.start}, END: ${value.end}');
                                                                  values = value;
                                                                  labels = RangeLabels('${values.start.toInt().toString()}\$', '${values.end.toInt().toString()}\$');
                                                                  setState(() {});
                                                                },

                                                                //Data from API
                                                              )
                                                            ]);
                                                      }
                                                    }),
                                              ],
                                            ),
                                            decoration: BoxDecoration(
                                                color: Colors.cyan[400],
                                                borderRadius: BorderRadius.only(
                                                  topLeft: const Radius.circular(30),
                                                  topRight: const Radius.circular(30),
                                                )),
                                          ),
                                        );
                                      },
                                    )
                                  }),
                        ),
                      ),
                    ),

1 个答案:

答案 0 :(得分:0)

onPressed: () => {
                                    showModalBottomSheet(
                                      context: context,
                                      builder: (_) => 
                                      StatefulBuilder(builder:(modalContext, modalSetState) => Container(
                                          color: Colors.black,
                                          height: 350,
                                          child: Container(
                                            height:200,
                                            // child: column(),

                                            child: Wrap(
                                              //dropdown button
                                              children: <Widget>[
                                                new Text(values.start.toString()),
                                                ListView.builder(
                                                    shrinkWrap: true,
                                                    itemCount: 3,
                                                    itemBuilder: (context, index) {
                                                      context=context;
                                                      if (index < 2) {
                                                        final sort = _sort[index];
                                                        return ExpansionTile(
                                                          title: ListTile(
                                                            title: Text(
                                                              sort.category,
                                                              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                                                            ),
                                                          ),
                                                          children: <Widget>[ListTile(title: sort.item)],
                                                        );
                                                      } else {
                                                        return ExpansionTile(
                                                            title: ListTile(
                                                                title: Text(
                                                              'By Price',
                                                              style: TextStyle(color: Colors.white, fontWeight: FontWeight.bold),
                                                            )),
                                                            children: <Widget>[
                                                              new RangeSlider(
                                                                min: 1,
                                                                max: 100,
                                                                divisions: 100,
                                                                values: values,
                                                                labels: labels,
                                                                onChanged: (value) {
                                                                  print('START: ${value.start}, END: ${value.end}');
                                                                  setValuesSlider(value);
                                                                  labels = RangeLabels('${values.start.toInt().toString()}\$', '${values.end.toInt().toString()}\$');
                                                                  modalSetState(() {});
                                                                },

                                                                //Data from API
                                                              )
                                                            ]);
                                                      }
                                                    }),
                                              ],
...

只需使用 StatefulBuilder(builder:(modalContext,modalSetState)