Flutter how to handle time based events? - flutter

I am having a widget in the flutter which can be dismissed by watching a rewarded video. But I don't want the widget to be completely dismissed. Say for 3 days.
So if the user clicks on the specific widget then the ads will be disabled for 3 days. Is it possible to do? Could someone help me with references or ideas to get this done?
Please help

First, Get shared preferences Package to make local storage to track the Date shared_preferences: ^2.0.5
Make A Local Storage like this -
import 'package:shared_preferences/shared_preferences.dart';
class SetUserLocalStorage {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
void date(String valueOfDate) async {
final SharedPreferences prefs = await _prefs;
prefs.setString(UserStorageKey().valueOfDate, valueOfDate);
}
void clear() async { // For Your Further Operation, If needed
final SharedPreferences prefs = await _prefs;
prefs.clear();
}
}
class GetUserLocalStorage {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Future<String> date() async {
final SharedPreferences prefs = await _prefs;
return prefs.getString(UserStorageKey().valueOfDate);
}
}
class UserStorageKey {
String get valueOfDate => "valueOfDate";
}
Now, in your page / Screen, Define Variable -
bool _showTheAd = false;
DateTime _now = DateTime.now();
DateTime _date = DateTime.now();
and In InitState, Start Checking the Condition on the base of Time,I am making it in three part for better understanding
#override
void initState() {
super.initState();
_initialPoint();
}
in _initialPoint() -
void _initialPoint() async {
await GetUserLocalStorage().date().then((value) {
setState(() {
_date = DateTime.parse(value);
});
}).then((value) {
_conditionCheck();
});
}
In _conditionCheck -
void _conditionCheck() {
if (_date == null) {
setState(() {
_showTheAd = true;
});
} else {
setState(() {
_now = DateTime.now();
});
if (_now.isAfter(_date)) {
setState(() {
_showTheAd = true;
});
}
}
}
I know that,these are like "dirty code", but I think that will help you understand the scenario.
in body, show the add based on the _showTheAd condition, and use some interceptor / listener of kind to sense when the video is end,I am using an inkwell, and execute the code in onTap(), full scenario -
Container(
child: Column(
children: [
if (_showTheAd)
InkWell(
onTap: () {
setState(
() {
_date = _now.add(
Duration(seconds: 5),
); // to add Date _now.add(Duration(days:3));
},
);
SetUserLocalStorage().date(_date.toIso8601String());
},
child: Center(
child: Container(
height: 120,
width: 120,
color: Colors.red,
child: Text("the ad"),
),
),
)
],
),
),

Related

How to use sharedpreferences to save users roles and navigate to a specific page depending on role in Flutter

I'm working on app that have user logins (Admin login and user login). First i make a user part and it works, the account keep logged even when the app restart. and then when i have to separate the users (admin and user) i got some problem. I don't know how to code the shared preferences, this is the code when i make a user part
preference_helper.dart
import 'package:shared_preferences/shared_preferences.dart';
class PreferencesHelper {
final Future<SharedPreferences> sharedPreferences;
const PreferencesHelper({required this.sharedPreferences});
static const String login = 'LOGIN';
void setIsLogin(bool value) async {
final prefs = await sharedPreferences;
prefs.setBool(login, value);
}
Future<bool> get isLogin async {
final prefs = await sharedPreferences;
return prefs.getBool(login) ?? false;
}
}
i use the provider like this
preference_notifier.dart
class PreferencesNotifier extends ChangeNotifier {
PreferencesHelper preferencesHelper;
PreferencesNotifier({required this.preferencesHelper}) {
_getIsLogin();
}
bool _isLogin = false;
bool get isLogin => _isLogin;
void _getIsLogin() async {
_isLogin = await preferencesHelper.isLogin;
notifyListeners();
debugPrint(_isLogin ? 'isLogin true' : 'isLogin false');
}
void setIsLogin(bool value) async {
preferencesHelper.setIsLogin(value);
_getIsLogin();
}
}
i want to use shared preferences to save the user roles and navigate to specific page. So if the user's log in it will go to the UserHomePage and if the admin log in it will go to the AdminHomePage. My backend is firebase firestore.
this is part of sign page (when click register button)
MaterialButton(
color: primaryColor,
textTheme: ButtonTextTheme.primary,
height: 40,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
onPressed: () async {
setState(() {
_isLoading = true;
});
try {
final navigator = Navigator.of(context);
final email = _emailController.text;
final password = _passwordController.text;
const role = "user";
await _auth
.createUserWithEmailAndPassword(
email: email,
password: password,
)
.then((value) => {postDetailsToFirestore(email, role)});
navigator.pop();
} catch (err) {
final snackBar = SnackBar(content: Text(err.toString()));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} finally {
setState(() {
_isLoading = false;
});
}
},
child: const Text('Signup'),
),
postDetailsToFirestore(String email, String role) async {
FirebaseFirestore firebaseFirestore = FirebaseFirestore.instance;
var user = _auth.currentUser;
CollectionReference ref = firebaseFirestore.collection('users');
ref.doc(user!.uid).set({'email': _emailController.text, 'role': role});
}
this is the login page (when click the login button)
MaterialButton(
color: primaryColor,
textTheme: ButtonTextTheme.primary,
height: 40,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
onPressed: () async {
setState(() {
_isLoading = true;
});
try {
final navigator = Navigator.of(context);
final email = _emailController.text;
final password = _passwordController.text;
await _auth.signInWithEmailAndPassword(
email: email,
password: password,
);
route();
value.setIsLogin(true);
navigator.pushReplacementNamed(HomePage.routeName);
} catch (err) {
final snackBar = SnackBar(content: Text(err.toString()));
ScaffoldMessenger.of(context).showSnackBar(snackBar);
} finally {
setState(() {
_isLoading = false;
});
}
},
child: const Text('Login'),
),
i want to navigate the navigator to specific user role
this is the route() function
void route() {
User? user = FirebaseAuth.instance.currentUser;
FirebaseFirestore.instance.collection('users').doc(user!.uid).get().then(
(DocumentSnapshot documentSnapshot) {
if (documentSnapshot.exists) {
if (documentSnapshot.get('role') == "user") {
Navigator.pushNamed(context, UserHomePage.routeName);
} else {
Navigator.pushNamed(context, AdminHomePage.routeName);
}
} else {
debugPrint('Document does not exist on the database');
}
},
);
}
and this is the main.dart at runApp()
runApp(
await preferencesHelper.isLogin
? const MyApp(
pageRouteName: HomePage.routeName,
)
: const MyApp(
pageRouteName: LoginPage.routeName,
),
);
I really need to know how am i supposed to do because this is for my exam. I'm sorry if my english is bad, i'm barely use English to talk. Thank you
that code that i share is what i tried to make sharedpreferences but it just for 1 user, i dont know how to separate user (admin and user)
First of all, you need to use architecture to separate the UI from logic and in your architect, you have to create a layer to handle basic requests of the local database and then create a class for implementing basic commands of the database, then you can create a separated storage layer for each of entities that you have.
the abstract basic commands class is like this :
abstract class LocalStorage {
Future<void> write(final String key, final dynamic json);
dynamic read<S>(final String key);
void remove(final String key);
void removeAll();
}
and for implementation :
class StorageService implements LocalStorage {
StorageService() {
_init();
}
late GetStorage storage;
void _init() {
storage = GetStorage();
}
#override
Future<void> write(final String key, final dynamic value) async {
await storage.write(key, convert.jsonEncode(value));
}
#override
dynamic read<S>(final String key) {
final value = storage.read(key);
if (value == null) return;
return convert.jsonDecode(value.toString());
}
#override
void remove(final String key) {
GetStorage().remove(key);
}
#override
void removeAll() {
GetStorage.Remove(key1);
GetStorage.Remove(key2);
...
}
}
and for Usage for each entity:
class UserStorage {
final LocalStorage _storage;
Future<void> SaveUser(User usr) async {
await _storage.write(userKey, usr);
}
}
I have used GetX to handle local storage for read and write but you can replace your preferred shared preference library.

How do I use sharedPreferences with booleans?

I am currently learning sharedPreferences and trying to set and get values to check if a button has been clicked or not.
This is my class for sharedPreferences
class UserSimplePrefences {
static SharedPreferences? _preferences;
static const _keyButton = 'buttonStatus';
static Future init() async {
_preferences = await SharedPreferences.getInstance();
}
static Future setButtonStatus(bool btnStatus) async {
await _preferences?.setBool(_keyButton, btnStatus);
}
static bool? getButtonStatus() {
return _preferences?.getBool(_keyButton);
}
}
here in my main.dart
I have a button.
bool? onLineStatus;
//
void initState() {
super.initState();
WidgetsBinding.instance?.addObserver(this);
onLineStatus = UserSimplePrefences.getButtonStatus() !;
displayToastMessage(onLineStatus.toString(), context);
}
//
#override
Widget build(BuildContext context) {
//
RaisedButton(
onPressed: () async {
if (UserOnline! =true) {
UserOnline = true;
await UserSimplePrefences.setButtonStatus(true);
displayToastMessage("You are Online now", context);
} else {
UserOnline =false;
await UserSimplePrefences.setButtonStatus(false);
displayToastMessage("You are Offline now", context);
}}
),
}
UserOnline is to toggle the button, works fine without SharedPreferences.
In more simple language, when I hit the button i.e Online and close the app and reopen the app sometime later I want the init statement to be called with the toast message as true and similarly when I click offline I want the init statement to call the toast false.
Issue: My toast message is always true.
`
add setState to your onPressed
onPressed: () async {
if (UserOnline! =true) {
UserOnline = true;
await UserSimplePrefences.setButtonStatus(true);
displayToastMessage("You are Online now", context);
setState(() {});
} else {
UserOnline =false;
await UserSimplePrefences.setButtonStatus(false);
displayToastMessage("You are Offline now", context);
setState(() {});
}}

How to save event with sharedpreference in flutter

Hello I try to use this timeline package.
https://github.com/softmarshmallow/flutter-timeline
It's work fine to create timeline after press button but I don't success to save events with sharedpreference. I would like to restore history of the timeline at the initState.
TimelineEventDisplay get plainEventDisplay {
return TimelineEventDisplay(
child: TimelineEventCard(
title: Text("just now"),
content: Text("someone commented on your timeline ${DateTime.now()}"),
),
indicator: TimelineDots.of(context).circleIcon);
}
List<TimelineEventDisplay> events;
Widget _buildTimeline() {
return TimelineTheme(
data: TimelineThemeData(lineColor: Colors.blueAccent),
child: Timeline(
indicatorSize: 56,
events: events,
));
}
void _addEvent() {
setState(() {
events.add(plainEventDisplay);
});
}
#override
void initState() {
events = [
plainEventDisplay,
];
}
Create a SharedPref class so that it would be easy for you to manage things.
import 'package:shared_preferences/shared_preferences.dart';
import 'dart:convert';
class SharedPref {
read(String key) async {
final prefs = await SharedPreferences.getInstance();
if(prefs.getString(key) == null){
return null;
}
final map = jsonDecode(prefs.getString(key));
return map;
}
save(String key, value) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString(key, jsonEncode(value));
}
remove(String key) async {
final prefs = await SharedPreferences.getInstance();
prefs.remove(key);
}
}
In your Flutter widget, create initState as follows:
SharedPref _prefs = SharedPref();
final events;
#override
void initState() async {
super.initState();
events = await _prefs.read('events');
}
void _addEvent() async {
setState(() {
events.add(plainEventDisplay);
});
await _prefs.save('events', events);
}

Load initial text from shared prefs in TextField

I'm trying to have an initial text (of my choice) at first and if the user changes it, it should save in Shared Preferences and load as initial text next time in a TextField.
Edited code a bit but it's still same, I think I'm going wrong somewhere with Shared Preferences.
String initialSignatureText = 'Sent from Mail';
TextEditingController _signatureController = TextEditingController();
String signatureText;
void convertSignature(){
String convertedSignature = _signatureController.text;
setSignature(convertedSignature);
}
void setSignature(String convertedSignature) async{
SharedPreferences signPrefs = await SharedPreferences.getInstance();
signPrefs.setString('signatureTextKey', convertedSignature);
}
Future<String> getSignature() async {
SharedPreferences signPrefs = await SharedPreferences.getInstance();
signatureText = signPrefs.get('signatureTextKey');
print(signatureText);
return signatureText;
}
#override
Widget build(BuildContext context) {
_signatureController.text = signatureText;
TextField(
maxLines: 1,
style: TextStyle(
color: Theme.of(context).textTheme.title.color,
fontSize: 18
),
controller: _signatureController,
showCursor: true,
),
FlatButton(
child: Text('Ok'),
onPressed: (){
convertSignature();
Navigator.pop(context);
},
)
You can assign any string to the text property of your controller
_signatureController.text = "any string";
The second way to do this would be to use TextFormField, It provides you with intialValue property.
I got it. I've not implemented to load initialSignatureText for first time, I'll do it later.
TextEditingController _signatureController = TextEditingController();
String initialSignatureText = 'Sent from Mail.';
var signatureText;
void convertSignature(){
String convertedSignature = _signatureController.text;
setSignature(convertedSignature);
}
void setSignature(String convertedSignature) async{
SharedPreferences signPrefs = await SharedPreferences.getInstance();
signPrefs.setString('signatureTextKey', convertedSignature);
}
Future<String> getSignature() async {
SharedPreferences signPrefs = await SharedPreferences.getInstance();
signatureText = signPrefs.get('signatureTextKey');
return signatureText;
}
void initState() {
super.initState();
getSignature();
WidgetsBinding.instance.addPostFrameCallback((_) async {
_signatureController.text = await getSignature();
});
}

Shared preference save location address from geolocator

i was able to get current location using geolocator, but i want to cache and restore the string address without using lastKnownLocation in geolocator. im using shared preferences but cannot make it work. i used shared preference several times on my other codes, but with geolocator its kind of complicated. and im super new to flutter/dart
code:
final Geolocator geolocator = Geolocator()..forceAndroidLocationManager;
Position _currentPosition;
String _currentAddress;
String _locationCache;
String key = "location_cache";
#override
void initState() {
super.initState();
_getCurrentLocation();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("current location = " + _currentAddress),
Text("last location = " + __locationCache) // HERE GET STORED DATA ISNT WORKING
],
),
),
);
}
_getCurrentLocation() {
geolocator
.getCurrentPosition(desiredAccuracy: LocationAccuracy.best)
.then((Position position) {
setState(() {
_currentPosition = position;
});
_getAddressFromLatLng();
}).catchError((e) {
print(e);
});
}
_getAddressFromLatLng() async {
try {
List<Placemark> p = await geolocator.placemarkFromCoordinates(
_currentPosition.latitude, _currentPosition.longitude);
Placemark place = p[0];
setState(() {
_currentAddress = "${place.country}";
});
saveAddress();
} catch (e) {
print(e);
}
}
Future<bool> saveAddress() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
return await prefs.setString(key, _currentAddress);
}
Future<String> retrieveAddress() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
//Return String
return prefs.getString(key) ?? "";
}
loadAddress() {
retrieveAddress().then((value) {
setState(() {
_locationCache = value;
});
});
}
}
heres the working code without _locationCache:
Thank you for your time
If I understood you correctly, what you want to accomplish is to store the last address you caught and retrieve it if you don't have gps active.
To do so you could use SharedPreferences or SQLite, just check the documentation on how to use them.
found the solution. just replace loadAddress() function with
void save() {
String address = _currentAddress;
saveAddress(address);
}
void _updateName(String address) {
setState(() {
this.locationCache = address;
});
}
and then put retrieveAddress().then(updateName) inside initState()