92 lines
2.3 KiB
Dart
92 lines
2.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:talk/tools/home/home_chat_page.dart';
|
|
import 'package:talk/tools/home/home_model.dart';
|
|
|
|
import '../../beans/home_character_bean.dart';
|
|
|
|
///首页
|
|
class MyHomePage extends StatefulWidget {
|
|
const MyHomePage({super.key});
|
|
|
|
@override
|
|
State<MyHomePage> createState() => _MyHomePageState();
|
|
}
|
|
|
|
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin {
|
|
final PageController _pageController = PageController();
|
|
|
|
late StreamSubscription subscription;
|
|
final HomeModel _viewmodel = HomeModel();
|
|
|
|
///当前页下标
|
|
int _currentIndex = 0;
|
|
|
|
List<HomeCharacterBean> idList = [];
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
|
|
subscription = _viewmodel.streamController.stream.listen((newData) {
|
|
String code = newData['code'];
|
|
if (code.isNotEmpty) {
|
|
switch (code) {
|
|
case "getCharacterIdList":
|
|
idList = newData['data'];
|
|
print("idList==${idList[0].characterId}");
|
|
break;
|
|
case "getCharacterInfo":
|
|
break;
|
|
}
|
|
setState(() {});
|
|
}
|
|
});
|
|
|
|
_viewmodel.getCharacterIdList();
|
|
// _viewmodel.getCharacterInfo(1);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
_pageController.dispose();
|
|
subscription.cancel();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
Size size = MediaQuery.of(context).size;
|
|
|
|
return Scaffold(
|
|
backgroundColor: Color(0xFFE8EEFC),
|
|
body: PageView.custom(
|
|
controller: _pageController,
|
|
childrenDelegate: SliverChildBuilderDelegate(
|
|
(BuildContext context, int index) {
|
|
// 使用PageStorageKey来保存页面状态
|
|
final Key key = ValueKey<int>(index);
|
|
return Container(
|
|
key: key,
|
|
child: HomeChatPage(
|
|
key: key,
|
|
characterId: idList[index].characterId.toString(),
|
|
),
|
|
);
|
|
},
|
|
// 设置child数量
|
|
childCount: idList.length,
|
|
),
|
|
onPageChanged: (int index) {
|
|
setState(() {
|
|
_currentIndex = index;
|
|
});
|
|
},
|
|
),
|
|
);
|
|
}
|
|
}
|