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

Time:10-26

This my output

enter image description here

when the first time open app should display like this but select one radio button and close open then should display that selected one EX:- when the user opens the app at the first then should unselect all values, and then the user select grow and click the next button and close the app after reopening again then should display grow as the selected value.

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 RadioButtonScreen extends StatefulWidget {
  const RadioButtonScreen({Key? key}) : super(key: key);

  @override
  State<RadioButtonScreen> createState() => _RadioButtonScreenState();
}

class _RadioButtonScreenState extends State<RadioButtonScreen> {
  @override
  void initState() {
    super.initState();

    dropdownValueDisease = level;

    checkValueDisease();
  }

  // data which child
  // data which child
  String? dropdownValueDisease;
//   // List of items in our dropdown menu
  String level = 'y';

  //IF "dropdownValueMembers" is empty pass "which" word as a initial value if al ready selected then pass the shared preference value
  checkValueDisease() {
    _getDataDisease();
  }

  _saveDataDisease(String dropdownDiseaseShared) async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    sharedPreferences.setString("Disease", dropdownDiseaseShared);
  }

  _getDataDisease() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    dropdownValueDisease = sharedPreferences.getString("Disease") ?? level;
    setState(() {});
  }

//
//
  @override
  Widget build(BuildContext context) {
    double height = MediaQuery.of(context).size.height;
    return Scaffold(
      body: Container(
        child: Column(
          children: [
            SizedBox(
              height: height * 0.72,
              width: (MediaQuery.of(context).size.width),
              child: Column(
                children: <Widget>[
                  const SizedBox(
                    height: 10,
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: ListTile(
                          minLeadingWidth: 1,
                          leading: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.brightness_1,
                                color: Colors.black,
                                size: 10.0,
                              ),
                            ],
                          ),
                          title: const Text(
                            "taking",
                            style: TextStyle(
                                fontSize: 13.3, fontWeight: FontWeight.w800),
                          ),
                          trailing: Radio(
                            value: "talking",
                            groupValue: level,
                            onChanged: (value) {
                              setState(() {
                                level = value.toString();
                              });
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: ListTile(
                          minLeadingWidth: 1,
                          leading: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.brightness_1,
                                color: Colors.black,
                                size: 10.0,
                              ),
                            ],
                          ),
                          title: const Text(
                            "grow",
                            style: TextStyle(
                                fontSize: 13.3, fontWeight: FontWeight.w800),
                          ),
                          trailing: Radio(
                            value: "grow",
                            groupValue: level,
                            onChanged: (value) {
                              setState(() {
                                level = value.toString();
                              });
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: ListTile(
                          minLeadingWidth: 1,
                          leading: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.brightness_1,
                                color: Colors.black,
                                size: 10.0,
                              ),
                            ],
                          ),
                          title: const Text(
                            "listing ",
                            style: TextStyle(
                                fontSize: 13.3, fontWeight: FontWeight.w800),
                          ),
                          trailing: Radio(
                            value: "listing",
                            groupValue: level,
                            onChanged: (value) {
                              setState(() {
                                level = value.toString();
                              });
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: ListTile(
                          minLeadingWidth: 1,
                          leading: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.center,
                            children: [
                              Icon(
                                Icons.brightness_1,
                                color: Colors.black,
                                size: 10.0,
                              ),
                            ],
                          ),
                          title: const Text(
                            "eye",
                            style: TextStyle(
                                fontSize: 13.3, fontWeight: FontWeight.w800),
                          ),
                          trailing: Radio(
                            value: "eye",
                            groupValue: level,
                            onChanged: (value) {
                              setState(() {
                                level = value.toString();
                              });
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    children: <Widget>[
                      Expanded(
                        child: ListTile(
                          minLeadingWidth: 1,
                          leading: Icon(
                            Icons.brightness_1,
                            color: Colors.black,
                            size: 10.0,
                          ),
                          title: const Text(
                            "autism",
                            style: TextStyle(
                                fontSize: 13.3, fontWeight: FontWeight.w800),
                          ),
                          trailing: Radio(
                            value: "autism",
                            groupValue: level,
                            onChanged: (value) {
                              setState(() {
                                level = value.toString();
                              });
                            },
                          ),
                        ),
                      ),
                    ],
                  ),
                ],
              ),
            ),
            Padding(
              padding: const EdgeInsets.only(
                bottom: 0.0,
              ),
              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: () {
                      _saveDataDisease(level);
                  
                    },
                    child: const Text('next')),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

You are trying to save dropdownValueDisease but you should save level value:

onPressed: () {
     _saveDataDisease(level);
},

also in your _getDataDisease you need pass SharedPreferences value to level not dropdownValueDisease:

_getDataDisease() async {
    SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    dropdownValueDisease = sharedPreferences.getString("Disease") ?? level;
    level = dropdownValueDisease;
    setState(() {});
  }
  • Related