How to use shared preference making incremented container to stay?

105 views
Skip to first unread message

Abinav N

unread,
Jul 8, 2020, 5:37:33 AM7/8/20
to Flutter Development (flutter-dev)
Hi...I'm developing an app in which I'm incrementing the container once the floating button is pressed. 
But once I close the app the changes made were gone.. I'm using shared preference but I don't know how to do this function.
I dont know to use onTapped varible to call container in my code..
This is my code
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() {
  runApp(Grid());
}

setData() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  prefs.setBool("tapped"true);
}

getData() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  bool isTapped = prefs.getBool("tapped") ?? false;
  return isTapped;
}

List<Widget> gridChild = [
  Padding(
    padding: EdgeInsets.all(10.0),
    child: GestureDetector(
      onTap: () {},
      child: Container(
        decoration: BoxDecoration(
          image: DecorationImage(
            image: AssetImage('assets/backg.jpg'),
            fit: BoxFit.cover,
          ),
          borderRadius: BorderRadius.all(Radius.circular(20)),
        ),
        padding: EdgeInsets.all(8.0),
        alignment: Alignment.topCenter,
        child: Text(
          'Project 1',
          textAlign: TextAlign.center,
          style: TextStyle(
              color: Colors.white, fontSize: 25, fontWeight: FontWeight.bold),
        ),
      ),
    ),
  ),
];

class Grid extends StatefulWidget {
  @override
  _GridState createState() => _GridState();
}

class _GridState extends State<Grid> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: new AppBar(
          backgroundColor: const Color(0xFF0099a9),
        ),
       

        body: Container(
          child: GridView.count(
            //padding: EdgeInsets.all(10.0),
            crossAxisCount: 2,
            children:
                List.generate(gridChild.length, (index) => gridChild[index]),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          backgroundColor: Colors.red,
          child: Icon(Icons.add),
          onPressed: () async {
            bool onTapped = await getData();
            setData();

            setState(() {
              gridChild.add(
                Padding(
                  padding: EdgeInsets.all(10.0),
                  child: GestureDetector(
                    onTap: () {},
                    child: Container(
                      decoration: BoxDecoration(
                        image: DecorationImage(
                          image: AssetImage('assets/backg.jpg'),
                          fit: BoxFit.cover,
                        ),
                        borderRadius: BorderRadius.all(Radius.circular(20)),
                      ),
                      padding: EdgeInsets.all(8.0),
                      alignment: Alignment.topCenter,
                      child: Text(
                        'Project 1',
                        textAlign: TextAlign.center,
                        style: TextStyle(
                            color: Colors.white,
                            fontSize: 25,
                            fontWeight: FontWeight.bold),
                      ),
                    ),
                  ),
                ),
              );
              // _incrementCounter();
            });
          },
        ),
      ),
    );
  }
}

Souvik Dutta

unread,
Jul 9, 2020, 12:27:08 AM7/9/20
to Abinav N, Flutter Development (flutter-dev)
What do you mean by incrementing container? 

--
You received this message because you are subscribed to the Google Groups "Flutter Development (flutter-dev)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to flutter-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/flutter-dev/53beec00-d2a5-48be-a516-6885a9f997e3o%40googlegroups.com.

Anoop Rana

unread,
Jul 9, 2020, 3:47:44 AM7/9/20
to Souvik Dutta, flutt...@googlegroups.com

On Thu, 9 Jul 2020, 11:47 Anoop Rana, <anoopr...@gmail.com> wrote:
This is a duplicate of an old thread  where i answered all the relevant questions and how to implement them.Below is the attached thread pdf file.


Anoop Rana

unread,
Jul 9, 2020, 3:49:42 AM7/9/20
to Abinav N, flutt...@googlegroups.com
This is a duplicate of an old thread  where i answered all the relevant questions and how to implement them.Below is the attached thread pdf file.

--
DOUBT.pdf

Anoop Rana

unread,
Jul 9, 2020, 3:53:18 AM7/9/20
to Souvik Dutta, flutt...@googlegroups.com
This is a duplicate of an old thread  where i answered all the relevant questions and how to implement them.Below is the attached thread pdf file.

On Thu, 9 Jul 2020, 13:20 Souvik Dutta, <souvik...@gmail.com> wrote:
You forgot to attach the attachment.

Souvik flutter dev

On Thu, Jul 9, 2020, 12:57 PM Souvik Dutta <souvik...@gmail.com> wrote:
Reply this to evrybody in the group and not to me alone. 

On Thu, Jul 9, 2020, 11:47 AM Anoop Rana <anoopr...@gmail.com> wrote:
This is a duplicate of an old thread  where i answered all the relevant questions and how to implement them.Below is the attached thread pdf file.


On Thu, Jul 9, 2020 at 9:57 AM Souvik Dutta <souvik...@gmail.com> wrote:
DOUBT.pdf
Reply all
Reply to author
Forward
0 new messages