0

我所读到的所有内容都表明您可以使用 SetState 完成这项工作。这是我一直在使用的,它不会重绘。

class ServerSelectionState extends State<ServerSelection> {

  bool server1Selected, server2Selected, server3Selected = false;

  @override
  void initState() {
    setState(() {
      server1Selected= false;
      server2Selected= false;
      server3Selected= false;
    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
    //Basically have an onTap method from a ListTile that opens a dialog, like this:  
    onTap: () {
      showDialog(
        context: context,
        builder: (BuildContext context) {
          return AlertDialog(
            backgroundColor: greyTheme,
            content: Form(
              child: Container(
                children: <Widget>[
                  ButtonTheme(
                    child: RaisedButton(
                      onPressed: (){
                        if (!mounted) return;
                          setState(() {
                            server1Selected = true;
                            server2Selected = false;
                            server3Selected = false;
                          });
                        },
                        color: server1Selected == true ? Colors.pink : Colors.blue,
                        child: Text(
                          'Server 1',
                          style: TextStyle(
                            color: Colors.red,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            ),
          }
       );
     }

这将在热重载时很好地更新按钮颜色,但在此之前,重绘似乎并不快乐。if (!mounted) 部分防止了我看到的关于在重绘或类似之前调用 dispose 的错误,并且打印输出这正在工作,它正在正确更改布尔值的状态,它实际上并没有更新颜色直到您强制小部件重绘。

4

2 回答 2

1

showDialog 直接无法更改值,但要这样做,您的对话框必须是有状态的小部件。

从您的代码中可以看出,您必须创建另一个有状态的小部件,它返回整个警报对话框,如下面的代码。

您必须在新的有状态小部件中传递所有需要的变量。

import 'package:flutter/material.dart';

class Homestack extends StatefulWidget {
  @override
  _HomestackState createState() => _HomestackState();
}

class _HomestackState extends State<Homestack> {


  String dropdownValue ;

  @override
  Widget build(BuildContext context) {

    return new Container(
      color: Colors.blue,
        padding: const EdgeInsets.all(20),
        child: GestureDetector(
          child: new Text("Tap ME!"),
          onTap: (){
            return showDialog(
                context: context,
                builder: (context){
                  return xyz(
                    server3Selected: false,
                    greyTheme: Colors.blueAccent,
                    server1Selected: false,
                    server2Selected: false,
                  );
                }
            );
          },
        )
    );

  }
}



class xyz extends StatefulWidget {

    Color greyTheme ;
    bool server1Selected;
    bool server2Selected;
    bool server3Selected;

    xyz({Key key, this.greyTheme,this.server1Selected,this.server2Selected,this.server3Selected}): super(key: key);

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

  class _xyzState extends State<xyz> {
    @override
    Widget build(BuildContext context) {
      return AlertDialog(
        backgroundColor: widget.greyTheme,
        content: Form(
          child: Column(
            children: <Widget>[
              ButtonTheme(
                child: RaisedButton(
                  onPressed: (){
                    if (!mounted) return;
                    setState(() {
                      widget.server1Selected = true;
                      widget.server2Selected = false;
                      widget.server3Selected = false;
                    });
                  },
                  color: widget.server1Selected == true ? Colors.pink : Colors.blue,
                  child: Text(
                    'Server 1',
                    style: TextStyle(
                      color: Colors.red,
                    ),
                  ),
                ),
              ),
            ],
          ),
        ),
      );
    }
  }
于 2019-07-03T05:43:30.780 回答
0

来自颤振文档:如果您尝试更改按钮的颜色并且它没有任何效果,请检查您是否传递了非空 onPressed 或 onLongPress 回调。

于 2020-08-14T02:18:04.657 回答