【问题标题】:How do I set a default tab in BottomNavigation bar in Flutter?如何在 Flutter 的底部导航栏中设置默认选项卡?
【发布时间】:2019-12-10 03:50:08
【问题描述】:

我目前有 5 个标签页,如何在启动应用程序时设置默认标签页?假设我希望帐户选项卡成为打开应用程序时的默认选项卡。

我该怎么做?顺便说一句,我使用 WebView 插件来显示网站的内容。

下面是我的 main.dart 文件的代码。我试过搜索,但我发现的所有解决方案都不适合我(我认为我搜索错误的单词。另外,我是新来的)非常感谢!

import 'package:syncshop/widgets/cart_page.dart';
import 'package:syncshop/widgets/categories_page.dart';
import 'package:syncshop/widgets/home_page.dart';
import 'package:syncshop/widgets/profile_account.dart';
import 'package:syncshop/widgets/search_page.dart';


void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return MyAppState();
  }
}

class MyAppState extends State<MyApp> {
int _selectedPage = 0;
final _pageOptions = [
  HomeScreen(),
  CategoriesPage(),
  SearchPage(),
  CartPage(),
  ProfileAccount(),
];

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Sync Shop',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
      body: _pageOptions[_selectedPage],
      bottomNavigationBar: BottomNavigationBar(
          type: BottomNavigationBarType.fixed,
          currentIndex: _selectedPage,
          onTap: (int index) {
            setState((){
      _selectedPage = index;
            });
          },
          items: [
          BottomNavigationBarItem(icon: Icon(Icons.home), title: Text("Home"),
          ),
          BottomNavigationBarItem(icon: Icon(Icons.category), title: Text("Categories"),
          ),
          BottomNavigationBarItem(icon: Icon(Icons.search), title: Text("Search"),
          ),
          BottomNavigationBarItem(icon: Icon(Icons.shopping_cart), title: Text("Cart"),
          ),
          BottomNavigationBarItem(icon: Icon(Icons.account_circle), title: Text("Profile"),
          ),
          ],
          ),
      ),
      );
  }
}

【问题讨论】:

标签: flutter flutter-layout flutter-dependencies


【解决方案1】:

您可以覆盖StateinitState 函数,并可以在那里放置一些初始代码。

所以对于你的例子,可以这样做。

...

class MyAppState extends State<MyApp> {
    int _selectedPage = 0;
    @override
    void initState() {
        super.initState();
        _selectedPage = 4;
    }

...

【讨论】:

    【解决方案2】:

    当你创建 BottomNavigationBar 时,将 this.currentindex 设置为你想要的索引

    bottomNavigationBar: BottomNavigationBar(
              currentindex=1,//This line, it's default 0
              type: BottomNavigationBarType.fixed,
              currentIndex: _selectedPage,
              onTap: (int index) {
                setState(() {
                  _selectedPage = index;
                });
              },
    

    【讨论】:

      【解决方案3】:

      您必须使用标签控制器。

      首先你需要在课堂上扩展TickerProviderStateMixin

      class MyAppState extends State<MyApp> with TickerProviderStateMixin {
      
      }
      

      然后定义 TabController 之类的,

      _tabController = new TabController(length: _tabLength, vsync: this, initialIndex: 1);
      

      最后在你的TabBarView中设置控制器

      controller: _tabController,
      

      【讨论】:

      • 我应该在我的代码的上述部分中创建这部分吗? class _myPageState 使用 TickerProviderStateMixin{ } 扩展 State
      • 检查答案我做了修改,在State&lt;MyApp&gt;之后添加with TickerProviderStateMixin
      • 抱歉回复晚了,以后可能会用这个
      【解决方案4】:

      这里初始化默认选项卡

      int _selectedPage = 0;
      

      您可以将其更改为您想要的任何选项卡,如果您想要个人资料

      int _selectedPage = 4;
      

      【讨论】:

      • 这解决了我的问题。另一个问题,使用 ChyperX 的答案,我应该删除: int _selectedPage = 0;那么按照他的方式,效果会一样吗?
      【解决方案5】:
      import 'package:flutter/material.dart';
      import 'package:syncshop/widgets/cart_page.dart';
      import 'package:syncshop/widgets/categories_page.dart';
      import 'package:syncshop/widgets/home_page.dart';
      import 'package:syncshop/widgets/profile_account.dart';
      import 'package:syncshop/widgets/search_page.dart';
      
      void main() => runApp(MyApp());
      
      class MyApp extends StatefulWidget {
        @override
        State<StatefulWidget> createState() {
          return MyAppState();
        }
      }
      
      class MyAppState extends State<MyApp> {
        int _selectedPage = 4;
      
        final _pageOptions = [
          HomeScreen(),
          CategoriesPage(),
          SearchPage(),
          CartPage(),
          ProfileAccount(),
        ];
      
        @override
        Widget build(BuildContext context) {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            title: 'Sync Shop',
            theme: ThemeData(
              primarySwatch: Colors.blue,
            ),
            home: Scaffold(
              body: _pageOptions[_selectedPage],
              bottomNavigationBar: BottomNavigationBar(
                type: BottomNavigationBarType.fixed,
                currentIndex: _selectedPage,
                onTap: (int index) {
                  setState(() {
                    _selectedPage = index;
                  });
                },
                items: [
                  BottomNavigationBarItem(
                    icon: Icon(Icons.home),
                    title: Text("Home"),
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.category),
                    title: Text("Categories"),
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.search),
                    title: Text("Search"),
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.shopping_cart),
                    title: Text("Cart"),
                  ),
                  BottomNavigationBarItem(
                    icon: Icon(Icons.account_circle),
                    title: Text("Profile"),
                  ),
                ],
              ),
            ),
          );
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-10-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-14
        • 1970-01-01
        相关资源
        最近更新 更多