Как использовать TabController - PullRequest
0 голосов
/ 27 марта 2019

Я только что узнал флаттер, я не понял, как использовать TabController, я следовал тому, что было описано на официальном сайте, но появилась ошибка, и я не знаю, как ее исправить.

Я просто хочу изменить заголовок и ведущий из панели приложения при смене вкладок.

final List<ChangeTitleAndLeading> _data = [
  new ChangeTitleAndLeading(title: "Home", leading: Icon(Icons.home)),
  new ChangeTitleAndLeading(title: "Profile", leading: Icon(Icons.person)),
  new ChangeTitleAndLeading(title: "Friends", leading: Icon(Icons.people))
];

ChangeTitleAndLeading _handler;
TabController _controller;

@override
void initState() {
  super.initState();

  _checkEmailVerification();

  _controller = TabController(vsync: this, length: 3);
  _handler = _data[0];
  _controller.addListener(_handleSelected);
}

@override
void dispose() {
  _controller.dispose();
  super.dispose();
}

void _handleSelected() {
  setState(() {
    _handler = _data[_controller.index];
  });
}

return MaterialApp(
  theme: new ThemeData(
    primarySwatch: Colors.teal,
  ),
  home: new Scaffold(
    appBar: new AppBar(
      leading: Icon(Icons.home),
      title: new Text("Home"),
      bottom: new TabBar(
        controller: _controller,
        tabs: _tabs,
      ),
    ),

    body: TabBarView(
      controller: _controller,
      children: _pages,
    ),

    floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        onPressed: () {
          print('Current Index: ${_handler.title}');
        }
    ),

class ChangeTitleAndLeading {
  final String title;
  final Widget leading;

  ChangeTitleAndLeading({
    @required this.title,
    @required this.leading
  }) :
    assert(title != null),
    assert(leading != null);
}

Журнал ошибок:

Error Log:
I/flutter (19638): No TabController for TabBarView.
I/flutter (19638): When creating a TabBarView, you must either provide an explicit TabController using the "controller"
I/flutter (19638): property, or you must ensure that there is a DefaultTabController above the TabBarView.
I/flutter (19638): In this case, there was neither an explicit controller nor a default controller.
════════════════════════════════════════════════════════════════════════════════════════════════════

I / flutter (19638): Вышло еще одно исключение: НетTabController для TabBar.

И когда я изменяю это: leading: Icon(Icons.home), на leading: _handler.leading, и это: title: new Text("Home"), на title: new Text(_handler.title), всегда возвращать ошибку _handler.leading или _handler.title было нулевым

изображение

1 Ответ

0 голосов
/ 27 марта 2019

Проблема в том, что вам не хватает tabbarcontroller

Ваш код должен быть:

return MaterialApp(
  theme: new ThemeData(
    primarySwatch: Colors.teal,
  ),
  home:  DefaultTabController(
      length: 3,
child: new Scaffold(
    appBar: new AppBar(
      leading: Icon(Icons.home),
      title: new Text("Home"),
      bottom: new TabBar(
        controller: _controller,
        tabs: _tabs,
      ),
    ),

    body: TabBarView(
      controller: _controller,
      children: _pages,
    )...
...