mod: homePage开发、mainPage优化

This commit is contained in:
guozhigq
2023-04-18 17:12:32 +08:00
parent efa257b175
commit fbfdc2138b
6 changed files with 330 additions and 32 deletions

View File

@ -0,0 +1,30 @@
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:pilipala/pages/home/view.dart';
import 'package:pilipala/pages/hot/view.dart';
import 'package:pilipala/pages/mine/view.dart';
class MainController extends GetxController {
List<Widget> pages = <Widget>[
const HomePage(),
const HotPage(),
const MinePage(),
];
List navigationBars = [
{
'icon': const Icon(Icons.home_outlined),
'selectedIcon': const Icon(Icons.home),
'label': "推荐",
},
{
'icon': const Icon(Icons.whatshot_outlined),
'selectedIcon': const Icon(Icons.whatshot_rounded),
'label': "热门",
},
{
'icon': const Icon(Icons.person_outline),
'selectedIcon': const Icon(Icons.person),
'label': "我的",
}
];
}

View File

@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:pilipala/pages/home/view.dart';
import 'package:pilipala/pages/hot/view.dart';
import 'package:pilipala/pages/mine/view.dart';
import 'package:get/get.dart';
import './controller.dart';
class MainApp extends StatefulWidget {
const MainApp({super.key});
@ -10,12 +9,35 @@ class MainApp extends StatefulWidget {
State<MainApp> createState() => _MainAppState();
}
class _MainAppState extends State<MainApp> {
class _MainAppState extends State<MainApp> with SingleTickerProviderStateMixin {
final MainController _mainController = Get.put(MainController());
late AnimationController? _animationController;
late Animation<double>? _fadeAnimation;
late Animation<double>? _slideAnimation;
int selectedIndex = 0;
@override
void initState() {
super.initState();
_animationController = AnimationController(
duration: const Duration(milliseconds: 800),
reverseDuration: const Duration(milliseconds: 0),
value: 1,
vsync: this,
);
_fadeAnimation =
Tween<double>(begin: 0.8, end: 1.0).animate(_animationController!);
_slideAnimation =
Tween(begin: 0.8, end: 1.0).animate(_animationController!);
}
void setIndex(int value) {
if (selectedIndex != value) {
selectedIndex = value;
_animationController!.reverse().then((_) {
selectedIndex = value;
_animationController!.forward();
});
setState(() {});
}
}
@ -23,33 +45,34 @@ class _MainAppState extends State<MainApp> {
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: selectedIndex,
children: const [
HomePage(),
HotPage(),
MinePage(),
],
body: FadeTransition(
opacity: _fadeAnimation!,
child: SlideTransition(
position: Tween<Offset>(
begin: const Offset(0, 0.5),
end: Offset.zero,
).animate(
CurvedAnimation(
parent: _slideAnimation!,
curve: Curves.fastOutSlowIn,
reverseCurve: Curves.linear,
),
),
child: IndexedStack(
index: selectedIndex,
children: _mainController.pages,
),
),
),
bottomNavigationBar: NavigationBar(
elevation: 1,
destinations: const [
NavigationDestination(
icon: Icon(Icons.home_outlined),
selectedIcon: Icon(Icons.home),
label: "推荐",
),
NavigationDestination(
icon: Icon(Icons.whatshot_outlined),
selectedIcon: Icon(Icons.whatshot_rounded),
label: "热门",
),
NavigationDestination(
icon: Icon(Icons.person_outline),
label: "我的",
selectedIcon: Icon(Icons.person),
),
],
destinations: _mainController.navigationBars.map((e) {
return NavigationDestination(
icon: e['icon'],
selectedIcon: e['selectedIcon'],
label: e['label'],
);
}).toList(),
selectedIndex: selectedIndex,
onDestinationSelected: (value) => setIndex(value),
),