반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- code editor
- Prism.js
- babel
- methodChannel
- uint8array
- Image Resize typescript
- web track
- typescript
- REST API
- jszip
- react
- Three js
- KakaoMap
- Completer
- Excel
- FirebaseAnalytics
- identifierForVender
- swagger-typescript-api
- Redux
- userevent_tracker
- node
- Three-fiber
- Flutter
- Game js
- uint16array
- webrtc
- Raycasting
- RouteObserver
- androidId
- Babel standalone
Archives
- Today
- Total
Never give up
Flutter - SharedPreferences set and get Color 본문
반응형
Color값을 SharedPreferences에 저장하는것은 아주 간단하지만
이전에 설정된 값이 없어서 null인 경우에 처리하는 방법입니다
Splash Screen, init메소드
@override
void initState() {
super.initState();
_init();
}
void _init() async {
await Provider.of<ConfigData>(context, listen: false).initColorData();
Navigator.push(
context, MaterialPageRoute(builder: (context) => MainScreen()));
}
ConfigData
class ConfigData extends ChangeNotifier {
SharedPreferences _preferences;
Color _color;
Future<void> initColorData() async {
_preferences = await SharedPreferences.getInstance();
_color = Color(_preferences.getInt('color') ?? Colors.blue.value);
}
Color get getColor => _color;
void setColor(Color value) async {
_color = value;
notifyListeners();
await _preferences.setInt('color', _color.value);
}
}
메인
class _SharedPreferencesExampleState extends State<SharedPreferencesExample> {
@override
Widget build(BuildContext context) {
var item = Provider.of<ConfigData>(context);
Color _color;
return Scaffold(
appBar: AppBar(title: Text('SharedPreferences')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: MediaQuery.of(context).size.height * 0.25,
width: MediaQuery.of(context).size.width * 0.5,
color: item.getColor,
),
SizedBox(height: 10),
RaisedButton(
child: Text('Change color'),
onPressed: () => showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Pick Color',
style: TextStyle(
fontSize: 24, fontWeight: FontWeight.bold)),
content: MaterialColorPicker(
shrinkWrap: true,
selectedColor: item.getColor,
onColorChange: (color) {
_color = color;
},
),
actions: <Widget>[
FlatButton(
child: Text('Cancel'),
onPressed: () => Navigator.pop(context)),
FlatButton(
child: Text('Confirm'),
onPressed: () {
if (_color == null) {
_color = item.getColor;
}
item.setColor(_color);
Navigator.pop(context);
}),
],
)))
],
),
),
);
}
}
int나 String같은 값을 가져올때에는 preferences.getInt() ?? 0, preferences.getString() ?? '0'
이런식으로 default값을 처리해주면 되지만
다른 데이터 타입을 갖는경우 ex) Color, DateTime, etc
본문과 같이 처리를 해줘야됩니다
반응형
'Flutter' 카테고리의 다른 글
Flutter - When you store data in SQFLlite (0) | 2020.08.03 |
---|---|
Flutter - How to solve Error: No pubspec.yaml file found. (8) | 2020.08.03 |
Flutter - Provider listen : false with http (0) | 2020.08.03 |
Flutter - addPostFrameCallback (0) | 2020.08.03 |
Flutter - StatefulWidget Lifecycle (0) | 2020.08.03 |
Comments