在导航器中未调用onGenerateRoute

时间:2019-12-15 17:53:53

标签: flutter flutter-navigation

为什么在以下代码中没有再次调用onGenerateRoute

import 'package:flutter/material.dart';

import 'screens/screen_one.dart';
import 'screens/screen_two.dart';

class Main extends StatefulWidget {
  @override
  _MainState createState() => _MainState();
}

class _MainState extends State<Main> {
  int _currentPage = 0;

  @override
  Widget build(BuildContext context) {
    print('render');
    return WillPopScope(
      onWillPop: () async {
        return false;
      },
      child: Column(
        children: <Widget>[
          Expanded(
            child: Navigator(
              onGenerateRoute: (RouteSettings settings) {
                WidgetBuilder builder;
                print('current');
                print(_currentPage);
                switch (_currentPage) {
                  case 0:
                    builder = (BuildContext context) => ScreenOne();
                    break;
                  case 1:
                    builder = (BuildContext context) => ScreenTwo();
                    break;
                  default:
                    throw Exception('Invalid route: $_currentPage');
                }
                return MaterialPageRoute(builder: builder, settings: settings);
              },
            ),
          ),
          BottomNavigationBar(
            currentIndex: this._currentPage,
            onTap: (int index) => this.setState(() => _currentPage = index),
            type: BottomNavigationBarType.fixed,
            items: <BottomNavigationBarItem>[
              BottomNavigationBarItem(icon: Icon(Icons.person), title: Text('Screen 1')),
              BottomNavigationBarItem(icon: Icon(Icons.label), title: Text('Screen 2')),
            ],
          )
        ],
      ),
    );
  }
}

当我单击底部栏中的不同项目时,_currentPage得到更新,但是屏幕没有变化,并且没有调用onGenerateRoute,因此current和{{1 }}不会打印输出。

0 个答案:

没有答案