import 'dart:convert';
import 'dart:ui';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'data/GetPullBlackList.dart';
/*
* 拉黑列表
*/
List<String> userId = ["0"];
List nickName = [0];
List avatar = [0];
//取屏幕最大宽度和高度
final width = window.physicalSize.width;
final height = window.physicalSize.height;
class PullBlackList extends StatefulWidget {
const PullBlackList({key}) : super(key: key);
@override
//状态
PullBlackListState createState() => PullBlackListState();
}
class PullBlackListState extends State<PullBlackList> {
TextEditingController phoneController = TextEditingController();
@override
void initState() {
// TODO: implement initState
getPullBlackList();
super.initState();
}
@override
void dispose() {
//销毁
super.dispose();
}
@override
void disposeClear() {
userId.clear();
super.dispose();
}
//获取拉黑列表接口
getPullBlackList() async {
var apiUrl = "http://47.242.63.216:9527/v1/blacklist/getList";
SharedPreferences prefs = await SharedPreferences.getInstance();
var tokens = prefs.getString("token");
//参数
Map map = {};
//网络请求添加token请求头
Response result = await Dio().post(apiUrl,
data: map, options: Options(headers: {"x-token": tokens}));
debugPrint("${result}");
//json解析
Map<String, dynamic> nickname = json.decode(result.toString());
var httpRes = GetPullBlackList.fromJson(nickname);
print("${httpRes.msg}和${httpRes.data.list1[0].userId}");
//如果成功就吐司
if (httpRes.code == 200) {
Fluttertoast.showToast(
msg: "${httpRes.msg}",
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIosWeb: 10,
backgroundColor: Colors.white,
textColor: Colors.black,
fontSize: 16.0);
//成功后刷新数据
setState(() {
userId.clear();
nickName.clear();
avatar.clear();
for (int i = 0; i < httpRes.data.list1.length; i++) {
//如果数据是空的就清理掉,不显示
if (httpRes.data.list1.length == 0) {
disposeClear();
} else {
//有数据就添加
userId.add(httpRes.data.list1[i].userId.toString());
nickName.add(httpRes.data.list1[i].nickname);
avatar.add(httpRes.data.list1[i].avatar);
}
}
});
}
}
@override
Widget build(BuildContext context) {
//显示拉黑列表的方法体
Widget blackItem(String userId) {
return Container(
margin: EdgeInsets.only(top: 15, left: 15),
child: Row(
children: [
Container(
width: 75,
height: 75,
margin: const EdgeInsets.only(right: 18.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(50),
image: const DecorationImage(
image: NetworkImage(
"https://www.itying.com/images/flutter/1.png"),
fit: BoxFit.cover,
),
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
margin: EdgeInsets.only(bottom: 8),
child: Text(
"Id: " + userId,
style: const TextStyle(
color: Color(0xFFCCCCCC),
fontSize: 16,
),
),
),
Container(
child: Text(
"昵称: "+userId,
style: const TextStyle(
color: Color(0xFFCCCCCC),
fontSize: 16,
),
),
),
],
)
],
));
}
//拉黑列表
Widget blackListSection = SizedBox(
child: ListView(
//解决ListView一定要设置高度才能显示问题
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
scrollDirection: Axis.vertical,
children: userId.map((parameter) => blackItem(parameter)).toList(),
),
);
//MaterialApp-Scaffold
return MaterialApp(
//Scaffold相当于布局的容器
home: Scaffold(
appBar: AppBar(
title: const Text('拉黑列表'),
backgroundColor: const Color(0xFF222222),
//文字居中
centerTitle: true,
),
//body就是写界面了
body: Container(
//设置界面背景
decoration: const BoxDecoration(
color: Color(0xFF222222),
),
child: ListView(
children: [
blackListSection,
],
),
)),
);
}
}
flutter 每次获取网络数据后都要setState刷新全局变量
最新推荐文章于 2024-04-26 15:08:46 发布