123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- import 'package:flutter/material.dart';
- import 'package:flutter_osc/model/api.dart';
- import 'package:flutter_osc/model/constants.dart';
- import 'package:flutter_osc/events/LoginEvent.dart';
- import 'package:flutter_osc/model/user_info.dart';
- import 'package:flutter_osc/pages/CommonWebPage.dart';
- import 'package:flutter_osc/pages/login_page.dart';
- import 'package:flutter_osc/util/DataUtils.dart';
- import 'package:flutter_osc/util/NetUtils.dart';
- import 'dart:convert';
- import 'package:shared_preferences/shared_preferences.dart';
- class MyInfoPage extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- return MyInfoPageState();
- }
- }
- class MyInfoPageState extends State<MyInfoPage> {
- static const double IMAGE_ICON_WIDTH = 30.0;
- static const double ARROW_ICON_WIDTH = 16.0;
- var titles = ["我的消息", "阅读记录", "我的博客", "我的问答", "我的活动", "我的团队", "邀请好友"];
- var imagePaths = [
- "images/ic_my_message.png",
- "images/ic_my_blog.png",
- "images/ic_my_blog.png",
- "images/ic_my_question.png",
- "images/ic_discover_pos.png",
- "images/ic_my_team.png",
- "images/ic_my_recommend.png"
- ];
- var icons = [];
- var userAvatar;
- var userName;
- var titleTextStyle = TextStyle(fontSize: 16.0);
- var rightArrowIcon = Image.asset(
- 'images/ic_arrow_right.png',
- width: ARROW_ICON_WIDTH,
- height: ARROW_ICON_WIDTH,
- );
- MyInfoPageState() {
- for (int i = 0; i < imagePaths.length; i++) {
- icons.add(getIconImage(imagePaths[i]));
- }
- }
- @override
- void initState() {
- super.initState();
- _showUserInfo();
- Constants.eventBus.on().listen((event) {
- // 收到退出登录的消息,刷新个人信息显示
- _showUserInfo();
- });
- Constants.eventBus.on().listen((event) {
- // 收到登录的消息,重新获取个人信息
- getUserInfo();
- });
- }
- _showUserInfo() {
- DataUtils.getUserInfo().then((UserInfo? userInfo) {
- if (userInfo != null) {
- print(userInfo.name);
- print(userInfo.avatar);
- setState(() {
- userAvatar = userInfo.avatar;
- userName = userInfo.name;
- });
- } else {
- setState(() {
- userAvatar = null;
- userName = null;
- });
- }
- });
- }
- Widget getIconImage(path) {
- return Padding(
- padding: const EdgeInsets.fromLTRB(0.0, 0.0, 10.0, 0.0),
- child:
- Image.asset(path, width: IMAGE_ICON_WIDTH, height: IMAGE_ICON_WIDTH),
- );
- }
- @override
- Widget build(BuildContext context) {
- var listView = ListView.builder(
- itemCount: titles.length * 2,
- itemBuilder: (context, i) => renderRow(i),
- );
- return listView;
- }
- // 获取用户信息
- getUserInfo() async {
- SharedPreferences sp = await SharedPreferences.getInstance();
- String? accessToken = sp.get(DataUtils.SP_AC_TOKEN) as String?;
- Map<String, String?> params = Map();
- params['access_token'] = accessToken;
- NetUtils.get(Api.USER_INFO, params: params).then((data) {
- if (data != null) {
- var map = json.decode(data);
- setState(() {
- userAvatar = map['avatar'];
- userName = map['name'];
- });
- DataUtils.saveUserInfo(map);
- }
- });
- }
- _login() async {
- final result =
- await Navigator.of(context).push(MaterialPageRoute(builder: (context) {
- return LoginPage();
- }));
- if (result != null && result == "refresh") {
- // 刷新用户信息
- getUserInfo();
- // 通知动弹页面刷新
- Constants.eventBus.fire(LoginEvent());
- }
- }
- _showUserInfoDetail() {}
- renderRow(i) {
- if (i == 0) {
- var avatarContainer = Container(
- color: const Color(0xff63ca6c),
- height: 200.0,
- child: Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- userAvatar == null
- ? Image.asset(
- "images/ic_avatar_default.png",
- width: 60.0,
- )
- : Container(
- width: 60.0,
- height: 60.0,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- color: Colors.transparent,
- image: DecorationImage(
- image: NetworkImage(userAvatar), fit: BoxFit.cover),
- border: Border.all(
- color: Colors.white,
- width: 2.0,
- ),
- ),
- ),
- Text(
- userName == null ? "点击头像登录" : userName,
- style: TextStyle(color: Colors.white, fontSize: 16.0),
- ),
- ],
- ),
- ),
- );
- return GestureDetector(
- onTap: () {
- DataUtils.isLogin().then((isLogin) {
- if (isLogin) {
- // 已登录,显示用户详细信息
- _showUserInfoDetail();
- } else {
- // 未登录,跳转到登录页面
- _login();
- }
- });
- },
- child: avatarContainer,
- );
- }
- --i;
- if (i.isOdd) {
- return Divider(
- height: 1.0,
- );
- }
- i = i ~/ 2;
- String title = titles[i];
- var listItemContent = Padding(
- padding: const EdgeInsets.fromLTRB(10.0, 15.0, 10.0, 15.0),
- child: Row(
- children: [
- icons[i],
- Expanded(
- child: Text(
- title,
- style: titleTextStyle,
- )),
- rightArrowIcon
- ],
- ),
- );
- return InkWell(
- child: listItemContent,
- onTap: () {
- _handleListItemClick(title);
- // Navigator
- // .of(context)
- // .push(MaterialPageRoute(builder: (context) => CommonWebPage(title: "Test", url: "https://my.oschina.net/u/815261/blog")));
- },
- );
- }
- _showLoginDialog() {
- showDialog(
- context: context,
- builder: (BuildContext ctx) {
- return AlertDialog(
- title: Text('提示'),
- content: Text('没有登录,现在去登录吗?'),
- actions: <Widget>[
- TextButton(
- child: Text(
- '取消',
- style: TextStyle(color: Colors.red),
- ),
- onPressed: () {
- Navigator.of(context).pop();
- },
- ),
- TextButton(
- child: Text(
- '确定',
- style: TextStyle(color: Colors.blue),
- ),
- onPressed: () {
- Navigator.of(context).pop();
- _login();
- },
- )
- ],
- );
- });
- }
- _handleListItemClick(String title) {
- DataUtils.isLogin().then((isLogin) {
- if (!isLogin) {
- // 未登录
- _showLoginDialog();
- } else {
- DataUtils.getUserInfo().then((info) {
- Navigator.of(context).push(MaterialPageRoute(
- builder: (context) => CommonWebPage(
- title: "我的博客",
- url: "https://my.oschina.net/u/${info!.id}/blog")));
- });
- }
- });
- }
- }
|