204 lines
5.4 KiB
Dart
204 lines
5.4 KiB
Dart
import 'dart:async';
|
|
import 'dart:io';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
import 'package:magicsound/tools/create/create_page.dart';
|
|
import 'package:magicsound/tools/home/my_home_page.dart';
|
|
import 'package:magicsound/tools/me/me_page.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
import 'common/Global.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
State<HomePage> createState() => _HomePageState();
|
|
}
|
|
|
|
class _HomePageState extends State<HomePage> {
|
|
StreamSubscription? subscription;
|
|
|
|
//创建StreamController
|
|
StreamController? streamController;
|
|
|
|
// 获取StreamSink用于发射事件
|
|
StreamSink? get streamSink => streamController?.sink;
|
|
|
|
// 获取Stream用于监听
|
|
Stream? get streamData => streamController?.stream;
|
|
|
|
int currentIndex = 0;
|
|
late List<BottomNavigationBarItem> items;
|
|
late List<StatefulWidget> bodyList;
|
|
late PageController pageController; //
|
|
|
|
AssetImage homeImg = const AssetImage('assets/images/ic_home.png');
|
|
AssetImage homeImgS = const AssetImage('assets/images/ic_home.png');
|
|
AssetImage createImg = const AssetImage('assets/images/ic_create.png');
|
|
AssetImage createImgS = const AssetImage('assets/images/ic_create.png');
|
|
AssetImage meImg = const AssetImage('assets/images/ic_me.png');
|
|
AssetImage meImgS = const AssetImage('assets/images/ic_me.png');
|
|
|
|
@override
|
|
void initState() {
|
|
// TODO: implement initState
|
|
super.initState();
|
|
streamController = StreamController<int>();
|
|
pageController = PageController(initialPage: 0);
|
|
streamSink!.add(0);
|
|
_firstFiring();
|
|
}
|
|
|
|
//判断首次启动
|
|
_firstFiring() async {
|
|
SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
bool boolValue = prefs.getBool('firstFiring') ?? false;
|
|
if (!boolValue) {}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
bottomItem();
|
|
return WillPopScope(
|
|
onWillPop: doubleClickBack,
|
|
child: Scaffold(
|
|
backgroundColor: Colors.white,
|
|
bottomNavigationBar: StreamBuilder(
|
|
stream: streamData,
|
|
builder: (BuildContext context, AsyncSnapshot snapshot) {
|
|
if (snapshot.data != null) {
|
|
currentIndex = snapshot.data;
|
|
return BottomNavigationBar(
|
|
items: items,
|
|
elevation: 0,
|
|
backgroundColor: Color(0xFF0E0A10),
|
|
currentIndex: snapshot.data,
|
|
onTap: onTap,
|
|
selectedItemColor: Color(0xFFFFFFFF),
|
|
unselectedItemColor: Color(0xFF818181),
|
|
selectedFontSize: 12,
|
|
unselectedFontSize: 12,
|
|
type: BottomNavigationBarType.fixed,
|
|
);
|
|
} else {
|
|
return Container();
|
|
}
|
|
},
|
|
),
|
|
body: Stack(
|
|
children: [
|
|
PageView(
|
|
controller: pageController,
|
|
onPageChanged: onPageChanged,
|
|
children: bodyList,
|
|
physics: NeverScrollableScrollPhysics(),
|
|
)
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
|
|
///底部导航
|
|
bottomItem() {
|
|
items = [
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: homeImg,
|
|
),
|
|
activeIcon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: homeImgS,
|
|
),
|
|
label: '首页'),
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: createImg,
|
|
),
|
|
activeIcon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: createImgS,
|
|
),
|
|
label: '创建'),
|
|
BottomNavigationBarItem(
|
|
icon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: meImg,
|
|
),
|
|
activeIcon: Image(
|
|
width: 24,
|
|
height: 24,
|
|
image: meImgS,
|
|
),
|
|
label: '我的'),
|
|
];
|
|
bodyList = [
|
|
MyHomePage(),
|
|
CreatePage(),
|
|
MePage(),
|
|
];
|
|
}
|
|
|
|
void onPageChanged(int index) {
|
|
streamSink!.add(index);
|
|
}
|
|
|
|
void onTap(int index) {
|
|
// if (index > 1 && NetworkConfig.userId == "") {
|
|
// showLoginDialog();
|
|
// } else {
|
|
// EventBusUtil.fire(TabBarEvent(index));
|
|
pageController.animateToPage(
|
|
index,
|
|
duration: const Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
// }
|
|
}
|
|
|
|
// 获取原生的值
|
|
invokeNativeMethod(String method, Map<String, dynamic> map) async {
|
|
dynamic args;
|
|
try {
|
|
args = await Global.method.invokeMethod(method, map);
|
|
} on PlatformException catch (e) {}
|
|
}
|
|
|
|
int last = 0;
|
|
|
|
//双击退出
|
|
Future<bool> doubleClickBack() async {
|
|
int now = DateTime.now().millisecondsSinceEpoch;
|
|
// if (currentIndex != 0) {
|
|
// EventBusUtil.fire(TabBarEvent(0));
|
|
// } else {
|
|
if (now - last > 2500) {
|
|
last = DateTime.now().millisecondsSinceEpoch;
|
|
EasyLoading.showToast("妙创音乐");
|
|
return Future.value(false);
|
|
} else {
|
|
exit(0);
|
|
}
|
|
// }
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
// TODO: implement dispose
|
|
streamController?.close();
|
|
subscription?.cancel();
|
|
pageController.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|