diff --git a/lib/guess/guess_page.dart b/lib/guess/guess_page.dart index ac10319..44d7508 100644 --- a/lib/guess/guess_page.dart +++ b/lib/guess/guess_page.dart @@ -12,7 +12,7 @@ class GuessPage extends StatefulWidget { State createState() => _GuessPageState(); } -class _GuessPageState extends State with SingleTickerProviderStateMixin{ +class _GuessPageState extends State with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin{ late AnimationController controller; @@ -114,4 +114,7 @@ class _GuessPageState extends State with SingleTickerProviderStateMix ), ); } + + @override + bool get wantKeepAlive => true; } diff --git a/lib/muyu/muyu_page.dart b/lib/muyu/muyu_page.dart index 1e2b9b7..6545b27 100644 --- a/lib/muyu/muyu_page.dart +++ b/lib/muyu/muyu_page.dart @@ -23,7 +23,7 @@ class MuyuPage extends StatefulWidget { State createState() => _MuyuPageState(); } -class _MuyuPageState extends State { +class _MuyuPageState extends State with AutomaticKeepAliveClientMixin { int _counter = 0; MeritRecord? _cruRecord; @@ -174,4 +174,7 @@ class _MuyuPageState extends State { maxPlayers: 1, ); } + + @override + bool get wantKeepAlive => true; } diff --git a/lib/navigation/app_navigation.dart b/lib/navigation/app_navigation.dart index 4923ffc..5f29c2f 100644 --- a/lib/navigation/app_navigation.dart +++ b/lib/navigation/app_navigation.dart @@ -14,6 +14,7 @@ class AppNavigation extends StatefulWidget { class _AppNavigationState extends State { int _index = 0; + final PageController _ctrl = PageController(); final List menus = const [ MenuData(label: '猜数字', icon: Icons.question_mark), @@ -26,7 +27,7 @@ class _AppNavigationState extends State { return Column( children: [ Expanded( - child: _buildContent(_index), + child: _buildContent(), ), AppBottomBar( currentIndex: _index, @@ -38,22 +39,22 @@ class _AppNavigationState extends State { } void _onChangePage(int index) { + _ctrl.jumpToPage(index); setState(() { _index = index; }); } - Widget _buildContent(int index) { - switch(index){ - case 0: - return const GuessPage(); - case 1: - return const MuyuPage(); - case 2: - return const Paper(); - default: - return const SizedBox.shrink(); - } + Widget _buildContent() { + return PageView( + physics: const NeverScrollableScrollPhysics(), + controller: _ctrl, + children: const [ + GuessPage(), + MuyuPage(), + Paper(), + ], + ); } } diff --git a/lib/paper/paper.dart b/lib/paper/paper.dart index 0e8138e..41951f7 100644 --- a/lib/paper/paper.dart +++ b/lib/paper/paper.dart @@ -16,7 +16,12 @@ class Paper extends StatefulWidget { State createState() => _PaperState(); } -class _PaperState extends State { +class _PaperState extends State with AutomaticKeepAliveClientMixin { + + @override + bool get wantKeepAlive => true; + + List _lines = []; // 线列表 int _activeColorIndex = 0; // 颜色激活索引