import 'dart:async';标签:username,const,Icons,dart,userInfo,import,5.17 From: https://www.cnblogs.com/kongxiangzeng/p/18246563
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:test1/page/video.dart';
// 导入获取用户信息的方法
import '../Do/UserDao.dart';
import '../component/container.dart';
import '../component/drawer.dart';
import 'Home1.dart';
import 'Special_Column.dart';
import 'openAL.dart';
import 'column.dart';
import 'login.dart';
class MyApp2 extends StatelessWidget {
final String username;
const MyApp2({Key? key, required this.username}) : super(key: key); // 调用父类构造函数
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '心语航标',
theme: ThemeData(
primarySwatch: Colors.purple,
),
home: MyHome2(username: username),
);
}
}
class MyHome2 extends StatefulWidget {
final String username;
const MyHome2({Key? key, required this.username}) : super(key: key);
@override
_MyHome2State createState() => _MyHome2State();
}
class _MyHome2State extends State<MyHome2> {
Map<String, dynamic> userInfo = {}; // 初始化userInfo
@override
void initState() {
super.initState();
getUserInfo(widget.username); // 调用获取用户信息的方法
}
//登录查询用户信息
Future<void> getUserInfo(String username) async {
Dio dio = Dio();
String apiUrl = "http://192.168.144.241:9090/user/findAll?user_name=$username";
try {
Response response = await dio.get(apiUrl);
print(response);
if (response.statusCode == 200) {
setState(() {
userInfo = response.data; // 更新userInfo
});
} else {
setState(() {
userInfo = {}; // 请求失败时将userInfo清空
});
}
} catch (e) {
print("Error fetching user info: $e");
setState(() {
userInfo = {}; // 发生异常时将userInfo清空
});
}
}
@override
Widget build(BuildContext context) {
return DefaultTabController(
length: 4,
child: Scaffold(
appBar: AppBar(
title: const Text('心语航标'),
centerTitle: true,
actions: <Widget>[
IconButton(
icon: const Icon(Icons.search),
onPressed: () {
// 搜索操作
},
),
],
),
drawer: buildCustomDrawer(
accountEmail: userInfo['user_email'] ?? '未知邮箱',
accountName: userInfo['user_name2'] ?? '未登录',
currentAccountPictureAsset: 'assets/阿尼亚.jpg',
drawerBackgroundAsset: 'assets/可可爱爱阿尼亚.jpg',
drawerItems: [
DrawerItem(title: '用户反馈', icon: Icons.feedback),
DrawerItem(title: '系统设置', icon: Icons.settings),
DrawerItem(title: '我要发布', icon: Icons.send),
DrawerItem(title: '注销', icon: Icons.exit_to_app),
],
onLoginPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => LoginPage(key: UniqueKey()),
),
);
},
),
bottomNavigationBar: buildCustomBottomNavigationBar(
tabs: [
const Tab(icon: Icon(Icons.home), text: '首页'),
const Tab(icon: Icon(Icons.article), text: '专栏'),
const Tab(icon: Icon(Icons.chat_bubble), text: 'AI咨询'),
const Tab(icon: Icon(Icons.notifications), text: '消息'),
],
backgroundColor: Colors.black,
height: 60.0,
labelStyle: const TextStyle(height: 0, fontSize: 12),
),
body: TabBarView(
children: <Widget>[
Text('data1'),
ColumnPage1(),
ChatDialog(),
ColumnPage(),
],
),
),
);
}
}