4

i have been trying lots of solutions on changing appbar title with tabs, and i solved it but now there is a small problem; the appbar does change with the tabs when the tabs are pressed, but not the tabbarview. Swiping left or right on the tab bar view changes the tabbarview but not the tabs(appbar title does not change too).

my code below:

final List<Tabs> _tabs = [new Tabs(title: "Registered UAS", icon: new IconData(58826, fontFamily: 'MaterialIcons')),
new Tabs(title: "Registration", icon: IconData(57680, fontFamily: 'MaterialIcons'))
];

Tabs _myHandler ;

void initState() {
super.initState();
_controller = new TabController(length: 2, vsync: this);
_myHandler = _tabs[0];
_controller.addListener(_handleSelected);
}
void _handleSelected() {
setState(() {
  _myHandler= _tabs[_controller.index];
});
}

@override
Widget build(BuildContext context) {
return WillPopScope(
    child: DefaultTabController(
      length: 2,
      child: new Scaffold(
          appBar: AppBar(
            title: Text(_myHandler.title),
            bottom: new TabBar(
              controller: _controller,
              tabs: <Widget>[
                new Tab(icon: new Icon(_tabs[0].icon)),
                new Tab(icon: new Icon(_tabs[1].icon))
              ],
            ),
          ),
          body: new TabBarView(children: [...])

I followed this for changing appbar title on tab change and now im stuck with a similar situation like this, except i dont think the solution for it works for me(i have tried it)

4

2 回答 2

2

controller: _controller,通过在 TabBarView 下添加解决了它

于 2020-01-10T07:57:04.633 回答
1

如果你想使用自己的控制器,那么你不应该使用DefaultTabController小部件。例如,您使用的 DefaultTabController 不存在。

如果你想使用自己的控制器,你应该使用这个例子。 https://api.flutter.dev/flutter/material/TabController-class.html

如果您想在没有自己的控制器的情况下使用 DefaultTabController,您应该使用此示例。

import 'package:flutter/material.dart';

void main() {
  runApp(TabBarDemo());
}

class TabBarDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: DefaultTabController(
        length: 3,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              tabs: [
                Tab(icon: Icon(Icons.directions_car)),
                Tab(icon: Icon(Icons.directions_transit)),
                Tab(icon: Icon(Icons.directions_bike)),
              ],
            ),
            title: Text('Tabs Demo'),
          ),
          body: TabBarView(
            children: [
              Icon(Icons.directions_car),
              Icon(Icons.directions_transit),
              Icon(Icons.directions_bike),
            ],
          ),
        ),
      ),
    );
  }
} 
于 2020-01-26T07:14:46.107 回答