Flutter “更改颜色时使用共享首选项”按钮

Flutter “更改颜色时使用共享首选项”按钮,flutter,dart,Flutter,Dart,我有两个RaisedButton,您只能单击其中一个。单击一个后,无法使用另一个或取消选择您的选择。按钮单击时会更改颜色。一个是绿色的,另一个是红色的。喜欢正确和错误。(图中的示例仅适用于绿色。右侧的可变为红色。) 他们的工作没有问题 我想用共享的\u首选项保存用户的选择,只保存绿色 我猜我写的东西或我用的地方是错的。就像这里: @override void initState() { super.initState(); getGreen(); } Future

我有两个
RaisedButton
,您只能单击其中一个。单击一个后,无法使用另一个或取消选择您的选择。按钮单击时会更改颜色。一个是绿色的,另一个是红色的。喜欢正确和错误。(图中的示例仅适用于绿色。右侧的可变为红色。)

他们的工作没有问题

我想用
共享的\u首选项
保存用户的选择,只保存绿色

我猜我写的东西或我用的地方是错的。就像这里:

@override
  void initState() {
    super.initState();
    getGreen();
  }

  Future<bool> saveGreen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool ('answerGreen', true);
  }

  Future<bool> getGreen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    return _correct = prefs.getBool("answerGreen");
  }
...
...
 onPressed: () => {
                  setState(() {
                    _correct = !_wrong;
                    saveGreen();
                  })
                },

您应该在按下时分开按钮并仅执行特定操作

RaisedButton(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:_正确?颜色。绿色:颜色。蓝色,
按下:()=>{
设置状态(){
_正确=!\u错误;
_错误=!\u正确;
如果(正确){
saveGreen();
}
})
},
),
升起的按钮(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:错了?颜色。红色:颜色。蓝色,
按下:()=>{
设置状态(){
_错误=!\u正确;
_正确=!\u错误;
})
},
)

您应该在按下时分开按钮并仅执行特定操作

RaisedButton(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:_正确?颜色。绿色:颜色。蓝色,
按下:()=>{
设置状态(){
_正确=!\u错误;
_错误=!\u正确;
如果(正确){
saveGreen();
}
})
},
),
升起的按钮(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:错了?颜色。红色:颜色。蓝色,
按下:()=>{
设置状态(){
_错误=!\u正确;
_正确=!\u错误;
})
},
)

请您试试这个,我已经编辑了代码,请告诉我这个代码是否完美


class ButtonChangeColor extends StatefulWidget {
  @override
  _ButtonChangeColorState createState() => _ButtonChangeColorState();
}

class _ButtonChangeColorState extends State<ButtonChangeColor> {
  bool _correct = false;
  bool _wrong = false;

  @override
  void initState() {
    super.initState();
    getGreen();
    getRed();
  }

  Future<bool> saveGreen(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('answerGreen', value);
  }

  Future<bool> getGreen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _correct = prefs.getBool("answerGreen");
if(_correct==null){
_correct=false;
}
    setState(() {});
  }

  Future<bool> saveRed(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('answerRed', value);
  }

  Future<bool> getRed() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _wrong = prefs.getBool("answerRed");
if(_wrong == null){
_wrong= false;
}
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              RaisedButton(
                child: Text('Change Color'),
                textColor: Colors.white,
                color: _correct ? Colors.green : Colors.blue,
                onPressed: () => {
                  setState(() {
                    // _correct = !_wrong;
                    _correct = true;
                    _wrong = false;
                    saveGreen(true);
                    saveRed(false);
                    //saveGreen();  //??
                  })
                },
              ),
              RaisedButton(
                child: Text('Change Color'),
                textColor: Colors.white,
                color: _wrong ? Colors.red : Colors.blue,
                onPressed: () => {
                  setState(() {
                    _correct = false;
                    _wrong = true;
                    saveGreen(false);
                    saveRed(true);
                  })
                },
              )
            ],
          ),
        ],
      ),
    );
  }
}



类ButtonChangeColor扩展StatefulWidget{
@凌驾
_ButtonChangeColorState createState()=>_ButtonChangeColorState();
}
类_ButtonChangeColorState扩展状态{
bool _correct=false;
错误=错误;
@凌驾
void initState(){
super.initState();
getGreen();
getRed();
}
未来保存绿色(布尔值)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
首选设置工具('answerGreen',值);
}
Future getGreen()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
_correct=prefs.getBool(“answerGreen”);
如果(_correct==null){
_正确=错误;
}
setState((){});
}
未来保存(布尔值)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
首选设置工具(“应答”,值);
}
Future getRed()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
_错误=prefs.getBool(“应答”);
如果(_错误==null){
_错误=错误;
}
setState((){});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
升起的按钮(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:_正确?颜色。绿色:颜色。蓝色,
按下:()=>{
设置状态(){
//_correct=!_error;
_正确=正确;
_错误=错误;
saveGreen(真);
保存(假);
//saveGreen();/??
})
},
),
升起的按钮(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:错了?颜色。红色:颜色。蓝色,
按下:()=>{
设置状态(){
_正确=错误;
_错=真;
saveGreen(假);
saveRed(真);
})
},
)
],
),
],
),
);
}
}

请您试试这个,我已经编辑了代码,请告诉我这个代码是否完美


class ButtonChangeColor extends StatefulWidget {
  @override
  _ButtonChangeColorState createState() => _ButtonChangeColorState();
}

class _ButtonChangeColorState extends State<ButtonChangeColor> {
  bool _correct = false;
  bool _wrong = false;

  @override
  void initState() {
    super.initState();
    getGreen();
    getRed();
  }

  Future<bool> saveGreen(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('answerGreen', value);
  }

  Future<bool> getGreen() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _correct = prefs.getBool("answerGreen");
if(_correct==null){
_correct=false;
}
    setState(() {});
  }

  Future<bool> saveRed(bool value) async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setBool('answerRed', value);
  }

  Future<bool> getRed() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    _wrong = prefs.getBool("answerRed");
if(_wrong == null){
_wrong= false;
}
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              RaisedButton(
                child: Text('Change Color'),
                textColor: Colors.white,
                color: _correct ? Colors.green : Colors.blue,
                onPressed: () => {
                  setState(() {
                    // _correct = !_wrong;
                    _correct = true;
                    _wrong = false;
                    saveGreen(true);
                    saveRed(false);
                    //saveGreen();  //??
                  })
                },
              ),
              RaisedButton(
                child: Text('Change Color'),
                textColor: Colors.white,
                color: _wrong ? Colors.red : Colors.blue,
                onPressed: () => {
                  setState(() {
                    _correct = false;
                    _wrong = true;
                    saveGreen(false);
                    saveRed(true);
                  })
                },
              )
            ],
          ),
        ],
      ),
    );
  }
}



类ButtonChangeColor扩展StatefulWidget{
@凌驾
_ButtonChangeColorState createState()=>_ButtonChangeColorState();
}
类_ButtonChangeColorState扩展状态{
bool _correct=false;
错误=错误;
@凌驾
void initState(){
super.initState();
getGreen();
getRed();
}
未来保存绿色(布尔值)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
首选设置工具('answerGreen',值);
}
Future getGreen()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
_correct=prefs.getBool(“answerGreen”);
如果(_correct==null){
_正确=错误;
}
setState((){});
}
未来保存(布尔值)异步{
SharedReferences prefs=等待SharedReferences.getInstance();
首选设置工具(“应答”,值);
}
Future getRed()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
_错误=prefs.getBool(“应答”);
如果(_错误==null){
_错误=错误;
}
setState((){});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
正文:专栏(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
划船(
mainAxisAlignment:mainAxisAlignment.space,
儿童:[
升起的按钮(
子项:文本(“更改颜色”),
textColor:Colors.white,
颜色:_正确?颜色。绿色:颜色。蓝色,
按下:()=>{
设置状态(){
//_correct=!_error;
_正确=正确;
_错误=错误;