【问题标题】:Shared Preferences use on Change color RaisedButton共享首选项用于更改颜色 RaisedButton
【发布时间】:2021-02-15 03:06:30
【问题描述】:

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

他们的工作没有问题。

我想用shared_preferences 保存用户的选择只有绿色

我猜我写的或我使用的地方是错误的。喜欢这里:

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

如果用户选择绿色,我希望在用户重新启动应用程序时显示绿色。我如何使用shared_preferences 做到这一点?

(我认为保存绿色或红色没有任何区别。或者两者都保存。我无法保存问题)

完整代码如下

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ButtonChangeColor(),
    );
  }
}

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();
  }

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

  @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;
                    //saveGreen();  //??
                  })
                },
              ),
              RaisedButton(
                child: Text('Change Color'),
                textColor: Colors.white,
                color: _wrong ? Colors.red : Colors.blue,
                onPressed: () => {
                  setState(() {
                    _wrong = !_correct;
                  })
                },
              )
            ],
          ),
        ],
      ),
    );
  }
}

【问题讨论】:

  • 使用 Elevated 按钮而不是 RaisedButton。

标签: flutter dart


【解决方案1】:

你可以试试这个,我已经编辑了代码,请告诉我这个代码是否完美?


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


【讨论】:

  • 使用 ElevatedButton 因为 RaisedButton 已被弃用。
  • @Hars Chovatiya 当我使用你的代码时,我得到一个错误boolean expression must not be nulllike here
  • @zeed 现在请检查一下我在现有代码中有编辑代码
  • 虽然没有提供只点击一次的要求,但我的主要问题:保存已经解决。谢谢。使用 _correct = !_wrong_wrong = !_correct ,我提供了一次选择的条件。但是onPressed 继续工作,saveColor 方法有效并更改了保存值。 (它可以在显示绿色的同时保存红色。如果单击第一个绿色,然后单击红色。)我知道这是一个不同的问题主题。再次感谢!
【解决方案2】:

您应该分开按钮并只执行onPressed 中的特定操作:

RaisedButton(
  child: Text('Change Color'),
  textColor: Colors.white,
  color: _correct ? Colors.green : Colors.blue,
  onPressed: () => {
    setState(() {
      _correct = !_wrong;
      _wrong = !_correct;
      if (_correct) {
        saveGreen();
      }
    })
  },
),
RaisedButton(
  child: Text('Change Color'),
  textColor: Colors.white,
  color: _wrong ? Colors.red : Colors.blue,
  onPressed: () => {
    setState(() {
      _wrong = !_correct;
      _correct = !_wrong;
    })
  },
)

【讨论】:

  • 使用 ElevatedButton 而不是 RaisedButton
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 1970-01-01
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多