Newer
Older
flutterBaseApp / lib / ui / pages / main_left_page.dart
StephanieGitHub on 9 Feb 2021 6 KB first commit
import 'package:base_app/common/common.dart';
import 'package:base_app/data/protocol/user_model.dart';
import 'package:base_app/data/repository/user_repository.dart';
import 'package:base_app/res/index.dart';
import 'package:base_app/utils/login_utils.dart';
import 'package:base_app/utils/util_index.dart';
import 'package:fluintl/fluintl.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';

import 'common/about_page.dart';
import 'common/setting_page.dart';

/// 侧边抽屉
class MainLeftPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _MainLeftPageState();
  }
}

class PageInfo {
  PageInfo(this.titleId, this.iconData, this.page, [this.withScaffold = true]);

  String titleId;
  IconData iconData;
  Widget page;
  bool withScaffold;
}

class _MainLeftPageState extends State<MainLeftPage> {
  List<PageInfo> _pageInfo = new List();
  PageInfo loginOut =
      PageInfo(Ids.titleSignOut, Icons.power_settings_new, null);
  String _userName = '--';

  @override
  void initState() {
    super.initState();
    _pageInfo.add(PageInfo(Ids.titleSetting, Icons.settings, SettingPage()));
    _pageInfo.add(PageInfo(Ids.titleAbout, Icons.info, AboutPage()));
  }

  /// 注销
  void _showLoginOutDialog(BuildContext context) {
    showDialog(
        context: context,
        builder: (ctx) {
          return AlertDialog(
            content: Text(
              IntlUtil.getString(context, Ids.hint_quit),
            ),
            actions: <Widget>[
              FlatButton(
                child: Text(
                  IntlUtil.getString(ctx, Ids.cancel),
                  style: TextStyles.listExtra,
                ),
                onPressed: () {
                  Navigator.pop(ctx);
                },
              ),
              FlatButton(
                child: Text(
                  IntlUtil.getString(ctx, Ids.confirm),
                  style: TextStyles.listExtra,
                ),
                onPressed: () {
                  UserRepository userRepository = new UserRepository();
                  userRepository.logout().then((result) {
                    if (result) {
                      SpUtil.remove(BaseConstant.keyAppToken);
                      // 打开登录页,并将其他路由删除
                      Navigator.of(context).pushNamedAndRemoveUntil(
                          BaseConstant.routeLogin, (route) => false);
                    }
                  }).catchError((onError) {
                    SpUtil.remove(BaseConstant.keyAppToken);
                    Navigator.of(context).pushNamedAndRemoveUntil(
                        BaseConstant.routeLogin, (route) => false);
                  });
                },
              ),
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    // 判断是否已登录,已登录显示用户名和退出
    UserModel userModel =
        SpUtil.getObj(BaseConstant.keyUserModel, (v) => UserModel.fromJson(v));
    if (userModel != null) {
      if (!_pageInfo.contains(loginOut)) {
        _pageInfo.add(loginOut);
        UserModel userModel = SpUtil.getObj(
            BaseConstant.keyUserModel, (v) => UserModel.fromJson(v));
        setState(() {
          _userName = userModel?.name ?? "";
        });
        LogUtil.e("_userName : $_userName");
      }
    } else {
      setState(() {
        _userName = "未登录";
      });
      if (_pageInfo.contains(loginOut)) {
        _pageInfo.remove(loginOut);
      }
    }

    return new Scaffold(
      body: new Column(
        children: <Widget>[
          new Container(
            height: 166.0,
            color: Theme.of(context).primaryColor,
            padding: EdgeInsets.only(
                top: ScreenUtil.getInstance().statusBarHeight, left: 10.0),
            child: new Stack(
              children: <Widget>[
                new Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    new Container(
                      width: 64.0,
                      height: 64.0,
                      margin: EdgeInsets.only(top: 10.0, bottom: 10.0),
                      decoration: BoxDecoration(
                        shape: BoxShape.circle,
                        image: DecorationImage(
                          image: AssetImage(
                            Utils.getImgPath('default_avatar', format: 'jpg'),
                          ),
                        ),
                      ),
                    ),
                    new Text(
                      _userName,
                      style: new TextStyle(
                          color: Colors.white,
                          fontSize: 16.0,
                          fontWeight: FontWeight.bold),
                    ),
                    Gaps.vGap5,
                    new Text(
                      "个人简介",
                      style: new TextStyle(color: Colors.white, fontSize: 12.0),
                    ),
                  ],
                ),
                new Align(
                  alignment: Alignment.topRight,
                  child: new IconButton(
                      iconSize: 18.0,
                      icon: new Icon(Icons.edit, color: Colors.white),
                      onPressed: () {}),
                )
              ],
            ),
          ),
          new Expanded(
            child: new ListView.builder(
                padding: const EdgeInsets.all(0.0),
                itemCount: _pageInfo.length,
                itemBuilder: (BuildContext context, int index) {
                  PageInfo pageInfo = _pageInfo[index];
                  return new ListTile(
                    leading: new Icon(pageInfo.iconData),
                    title: Text(IntlUtil.getString(context, pageInfo.titleId)),
                    onTap: () {
                      if (pageInfo.titleId == Ids.titleSignOut) {
                        _showLoginOutDialog(context);
                      } else {
                        NavigatorUtil.pushPage(context, pageInfo.page,
                            pageName: pageInfo.titleId,
                            needLogin: Utils.isNeedLogin(pageInfo.titleId));
                      }
                    },
                  );
                }),
            flex: 1,
          )
        ],
      ),
    );
  }
}