319 lines
10 KiB
Dart
319 lines
10 KiB
Dart
|
|
import 'package:countdown/countdown.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/rendering.dart';
|
|
import 'package:fluttertoast/fluttertoast.dart';
|
|
|
|
import '../../constants.dart';
|
|
import '../../generated/l10n.dart';
|
|
import '../../routes.dart';
|
|
import '../../utils/http_util.dart';
|
|
import '../../utils/utils.dart';
|
|
|
|
class MobileNewUser extends StatefulWidget {
|
|
const MobileNewUser({Key key}) : super(key: key);
|
|
|
|
@override
|
|
State<StatefulWidget> createState() {
|
|
return MobileNewUserState();
|
|
}
|
|
|
|
}
|
|
|
|
class MobileNewUserState extends State<MobileNewUser> {
|
|
|
|
final GlobalKey<FormState> _formKey = GlobalKey();
|
|
|
|
final usernameController = TextEditingController();
|
|
bool usernameEnable = true;
|
|
final codeController = TextEditingController();
|
|
|
|
bool enableGetCode;
|
|
String getCodeText;
|
|
bool canRegister;
|
|
|
|
var countDownListener;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListView(
|
|
children: <Widget>[
|
|
Form(
|
|
key: _formKey,
|
|
child: Container(
|
|
padding: EdgeInsets.all(16.0),
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.start,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: <Widget>[
|
|
Container(
|
|
child: Text(
|
|
S.of(context).user_registration,
|
|
style: TextStyle(
|
|
fontSize: 24.0,
|
|
color: Colors.black
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
padding: EdgeInsets.only(top: 8.0, bottom: 16.0),
|
|
child: Text(
|
|
S.of(context).user_registration_desc,
|
|
style: TextStyle(
|
|
color: Colors.black38,
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.only(top: 10.0),
|
|
child: TextFormField(
|
|
controller: usernameController,
|
|
enabled: usernameEnable,
|
|
keyboardType: TextInputType.emailAddress,
|
|
decoration: new InputDecoration(
|
|
enabledBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(
|
|
color: Colors.black12,
|
|
),
|
|
),
|
|
focusedBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(
|
|
color: Colors.blue,
|
|
),
|
|
),
|
|
labelText: S.of(context).mobile_or_email,
|
|
),
|
|
style: TextStyle(
|
|
fontSize: 18.0
|
|
),
|
|
autofocus: true,
|
|
validator: (String value) {
|
|
if (value.trim().isEmpty) {
|
|
return S.of(context).mobile_or_email_is_required;
|
|
}
|
|
return null;
|
|
},
|
|
onChanged: (string) {
|
|
if (string.isEmpty) {
|
|
if (mounted) {
|
|
setState(() {
|
|
canRegister = false;
|
|
});
|
|
}
|
|
} else if (string.isNotEmpty && codeController.text.trim().isNotEmpty) {
|
|
if (mounted) {
|
|
setState(() {
|
|
canRegister = true;
|
|
});
|
|
}
|
|
}
|
|
if (string.isNotEmpty && !enableGetCode) {
|
|
if (mounted) {
|
|
setState(() {
|
|
enableGetCode = true;
|
|
});
|
|
}
|
|
} else if (string.isEmpty && enableGetCode) {
|
|
if (mounted) {
|
|
setState(() {
|
|
enableGetCode = false;
|
|
});
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
Container(
|
|
child: TextFormField(
|
|
controller: codeController,
|
|
keyboardType: TextInputType.number,
|
|
decoration: new InputDecoration(
|
|
enabledBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(
|
|
color: Colors.black12,
|
|
),
|
|
),
|
|
focusedBorder: UnderlineInputBorder(
|
|
borderSide: BorderSide(
|
|
color: Colors.blue,
|
|
),
|
|
),
|
|
labelText: S.of(context).verification_code,
|
|
suffixIcon: MouseRegion(
|
|
cursor: SystemMouseCursors.click,
|
|
child: GestureDetector(
|
|
child: Container(
|
|
margin: EdgeInsets.only(top: 6.0),
|
|
child: Container(
|
|
padding: EdgeInsets.only(left: 10.0, right: 10.0, top: 10.0, bottom: 10.0),
|
|
decoration: BoxDecoration(
|
|
shape: BoxShape.rectangle,
|
|
border: new Border.all(
|
|
color: enableGetCode ? Colors.black87 : Colors.black26,
|
|
width: 1.0,
|
|
),
|
|
borderRadius: BorderRadius.all(Radius.circular(10.0)),
|
|
),
|
|
child: Text(
|
|
getCodeText,
|
|
style: TextStyle(
|
|
color: enableGetCode ? Colors.black87 : Colors.black26,
|
|
fontSize: 14.0
|
|
),
|
|
),
|
|
),
|
|
),
|
|
onTap: enableGetCode ? getCodeTapped : null,
|
|
),
|
|
),
|
|
),
|
|
style: TextStyle(
|
|
fontSize: 18.0
|
|
),
|
|
validator: (String value) {
|
|
if (value.trim().isEmpty) {
|
|
return S.of(context).verification_code_is_required;
|
|
}
|
|
return null;
|
|
},
|
|
onChanged: (string) {
|
|
if (usernameController.text.trim().isNotEmpty && string.isNotEmpty) {
|
|
if (mounted) {
|
|
setState(() {
|
|
canRegister = true;
|
|
});
|
|
}
|
|
} else {
|
|
if (mounted) {
|
|
setState(() {
|
|
canRegister = false;
|
|
});
|
|
}
|
|
}
|
|
},
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
Container(
|
|
margin: EdgeInsets.only(top: 0.0, right: 16.0),
|
|
child: Align(
|
|
alignment: Alignment.centerRight,
|
|
child: RaisedButton(
|
|
color: Theme.of(context).primaryColor,
|
|
child: Text(
|
|
S.of(context).register,
|
|
style: TextStyle(
|
|
color: Colors.white,
|
|
),
|
|
),
|
|
onPressed: canRegister ? register : null,
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
setState(() {
|
|
enableGetCode = false;
|
|
canRegister = false;
|
|
usernameEnable = true;
|
|
});
|
|
}
|
|
|
|
@override
|
|
void didChangeDependencies() {
|
|
super.didChangeDependencies();
|
|
getCodeText = S.of(context).get_code;
|
|
}
|
|
|
|
void register() {
|
|
final FormState form = _formKey.currentState;
|
|
if (form.validate()) {
|
|
HttpUtil.httpPost('v1/users', (response) {
|
|
Routes.router.navigateTo(context, '/set-password/${usernameController.text.trim()}/${codeController.text.trim()}', replace: true);
|
|
},
|
|
queryParameters: {
|
|
'action': 'verify_code'
|
|
},
|
|
isFormData: true,
|
|
body: {
|
|
'mobile': usernameController.text.trim(),
|
|
'code': codeController.text.trim(),
|
|
'store_id': Constants.BUSINESS_ID
|
|
},
|
|
).catchError((error) {
|
|
Utils.showMessageDialog(context, error);
|
|
});
|
|
}
|
|
}
|
|
|
|
void getCodeTapped() {
|
|
if (usernameController.text.isNotEmpty) {
|
|
HttpUtil.httpPost('v1/users', (response) {
|
|
Fluttertoast.showToast(
|
|
msg: S.of(context).verification_code_sent,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.CENTER,
|
|
backgroundColor: Colors.black54,
|
|
textColor: Colors.white
|
|
);
|
|
countDownListener = CountDown(Duration(seconds: 90)).stream.listen(null);
|
|
startCountDown();
|
|
if (mounted) {
|
|
setState(() {
|
|
usernameEnable = false;
|
|
});
|
|
}
|
|
},
|
|
queryParameters: {'action': 'send_code'},
|
|
body: {
|
|
'mobile': usernameController.text,
|
|
},
|
|
isFormData: true,
|
|
).catchError((error) {
|
|
if (mounted) {
|
|
setState(() {
|
|
canRegister = false;
|
|
});
|
|
}
|
|
Utils.showMessageDialog(context, error);
|
|
});
|
|
} else {
|
|
Fluttertoast.showToast(
|
|
msg: S.of(context).enter_mobile_or_email,
|
|
toastLength: Toast.LENGTH_SHORT,
|
|
gravity: ToastGravity.CENTER,
|
|
backgroundColor: Colors.red,
|
|
textColor: Colors.white
|
|
);
|
|
}
|
|
}
|
|
|
|
void startCountDown() {
|
|
countDownListener.onData((Duration d) {
|
|
if (mounted) {
|
|
setState(() {
|
|
enableGetCode = false;
|
|
getCodeText = S.of(context).get_code_token(d.inSeconds);
|
|
});
|
|
}
|
|
});
|
|
|
|
countDownListener.onDone(() {
|
|
if (mounted) {
|
|
setState(() {
|
|
enableGetCode = true;
|
|
getCodeText = S.of(context).get_code_again;
|
|
});
|
|
}
|
|
countDownListener = CountDown(Duration(seconds: 90)).stream.listen(null);
|
|
});
|
|
}
|
|
} |