-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
changed the hide play button to using cubit
- Loading branch information
1 parent
41c28ca
commit 38e6302
Showing
7 changed files
with
115 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import 'package:bloc/bloc.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:meta/meta.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
part 'play_button_state.dart'; | ||
|
||
class PlayButtonCubit extends Cubit<PlayButtonState> { | ||
String key = 'playButton'; | ||
SharedPreferences? _pref; | ||
|
||
|
||
PlayButtonCubit() : super(PlayButtonState.initial()) { | ||
_loadFromPref(); | ||
} | ||
|
||
// initialize the shared Preference Instance | ||
Future<void> _initPrefs() async { | ||
_pref ??= await SharedPreferences.getInstance(); | ||
} | ||
|
||
// Load the data from it and check the current value | ||
Future<void> _loadFromPref() async { | ||
await _initPrefs(); | ||
final bool canPlay = _pref!.getBool(key) ?? false; | ||
emit(state.copyWith(canPlay: canPlay)); | ||
} | ||
|
||
// save the new value to the key | ||
Future<void> _saveToPref(bool value) async { | ||
await _initPrefs(); | ||
await _pref!.setBool(key, value); | ||
} | ||
|
||
// toggle between light or dark mode | ||
void togglePlayButton() { | ||
final newCanPlay = !state.canPlay; | ||
_saveToPref(newCanPlay); | ||
emit(state.copyWith(canPlay: newCanPlay)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
part of 'play_button_cubit.dart'; | ||
|
||
@immutable | ||
class PlayButtonState extends Equatable { | ||
final bool canPlay; | ||
|
||
const PlayButtonState({ | ||
this.canPlay = false, | ||
}); | ||
|
||
factory PlayButtonState.initial() { | ||
return const PlayButtonState(); | ||
} | ||
|
||
@override | ||
List<Object> get props => [canPlay]; | ||
|
||
PlayButtonState copyWith({ | ||
bool? canPlay, | ||
}) { | ||
return PlayButtonState( | ||
canPlay: canPlay ?? this.canPlay, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:shared_preferences/shared_preferences.dart'; | ||
|
||
class HidePlayButtonProvider with ChangeNotifier { | ||
final String? key = 'playButton'; | ||
SharedPreferences? _pref; | ||
bool? _mPlayButton; | ||
|
||
bool get mPlayButton => _mPlayButton!; | ||
|
||
HidePlayButtonProvider() { | ||
_mPlayButton = false; | ||
_loadFromPref(); | ||
} | ||
|
||
Future _initPrefs() async { | ||
_pref == null ? _pref = await SharedPreferences.getInstance() : null; | ||
} | ||
|
||
void _loadFromPref() async { | ||
await _initPrefs(); | ||
_mPlayButton = _pref!.getBool(key!) ?? false; | ||
notifyListeners(); | ||
} | ||
|
||
void _saveToPref() async { | ||
await _initPrefs(); | ||
await _pref!.setBool(key!, _mPlayButton!); | ||
} | ||
|
||
void checkButtonState() { | ||
_mPlayButton = !_mPlayButton!; | ||
_saveToPref(); | ||
notifyListeners(); | ||
} | ||
} | ||
// import 'package:flutter/material.dart'; | ||
// import 'package:shared_preferences/shared_preferences.dart'; | ||
// | ||
// class HidePlayButtonProvider with ChangeNotifier { | ||
// final String? key = 'playButton'; | ||
// SharedPreferences? _pref; | ||
// bool? _mPlayButton; | ||
// | ||
// bool get mPlayButton => _mPlayButton!; | ||
// | ||
// HidePlayButtonProvider() { | ||
// _mPlayButton = false; | ||
// _loadFromPref(); | ||
// } | ||
// | ||
// Future _initPrefs() async { | ||
// _pref == null ? _pref = await SharedPreferences.getInstance() : null; | ||
// } | ||
// | ||
// void _loadFromPref() async { | ||
// await _initPrefs(); | ||
// _mPlayButton = _pref!.getBool(key!) ?? false; | ||
// notifyListeners(); | ||
// } | ||
// | ||
// void _saveToPref() async { | ||
// await _initPrefs(); | ||
// await _pref!.setBool(key!, _mPlayButton!); | ||
// } | ||
// | ||
// void checkButtonState() { | ||
// _mPlayButton = !_mPlayButton!; | ||
// _saveToPref(); | ||
// notifyListeners(); | ||
// } | ||
// } |