【问题标题】:Flutter TabBarView and TabsFlutter TabBarView 和 Tabs
【发布时间】:2021-05-19 02:59:36
【问题描述】:

我的 TabBarView 和 Tabs 有一个奇怪的问题,那就是在使用 TabBarView 时,第一个屏幕的 appBar 标题出现在其他屏幕上。我只希望 screen1 中的 appBar 标题出现在 screen1 上,而 screen2 中的 appBar 标题出现在 screen2 上。 Screen1 和 Screen2 相同。 如何解决此错误?

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: TabBarDemo(),
    );
  }
}

class TabBarDemo extends StatefulWidget {
  @override
  _TabBarDemoState createState() => _TabBarDemoState();
}

class _TabBarDemoState extends State<TabBarDemo>
    with SingleTickerProviderStateMixin {
 TabController tabController;
  List<Tab> tabBars;
  List<Widget> tabBarViews;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
  tabController = new TabController(vsync: this, length: 3);
     tabBars = [
      Tab(icon: Icon(Icons.home, size: 34), text: 'Hem',),
      Tab(icon: Icon(Icons.search, size: 34), text: 'Sök',),
      Tab(icon: Icon(Icons.settings, size: 34), text: 'Inställningar'),
    ];
    tabBarViews = [welcomeLayout(context),Screen1(),Screen2()];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: ColorStyle.black,
        elevation: 0,
        automaticallyImplyLeading: false,
        title: Text('Home Screen'),
        centerTitle: true,
      ),
       body: TabBarView(
          children: tabBarViews,
          controller: tabController,
        ),
      bottomNavigationBar: TabBar(controller: tabController, tabs: tabBars,
        labelColor: ColorStyle.white,
      ),
    );
  }
}
class FirstScreen extends StatelessWidget {  
 @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: ColorStyle.black,
        elevation: 0,
        title: Text('First Screen'),
        centerTitle: true,
      ),
      backgroundColor: ColorStyle.black,
      body: Container()
      ),
    );
  }
}

【问题讨论】:

    标签: flutter title tabbar


    【解决方案1】:

    实际上它不是显示第一个屏幕的应用栏。这是您在 TabBarDemo 中提到的那个。因此,要摆脱它,请将其从他们的 i.e. 中删除-

    @override
      Widget build(BuildContext context) {
        return Scaffold(
           //removed appBar
           body: TabBarView(
              children: tabBarViews,
              controller: tabController,
            ),
          bottomNavigationBar: TabBar(controller: tabController, tabs: tabBars,
            labelColor: ColorStyle.white,
          ),
        );
      }
    }
    

    如果你想在welcomeLayout中显示它,那么让它成为一个脚手架小部件并在那里显示appbar。

    【讨论】:

    • 嗯,它是来自 TabBarDemo 的 appBar 标题出现在 Screen1 上,welcomeLayOut 只是一个带有一些子小部件的堆栈。 Screen1 的 AppBar 标题也显示在 TabBarDemo 的标题下。
    • 如果您不想在所有页面上使用相同的 appbar,请按照代码所示将其删除,然后将 appbar 单独添加到每个页面。为此,welcomeLayout 将堆栈包装为脚手架的主体。
    猜你喜欢
    • 2021-08-16
    • 2021-09-02
    • 1970-01-01
    • 2018-01-13
    • 2019-10-13
    • 2021-08-29
    • 2018-11-25
    • 2020-08-08
    • 2019-03-24
    相关资源
    最近更新 更多