如何在Flutter的BottomNavigation栏中设置默认标签?

问题描述 投票:-1回答:3

我目前有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
3个回答
0
投票

这里初始化默认选项卡

int _selectedPage = 0;

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

int _selectedPage = 4;

0
投票
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"),
            ),
          ],
        ),
      ),
    );
  }
}

0
投票

您必须使用制表符控制器。

首先,您需要在课堂上扩展TickerProviderStateMixin

class MyAppState extends State<MyApp> with TickerProviderStateMixin {

}

然后像定义TabController,

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

最后在TabBarView中设置控制器

controller: _tabController,
© www.soinside.com 2019 - 2024. All rights reserved.