Skip to content

Commit fd11526

Browse files
committed
settings change username
1 parent 1bcc4ef commit fd11526

File tree

5 files changed

+207
-20
lines changed

5 files changed

+207
-20
lines changed

lib/db/database_provider.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ class DatabaseProvider {
8686
return index > 0;
8787
}
8888

89+
Future<bool> updateUser(User user) async {
90+
final db = await database;
91+
int change = await db
92+
.update('user', user.toJson(), where: 'id = ?', whereArgs: [user.id]);
93+
return change > 0;
94+
}
95+
8996
Future<List<Habit>> getAllHabits() async {
9097
final db = await database;
9198
var habits = await db.query('habits');

lib/mine/mine_screen.dart

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
import 'package:flutter/cupertino.dart';
12
import 'package:flutter/material.dart';
23
import 'package:flutter_bloc/flutter_bloc.dart';
34
import 'package:flutter_svg/svg.dart';
45
import 'package:timefly/app_theme.dart';
56
import 'package:timefly/blocs/theme/theme_bloc.dart';
67
import 'package:timefly/blocs/theme/theme_state.dart';
78
import 'package:timefly/mine/mine_screen_views.dart';
9+
import 'package:timefly/mine/settings_screen.dart';
810
import 'package:timefly/utils/system_util.dart';
911

1012
class MineScreen extends StatefulWidget {
@@ -30,26 +32,34 @@ class _MineScreenState extends State<MineScreen> {
3032
height: 100,
3133
)
3234
]),
33-
Container(
34-
alignment: Alignment.centerRight,
35-
margin:
36-
EdgeInsets.only(top: MediaQuery.of(context).padding.top + 26),
37-
height: 45,
35+
GestureDetector(
36+
onTap: () {
37+
Navigator.of(context)
38+
.push(CupertinoPageRoute(builder: (context) {
39+
return SettingsScreen();
40+
}));
41+
},
3842
child: Container(
39-
alignment: Alignment.center,
40-
width: 90,
41-
decoration: BoxDecoration(
42-
shape: BoxShape.rectangle,
43-
borderRadius: BorderRadius.only(
44-
topLeft: Radius.circular(26),
45-
bottomLeft: Radius.circular(26)),
46-
color: AppTheme.appTheme.cardBackgroundColor(),
47-
boxShadow: AppTheme.appTheme.containerBoxShadow()),
48-
child: SvgPicture.asset(
49-
'assets/images/icon_jiaohuan.svg',
50-
width: 25,
51-
height: 25,
52-
color: AppTheme.appTheme.normalColor().withOpacity(0.8),
43+
alignment: Alignment.centerRight,
44+
margin: EdgeInsets.only(
45+
top: MediaQuery.of(context).padding.top + 26),
46+
height: 45,
47+
child: Container(
48+
alignment: Alignment.center,
49+
width: 90,
50+
decoration: BoxDecoration(
51+
shape: BoxShape.rectangle,
52+
borderRadius: BorderRadius.only(
53+
topLeft: Radius.circular(26),
54+
bottomLeft: Radius.circular(26)),
55+
color: AppTheme.appTheme.cardBackgroundColor(),
56+
boxShadow: AppTheme.appTheme.containerBoxShadow()),
57+
child: SvgPicture.asset(
58+
'assets/images/icon_jiaohuan.svg',
59+
width: 25,
60+
height: 25,
61+
color: AppTheme.appTheme.normalColor().withOpacity(0.8),
62+
),
5363
),
5464
),
5565
)

lib/mine/settings_screen.dart

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
import 'package:data_plugin/bmob/table/bmob_user.dart';
2+
import 'package:flutter/material.dart';
3+
import 'package:flutter/services.dart';
4+
import 'package:flutter_bloc/flutter_bloc.dart';
5+
import 'package:flutter_svg/svg.dart';
6+
import 'package:timefly/blocs/theme/theme_bloc.dart';
7+
import 'package:timefly/blocs/theme/theme_state.dart';
8+
import 'package:timefly/models/user.dart';
9+
import 'package:timefly/utils/system_util.dart';
10+
import 'package:timefly/widget/custom_edit_field.dart';
11+
12+
import '../app_theme.dart';
13+
14+
class SettingsScreen extends StatefulWidget {
15+
@override
16+
_SettingsScreenState createState() => _SettingsScreenState();
17+
}
18+
19+
class _SettingsScreenState extends State<SettingsScreen> {
20+
@override
21+
Widget build(BuildContext context) {
22+
return BlocBuilder<ThemeBloc, ThemeState>(
23+
builder: (context, state) {
24+
SystemUtil.changeStateBarMode(
25+
AppTheme.appTheme.isDark() ? Brightness.light : Brightness.dark);
26+
return Container(
27+
color: AppTheme.appTheme.containerBackgroundColor(),
28+
child: Stack(
29+
children: [
30+
ListView(physics: ClampingScrollPhysics(), children: [
31+
Container(
32+
margin: EdgeInsets.only(
33+
top: MediaQuery.of(context).padding.top, right: 24),
34+
alignment: Alignment.topRight,
35+
child: ClipRRect(
36+
borderRadius: BorderRadius.all(Radius.circular(16)),
37+
child: Image.network(
38+
'https://c-ssl.duitang.com/uploads/item/201903/06/20190306091021_kzhxr.jpg',
39+
width: 130,
40+
height: 130,
41+
fit: BoxFit.cover,
42+
),
43+
),
44+
),
45+
ChangeUserInfoView(),
46+
SizedBox(
47+
height: 100,
48+
)
49+
]),
50+
GestureDetector(
51+
onTap: () {
52+
Navigator.of(context).pop();
53+
},
54+
child: Container(
55+
alignment: Alignment.centerLeft,
56+
margin: EdgeInsets.only(
57+
top: MediaQuery.of(context).padding.top + 26),
58+
height: 45,
59+
child: Container(
60+
alignment: Alignment.center,
61+
width: 90,
62+
decoration: BoxDecoration(
63+
shape: BoxShape.rectangle,
64+
borderRadius: BorderRadius.only(
65+
topRight: Radius.circular(26),
66+
bottomRight: Radius.circular(26)),
67+
color: AppTheme.appTheme.cardBackgroundColor(),
68+
boxShadow: AppTheme.appTheme.containerBoxShadow()),
69+
child: SvgPicture.asset(
70+
'assets/images/fanhui.svg',
71+
width: 25,
72+
height: 25,
73+
color: AppTheme.appTheme.normalColor().withOpacity(0.8),
74+
),
75+
),
76+
),
77+
)
78+
],
79+
),
80+
);
81+
},
82+
);
83+
}
84+
}
85+
86+
class ChangeUserInfoView extends StatefulWidget {
87+
@override
88+
State<StatefulWidget> createState() {
89+
return _ChangeUserInfoViewState();
90+
}
91+
}
92+
93+
class _ChangeUserInfoViewState extends State<ChangeUserInfoView> {
94+
String userName;
95+
96+
@override
97+
void initState() {
98+
userName = SessionUtils.sharedInstance().currentUser.username;
99+
super.initState();
100+
}
101+
102+
@override
103+
Widget build(BuildContext context) {
104+
return Container(
105+
margin: EdgeInsets.only(top: 32, left: 22, right: 22),
106+
padding: EdgeInsets.only(top: 16, bottom: 16),
107+
decoration: BoxDecoration(
108+
shape: BoxShape.rectangle,
109+
borderRadius: BorderRadius.all(Radius.circular(15)),
110+
color: AppTheme.appTheme.cardBackgroundColor(),
111+
boxShadow: AppTheme.appTheme.containerBoxShadow()),
112+
child: Column(
113+
children: [
114+
CustomEditField(
115+
maxLength: 10,
116+
autoFucus: false,
117+
inputType: TextInputType.name,
118+
initValue: userName,
119+
hintText: '名字',
120+
hintTextStyle: AppTheme.appTheme
121+
.hint(fontWeight: FontWeight.normal, fontSize: 16),
122+
textStyle: AppTheme.appTheme
123+
.headline1(fontWeight: FontWeight.normal, fontSize: 16),
124+
containerDecoration: BoxDecoration(
125+
shape: BoxShape.rectangle,
126+
borderRadius: BorderRadius.all(Radius.circular(15)),
127+
color: AppTheme.appTheme.containerBackgroundColor()),
128+
numDecoration: BoxDecoration(
129+
shape: BoxShape.rectangle,
130+
color: AppTheme.appTheme.cardBackgroundColor(),
131+
borderRadius: BorderRadius.all(Radius.circular(15)),
132+
boxShadow: AppTheme.appTheme.containerBoxShadow()),
133+
numTextStyle: AppTheme.appTheme
134+
.themeText(fontWeight: FontWeight.bold, fontSize: 15),
135+
onValueChanged: (value) {
136+
userName = value;
137+
},
138+
onCompleted: () async {
139+
FocusScope.of(context).requestFocus(FocusNode());
140+
User user = SessionUtils.sharedInstance().currentUser;
141+
BmobUser bmobUser = BmobUser();
142+
bmobUser.objectId = user.id;
143+
bmobUser.mobilePhoneNumber = user.phone;
144+
bmobUser.mobilePhoneNumberVerified = true;
145+
bmobUser.username = userName;
146+
try {
147+
await bmobUser.update();
148+
SessionUtils.sharedInstance().updateName(userName);
149+
} catch (e) {
150+
print(e);
151+
}
152+
},
153+
)
154+
],
155+
),
156+
);
157+
}
158+
}

lib/models/user.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ class User {
2121
data["phone"] = phone;
2222
return data;
2323
}
24+
25+
User copyWith({String id, String username, String phone}) {
26+
return User(id ?? this.id, username ?? this.username, phone ?? this.phone);
27+
}
2428
}
2529

2630
class SessionUtils {
@@ -61,6 +65,11 @@ class SessionUtils {
6165
habitsBloc.add(HabitsLoad());
6266
}
6367

68+
void updateName(String name) async {
69+
currentUser = currentUser.copyWith(username: name);
70+
await DatabaseProvider.db.updateUser(currentUser);
71+
}
72+
6473
bool isLogin() {
6574
return currentUser != null;
6675
}

lib/widget/custom_edit_field.dart

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CustomEditField extends StatefulWidget {
1919

2020
final String initValue;
2121
final ValueChanged<String> onValueChanged;
22+
final VoidCallback onCompleted;
2223

2324
const CustomEditField(
2425
{Key key,
@@ -34,7 +35,8 @@ class CustomEditField extends StatefulWidget {
3435
this.numTextStyle,
3536
this.minHeight,
3637
this.autoFucus,
37-
this.inputType})
38+
this.inputType,
39+
this.onCompleted})
3840
: super(key: key);
3941

4042
@override
@@ -118,6 +120,7 @@ class _CustomEditFieldState extends State<CustomEditField>
118120
numAnimationController.reverse(from: 0.3);
119121
}
120122
},
123+
onEditingComplete: widget.onCompleted ?? () {},
121124
),
122125
),
123126
ScaleTransition(

0 commit comments

Comments
 (0)