-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsharedpreference.dart
77 lines (70 loc) · 2.44 KB
/
sharedpreference.dart
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//Make sure you added in pubspec.yaml
// shared_preferences:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
final _textController1 = TextEditingController();
final _textController2 = TextEditingController();
final _nameController = TextEditingController();
final _phoneController = TextEditingController();
_saveValues() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString("name", _textController1.text);
prefs.setString("phone", _textController2.text);
}
getSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
_nameController.text = prefs.getString("name");
_phoneController.text = prefs.getString("phone");
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Shared Preference demo',
home: Scaffold(
appBar: AppBar(
title: Text('Flutter Shared Preference demo'),
),
body: Center(
child: Container(
padding: EdgeInsets.fromLTRB(20, 30, 20, 10),
child: Column(
children: <Widget>[
Text('To Save', style: TextStyle(fontSize: 20),),
Text('Name'),
TextField(controller: _textController1,),
Text('Phone'),
TextField(controller: _textController2,),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('Save Value'),
onPressed: () {
_saveValues();
},
),
SizedBox(width: 10,),
RaisedButton(
child: Text('Show Value'),
onPressed: () {
getSharedPreferences();
},
),
],
),
SizedBox(height: 100,),
Text('To Show', style: TextStyle(fontSize: 20),),
Text('Name:'),
TextField(controller: _nameController,),
Text('Phone'),
TextField(controller: _phoneController,),
],
),
),
),
),
);
}
}