首页 > 解决方案 > 从底部应用程序栏颤动更改页面

问题描述

我是 Flutter 的新手,我正在尝试使用 2 个导航栏。我TabBar的工作也很好,但我BottomAppBar的页面更改不起作用。是的,我想保留我的 FAB BottomAppBar 的设计,这就是我不想使用BottomNavigationBar.

我已阅读以下答案:但我的身体已经分配,​​所以我无法制作 PageView。

我的代码:

class MyApp extends StatelessWidget {
  // This widget is the root of your application.

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: DefaultTabController(
        length: 6,
        child: Scaffold(
          appBar: AppBar(
            bottom: TabBar(
              isScrollable: true,
              tabs: [
                Tab(text: "NewPay1.1"),
                Tab(text: "NewMall 1.0"),
                Tab(text: "海报"),
                Tab(text: "企业版"),
                Tab(text: "个版"),
                Tab(text: "poa")
              ],
            ),
            title: Text('tabBar'),
          ),
          body: TabBarView(
            children: [
              TaskListPage(),
              TestPage(),
            ],
          ),
          bottomNavigationBar: BottomAppBar(
            color: Colors.white,
            shape: CircularNotchedRectangle(),
            child: Row(
              children: <Widget>[
                IconButton(onPressed: () {
                  print("home clicked");
                  TaskListPage();
                  },
                  icon: Icon(Icons.home),),
                SizedBox(),
                IconButton(onPressed: () {
                  print("third clicked");
                  TestPage();
                },
                    icon: Icon(Icons.more))
              ],
              mainAxisAlignment: MainAxisAlignment.spaceAround,
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: (){
              return TestPage().createState();
            },
            child: Icon(Icons.add),
          ),
          floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
        ),
      ),
    );
  }
}

标签: dartflutter

解决方案


输出:

在此处输入图像描述

这是你需要的。

class _MyAppState extends State<MyApp> with TickerProviderStateMixin {
  TabController _tabController; // use this instead of DefaultTabController

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 6, vsync: this); // initialise it here
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(primarySwatch: Colors.blue),
      home: Scaffold(
        appBar: AppBar(
          bottom: TabBar(
            controller: _tabController,
            isScrollable: true,
            tabs: [
              Tab(text: "NewPay1.1"),
              Tab(text: "NewMall 1.0"),
              Tab(text: "海报"),
              Tab(text: "企业版"),
              Tab(text: "个版"),
              Tab(text: "poa"),
            ],
          ),
          title: Text('tabBar'),
        ),
        body: TabBarView(
          controller: _tabController,
          children: [ // these are your pages 
            Page1(),
            Page2(),
            Page3(),
            Page4(),
            Page5(),
            Page6(),
          ],
        ),
        bottomNavigationBar: BottomAppBar(
          color: Colors.white,
          shape: CircularNotchedRectangle(),
          child: Row(
            children: <Widget>[
              IconButton(
                onPressed: () => _tabController.animateTo(0), // go to page1
                icon: Icon(Icons.home),
              ),
              SizedBox(),
              IconButton(onPressed: () => _tabController.animateTo(1), // go to page 2
              icon: Icon(Icons.more))
            ],
            mainAxisAlignment: MainAxisAlignment.spaceAround,
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: () {},
          child: Icon(Icons.add),
        ),
        floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      ),
    );
  }
}

推荐阅读