123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- import 'package:flutter/material.dart';
- import 'dart:async';
- import 'package:shuqi/public.dart';
- import 'code_button.dart';
- class LoginPage extends StatefulWidget {
- @override
- State<StatefulWidget> createState() => LoginPageState();
- }
- class LoginPageState extends State {
- TextEditingController phoneEditer = TextEditingController();
- TextEditingController codeEditer = TextEditingController();
- int coldDownSeconds = 0;
- Timer? timer;
- fetchSmsCode() async {
- if (phoneEditer.text.length == 0) {
- return;
- }
- try {
- await Request.post(
- action: 'sms',
- params: {'phone': phoneEditer.text, 'type': 'login'},
- );
- setState(() {
- coldDownSeconds = 60;
- });
- coldDown();
- } catch (e) {
- Toast.show(e.toString());
- }
- }
- login() async {
- var phone = phoneEditer.text;
- var code = codeEditer.text;
- try {
- var response = await Request.post(action: 'login', params: {
- 'phone': phone,
- 'code': code,
- });
- UserManager.instance.login(response);
- Navigator.pop(context);
- } catch (e) {
- Toast.show(e.toString());
- }
- }
- @override
- void dispose() {
- if (timer != null) {
- timer!.cancel();
- }
- super.dispose();
- }
- coldDown() {
- timer = Timer(Duration(seconds: 1), () {
- setState(() {
- --coldDownSeconds;
- });
- coldDown();
- });
- }
- Widget buildPhone() {
- return Container(
- padding: EdgeInsets.symmetric(horizontal: 8),
- decoration: BoxDecoration(
- color: SQColor.paper,
- borderRadius: BorderRadius.circular(5),
- ),
- child: TextField(
- controller: phoneEditer,
- keyboardType: TextInputType.phone,
- style: TextStyle(fontSize: 14, color: SQColor.darkGray),
- decoration: InputDecoration(
- hintText: '请输入手机号',
- hintStyle: TextStyle(color: SQColor.gray),
- border: InputBorder.none,
- ),
- ),
- );
- }
- Widget buildCode() {
- return Container(
- padding: EdgeInsets.only(left: 8),
- decoration: BoxDecoration(
- color: SQColor.paper,
- borderRadius: BorderRadius.circular(5),
- ),
- child: Row(
- children: <Widget>[
- Flexible(
- child: TextField(
- controller: codeEditer,
- keyboardType: TextInputType.phone,
- style: TextStyle(fontSize: 14, color: SQColor.darkGray),
- decoration: InputDecoration(
- hintText: '请输入短信验证码',
- hintStyle: TextStyle(color: SQColor.gray),
- border: InputBorder.none,
- ),
- ),
- ),
- Container(color: Color(0xffdae3f2), width: 1, height: 40),
- CodeButton(
- onPressed: fetchSmsCode,
- coldDownSeconds: coldDownSeconds,
- ),
- ],
- ),
- );
- }
- Widget buildBody() {
- return Column(
- children: <Widget>[
- SizedBox(height: 20),
- Container(
- margin: EdgeInsets.symmetric(horizontal: 15),
- child: Column(
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: <Widget>[
- buildPhone(),
- SizedBox(height: 10),
- buildCode(),
- SizedBox(height: 10),
- Container(
- decoration: BoxDecoration(
- borderRadius: BorderRadius.circular(5),
- color: SQColor.primary,
- ),
- height: 40,
- child: TextButton(
- onPressed: login,
- child: Text(
- '登录',
- style: TextStyle(fontSize: 16, color: Color(0xffffffff)),
- ),
- ),
- ),
- ],
- ),
- )
- ],
- );
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(title: Text('登录'), elevation: 0),
- backgroundColor: Colors.white,
- body: buildBody(),
- );
- }
- }
|