Flutter 如何在Flatter中的底部导航栏中设置默认选项卡?
我目前有5个选项卡,如何在启动应用程序时设置默认选项卡?假设我希望帐户选项卡成为打开应用程序时的默认选项卡 我该怎么做?顺便说一句,我使用WebView插件来显示网站的内容 下面是我的main.dart文件的代码。我试着寻找,但我找到的所有解决方案都不适合我,我想我找错了词。还有,我是新手,非常感谢Flutter 如何在Flatter中的底部导航栏中设置默认选项卡?,flutter,flutter-layout,flutter-dependencies,Flutter,Flutter Layout,Flutter Dependencies,我目前有5个选项卡,如何在启动应用程序时设置默认选项卡?假设我希望帐户选项卡成为打开应用程序时的默认选项卡 我该怎么做?顺便说一句,我使用WebView插件来显示网站的内容 下面是我的main.dart文件的代码。我试着寻找,但我找到的所有解决方案都不适合我,我想我找错了词。还有,我是新手,非常感谢 import 'package:syncshop/widgets/cart_page.dart'; import 'package:syncshop/widgets/categories_page.
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"),
),
],
),
),
);
}
}
这里初始化默认选项卡
int _selectedPage = 0;
如果您想要配置文件,您可以将其更改为任何选项卡
int _selectedPage = 4;
这里初始化默认选项卡
int _selectedPage = 0;
如果您想要配置文件,您可以将其更改为任何选项卡
int _selectedPage = 4;
您必须使用选项卡控制器 首先,您需要在类中扩展TickerProviderStateMixin
class MyAppState extends State<MyApp> with TickerProviderStateMixin {
}
最后在TabBarView中设置控制器
您必须使用选项卡控制器 首先,您需要在类中扩展TickerProviderStateMixin
class MyAppState extends State<MyApp> with TickerProviderStateMixin {
}
最后在TabBarView中设置控制器
您可以重写State的initState函数,并在其中放置一些初始代码
以你为例,你可以这样做
…您可以重写State的initState函数,并可以在其中放置一些初始代码
以你为例,你可以这样做
…创建底部导航栏时,请将此.currentindex设置为所需的索引
bottomNavigationBar: BottomNavigationBar(
currentindex=1,//This line, it's default 0
type: BottomNavigationBarType.fixed,
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
创建BottomNavigationBar时,请将此.currentindex设置为所需的索引
bottomNavigationBar: BottomNavigationBar(
currentindex=1,//This line, it's default 0
type: BottomNavigationBarType.fixed,
currentIndex: _selectedPage,
onTap: (int index) {
setState(() {
_selectedPage = index;
});
},
这回答了你的问题吗?这回答了你的问题吗?这解决了我的问题。另一个问题,使用ChyperX回答,我是否应该删除:int _selectedPage=0;然后用他的方法,它也能正常工作吗?这解决了我的问题。另一个问题,使用ChyperX回答,我是否应该删除:int _selectedPage=0;那么使用他的方法,它是否也能正常工作?我是否应该在代码的上述部分中创建此部分?类_myPageState使用TickerProviderStateMixin扩展状态{}检查我在其中所做的更改,在StateMixin之后使用TickerProviderStateMixin添加抱歉答复太晚,将来可能会使用它。我应该在代码的上述部分创建此部分吗?类_myPageState使用TickerProviderStateMixin扩展状态{}检查我在其中所做的更改的答案,在StateMixin之后使用TickerProviderStateMixin添加抱歉答复太晚,将来可能会使用此选项