123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- import 'dart:convert';
- 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/pages/login_page.dart';
- import 'package:flutter_osc/util/BlackListUtils.dart';
- import 'package:flutter_osc/util/DataUtils.dart';
- import 'package:flutter_osc/util/NetUtils.dart';
- import 'package:flutter_osc/util/Utf8Utils.dart';
- import 'package:shared_preferences/shared_preferences.dart';
- class BlackHousePage extends StatefulWidget {
- @override
- State<StatefulWidget> createState() {
- return BlackHousePageState();
- }
- }
- class BlackHousePageState extends State<BlackHousePage> {
- bool isLogin = true;
- List? blackDataList;
- TextStyle btnStyle = TextStyle(color: Colors.white, fontSize: 12.0);
- BlackHousePageState() {
- queryBlackList();
- }
- queryBlackList() {
- DataUtils.getUserInfo().then((userInfo) {
- if (userInfo != null) {
- String url = Api.QUERY_BLACK;
- url += "/${userInfo.id}";
- NetUtils.get(url).then((data) {
- if (data != null) {
- var obj = json.decode(data);
- if (obj['code'] == 0) {
- setState(() {
- blackDataList = obj['msg'];
- });
- }
- }
- });
- } else {
- setState(() {
- isLogin = false;
- });
- }
- });
- }
- // 获取用户信息
- 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);
- DataUtils.saveUserInfo(map).then((userInfo) {
- queryBlackList();
- });
- }
- });
- }
- // 从黑名单中删除
- deleteFromBlack(authorId) {
- DataUtils.getUserInfo().then((userInfo) {
- if (userInfo != null) {
- String userId = "${userInfo.id}";
- Map<String, String> params = Map();
- params['userid'] = userId;
- params['authorid'] = "$authorId";
- NetUtils.get(Api.DELETE_BLACK, params: params).then((data) {
- Navigator.of(context).pop();
- if (data != null) {
- var obj = json.decode(data);
- if (obj['code'] == 0) {
- // 删除成功
- BlackListUtils.removeBlackId(authorId);
- queryBlackList();
- } else {
- showResultDialog("操作失败:${obj['msg']}");
- }
- }
- }).catchError((e) {
- Navigator.of(context).pop();
- showResultDialog("网络请求失败:$e");
- });
- }
- });
- }
- showResultDialog(String msg) {
- showDialog(
- context: context,
- builder: (ctx) {
- return AlertDialog(
- title: Text('提示'),
- content: Text(msg),
- actions: <Widget>[
- TextButton(
- child: Text(
- '确定',
- style: TextStyle(color: Colors.red),
- ),
- onPressed: () {
- Navigator.of(context).pop();
- },
- )
- ],
- );
- });
- }
- showSetFreeDialog(item) {
- String? name = Utf8Utils.decode(item['authorname']);
- showDialog(
- context: context,
- builder: (BuildContext ctx) {
- return AlertDialog(
- title: Text('提示'),
- content: Text('确定要把\"$name\"放出小黑屋吗?'),
- actions: <Widget>[
- TextButton(
- child: Text(
- '确定',
- style: TextStyle(color: Colors.red),
- ),
- onPressed: () {
- deleteFromBlack(item['authorid']);
- },
- )
- ],
- );
- });
- }
- Widget getBody() {
- if (!isLogin) {
- return Center(
- child: InkWell(
- child: Container(
- padding: const EdgeInsets.fromLTRB(15.0, 8.0, 15.0, 8.0),
- child: Text("去登录"),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.black),
- borderRadius: BorderRadius.all(Radius.circular(5.0))),
- ),
- onTap: () async {
- final result = await Navigator.of(context)
- .push(MaterialPageRoute(builder: (BuildContext context) {
- return LoginPage();
- }));
- if (result != null && result == "refresh") {
- // 通知动弹页面刷新
- Constants.eventBus.fire(LoginEvent());
- getUserInfo();
- }
- },
- ),
- );
- }
- if (blackDataList == null) {
- return Center(
- child: CircularProgressIndicator(),
- );
- } else if (blackDataList!.length == 0) {
- return Center(
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [Text("小黑屋中没人..."), Text("长按动弹列表即可往小黑屋中加人")],
- ),
- );
- }
- return GridView.count(
- crossAxisCount: 3,
- children: List.generate(blackDataList!.length, (index) {
- String name = Utf8Utils.decode(blackDataList![index]['authorname'])!;
- return Container(
- margin: const EdgeInsets.all(2.0),
- color: Colors.black,
- child: Column(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Container(
- width: 45.0,
- height: 45.0,
- decoration: BoxDecoration(
- shape: BoxShape.circle,
- color: Colors.transparent,
- image: DecorationImage(
- image: NetworkImage(
- "${blackDataList![index]['authoravatar']}"),
- fit: BoxFit.cover),
- border: Border.all(
- color: Colors.white,
- width: 2.0,
- ),
- ),
- ),
- Container(
- margin: const EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
- child: Text(name, style: TextStyle(color: Colors.white)),
- ),
- InkWell(
- child: Container(
- padding: const EdgeInsets.fromLTRB(8.0, 5.0, 5.0, 8.0),
- child: Text(
- "放我出去",
- style: btnStyle,
- ),
- decoration: BoxDecoration(
- border: Border.all(color: Colors.white),
- borderRadius: BorderRadius.all(Radius.circular(5.0))),
- ),
- onTap: () {
- showSetFreeDialog(blackDataList![index]);
- },
- ),
- ],
- ),
- );
- }),
- );
- }
- @override
- Widget build(BuildContext context) {
- return Scaffold(
- appBar: AppBar(
- title: Text("动弹小黑屋", style: TextStyle(color: Colors.white)),
- iconTheme: IconThemeData(color: Colors.white),
- ),
- body: Padding(
- padding: const EdgeInsets.fromLTRB(2.0, 4.0, 2.0, 0.0),
- child: getBody(),
- ),
- );
- }
- }
|