Home > Net >  How to save the state of checkbox in flutter. It should remain checked or unchecked even if we navig
How to save the state of checkbox in flutter. It should remain checked or unchecked even if we navig

Time:07-20

How to save the checkbox state means when we checked it,it should remain checked even if we navigate through ought or close the program in flutter. Here I have used provider to save the state Please guide how to save checkbox state once we checked or unchecked it in flutter.below is my code:

main.dart file-

import 'dart:developer';

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

    import 'package:samplecheckbox/checkprovider.dart';

    import 'package:samplecheckbox/myhomepage.dart';

void main() {
  runApp(MultiProvider(providers: [
    ChangeNotifierProvider(create: (_) => CheckProvider()),
  ], child: MyApp()));
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Create a Checkbox',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

provider.dart file-

import 'package:flutter/material.dart';

class CheckProvider extends ChangeNotifier {
  bool _checkbox = false;
  void checkValue() {
    _checkbox = !_checkbox;
  }
}

myhomepage.dart file-

import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:samplecheckbox/checkprovider.dart';

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  bool _checkbox = false;
  bool _checkboxListTile = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Create a Checkbox'),
      ),
      body: Center(
        child: Column(
          children: [
            Row(
              children: [
                Checkbox(
                  value: _checkbox,
                  onChanged: (value) =>
                      context.read<CheckProvider>().checkValue(),
                )
              ],
            ),
            
          ],
        ),
      ),
    );
  }
}

CodePudding user response:

You can use the shared_preferences to store your key-value pair data in a flutter.

To Store data:

// Obtain shared preferences.

final prefs = await SharedPreferences.getInstance();

// Save an boolean value to 'repeat' key.

await prefs.setBool('isCheckboxChecked', true);

To Get Data:

final bool? isChecked = prefs.getBool('isCheckboxChecked');

CodePudding user response:

if your chick box values does not care of server then use shared_preferences

if your chick box values does care of server then shared_preferences will never help and you need to return bool value from your server every time you visit that specific page .

if you need to save the values within the app usage then you need to use global bool value

  • Related