import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:fun_selfie_app/utils/storage.dart';
import 'package:fun_selfie_app/utils/common_toast.dart';

class DrawerModalPage extends StatefulWidget {
  const DrawerModalPage({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _DrawerModalPageState();
}

class _DrawerModalPageState extends State<DrawerModalPage> {
  late Map userMsgObj = {'configName': '', 'configPass': ''};

  @override
  void initState() {
    super.initState();
    // 初始化的时候开启倒计时
    getUser(context);
  }

  @override
  Widget build(BuildContext context) {
    final screenSize = MediaQuery.of(context).size;
    return Drawer(
        // elevation: 16.0,
        child: Stack(
      children: <Widget>[
        Container(
            height: screenSize.height / 2,
            color: const Color(0xff009EBA),
            padding: const EdgeInsetsDirectional.only(top: 60),
            alignment: Alignment.center,
            child: Column(
              children: <Widget>[
                const ClipOval(
                  child: Image(
                    image: AssetImage('static/images/login_bg.png'),
                    fit: BoxFit.fill,
                    width: 300,
                    height: 300,
                  ),
                ),
                const SizedBox(height: 30),
                Text(
                  '${userMsgObj['configName']}',
                  style: const TextStyle(fontSize: 30, color: Colors.white),
                ),
                Text(
                  '${userMsgObj['configPass']}',
                  style: const TextStyle(fontSize: 16, color: Colors.white),
                ),
              ],
            )),
        Positioned(
            bottom: 20,
            right: 16,
            left: 16,
            child: SizedBox(
                height: 50,
                child: ElevatedButton(
                  child: const Text(
                    "退出",
                    style: TextStyle(fontSize: 18),
                  ),
                  onPressed: () async {
                    Navigator.pop(context);
                    Navigator.pushNamed(context, "/login");
                    CommonToast.okToast('已经退出登录');
                    await StorageUtil.remove('userMsg');
                  },
                )))
      ],
    ));
  }

  void getUser(context) async {
    String userMsg = await StorageUtil.getString('userMsg');
    if (userMsg.isNotEmpty) {
      setState(() {
        userMsgObj = jsonDecode(userMsg);
      });
    } else {
      Navigator.pushNamed(context, "/login");
    }
  }
}
