Skip to content

Instantly share code, notes, and snippets.

@Blasanka
Last active March 30, 2018 10:18
Show Gist options
  • Save Blasanka/5548444078e68a4cc1f42d82e24e3cd0 to your computer and use it in GitHub Desktop.
Save Blasanka/5548444078e68a4cc1f42d82e24e3cd0 to your computer and use it in GitHub Desktop.
In this gist you can learn how to save variable value using Shared_Preferences in Dart and Flutter. Also this sample code shows how you can use RaisedButton Widget, Text Widget, ButtonBar Widget, CenterWidget , TextStyle Widget and lot of properties of those widgets. this codee for simple vote cast app. I wanted to show, how you can use shared p…
//In this gist you can learn how to save variable value using Shared_Preferences in Dart and Flutter.
//this codee for simple vote cast app. I wanted to show, how you can use shared preferences in flutter and dart. This is the
//easiest and understable way. But you can think of a way to do this differently. You can use database, file system and etc
//to store data. I will show those in upcoming videos. This code can be optimized and change as like and dislike for yuor app.
//This is created for the SL Coder youtube channel video tutorial by blasanka
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(new MyApp());
int _upVotes = 0, _downVotes = 0, _count = 0;
class MyApp extends StatefulWidget {
_HomePage createState() => new _HomePage();
}
class _HomePage extends State<MyApp> {
SharedPreferences prefs;
initState() {
super.initState();
init();
}
void init() async {
prefs = await SharedPreferences.getInstance();
_upVotes = prefs.getInt('upVotes');
_downVotes = prefs.getInt('downVotes');
_count = _upVotes + _downVotes;
setPreviousState();
}
void setPreviousState() {
setState(() {
_upVotes;
_downVotes;
_count;
});
}
void _increase() {
setState(() {
_upVotes++;
_count++;
});
prefs.setInt('upVotes', _upVotes);
}
void _decrease() {
setState(() {
_downVotes++;
_count++;
});
prefs.setInt('downVotes', _downVotes);
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Poll App',
home: new Scaffold(
appBar: new AppBar(
title: new Text("Poll App"),
backgroundColor: new Color(0xFF8B1122),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
makeATextWidget("Up Votes :", _upVotes),
new ButtonBar(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
makeAButton('+1', _increase),
makeAButton('-1', _decrease),
],
),
makeATextWidget("Down Votes :", _downVotes),
],
),
),
bottomNavigationBar: new BottomAppBar(
color: new Color(0xFF8B1122),
child: new Text(
'$_count votes casted',
style: new TextStyle(
color: Colors.white,
),
),
),
),
);
}
}
Widget makeAButton(String label, f()) {
return new RaisedButton(
child: new Text(label,
style: new TextStyle(
fontSize: 20.0,
)),
color: new Color(0xFF8B1122),
textColor: Colors.white,
padding: const EdgeInsets.symmetric(
vertical: 15.0, horizontal: 20.0),
elevation: 10.0,
splashColor: Colors.white70,
onPressed: f,
);
}
Widget makeATextWidget(String txt, int value) {
return new Text(
"$txt : $value",
style: new TextStyle(
fontSize: 25.0,
color: new Color(0xFF8B1122),
fontWeight: FontWeight.w600,
),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment