Home > Software engineering >  How can I save my checkbox value in SharedPreferences?
How can I save my checkbox value in SharedPreferences?

Time:10-26

In this, I used a Agree checkbox and in there is unselecting, when the click can select check box then value is true, That works perfectly. But I wanna add shared Preferences to this When added that, after the selected word and close the app and reopened again then should show the check box value true that is selected at the last. like this enter image description here

my code

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

class AgreeScreen extends StatefulWidget {
  const AgreeScreen({Key? key}) : super(key: key);

  @override
  State<AgreeScreen> createState() => _AgreeScreenState();
}

class _AgreeScreenState extends State<AgreeScreen> {
  // 1st dropdown button
  @override
  void initState() {
    super.initState();
    valueAgree;
    checkValueAgree();
  }

  // // are you agree button
  String? valueAgree;
  // //boolean value (are you agree)
  bool isChecked = false;

  checkValueAgree() {
    _getDataAgree();
  }

  _saveDataAgree(bool isChecked) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool("Agree", isChecked);
  }

  _getDataAgree() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();

    var result = sharedPreferences.getBool("Agree");
    if (result != null) {
      isChecked;
      setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          children: [
            Padding(
              padding: const EdgeInsets.only(top: 100, left: 15.0),
              child: Row(
                children: <Widget>[
                  const Icon(
                    Icons.brightness_1,
                    color: Colors.black,
                    size: 10,
                  ),
                  const Padding(
                    padding: EdgeInsets.only(left: 15.0),
                    child: Text(
                      "Are  you agree",
                      style:
                          TextStyle(fontSize: 16, fontWeight: FontWeight.w500),
                    ),
                  ),
                  const Padding(
                    padding: EdgeInsets.only(left: 30),
                    child: Text(
                      'yes',
                      style:
                          TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
                    ),
                  ),
                  Checkbox(
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8.0),
                    ),
                    side: MaterialStateBorderSide.resolveWith(
                      (states) =>
                          BorderSide(width: 3.0, color: Colors.blueAccent),
                    ),
                    value: isChecked,
                    onChanged: (bool? value) {
                      setState(() {
                        isChecked = value!;
                      });
                    },
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(bottom: 0.0, top: 150),
              child: SizedBox(
                width: 160.0,
                height: 35.0,
                child: ElevatedButton(
                    style: ButtonStyle(
                      shape: MaterialStateProperty.all<RoundedRectangleBorder>(
                        RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(18.0),
                          side: const BorderSide(
                            color: Colors.blueAccent,
                          ),
                        ),
                      ),
                    ),
                    onPressed: () {
                      //do null check 1st

                      _saveDataAgree(isChecked);
                    },
                    child: const Text('next')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

In here you need pass you SharedPreferences value to isChecked, because your Checkbox work with isChecked, you don't need String? valueAgree just remove it:

_getDataAgree() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
  
    var result = sharedPreferences.getBool("Agree");
    if (result != null){
        isChecked = result;
        setState(() {});
    }
  }

and change your _saveDataAgree to this:

_saveDataAgree(bool isChecked) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setBool("Agree", isChecked);
    
  }

CodePudding user response:

If you save the question as key and the selected item as value it will be good to go.

  • Related