如何在Flutter中滑动TabBarView()时改变Tabs()的形状?
How to change the shape of Tabs() when TabBarView() is swiped in Flutter?
我一直在尝试更改 TabBar()
中的 Tabs()
的形状,并且我在 TabBar()
中使用 onTap:(){}
属性 做到了。
但问题是我在TabBarView()
中滑动页面时想要相同的功能,所以我也尝试这样做,但我做不到。
我想在TabBarView()
中滑动页面时改变Tabs()
的形状。
下面是我的代码实现:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget with PreferredSizeWidget {
@override
_DemoState createState() => _DemoState();
@override
Size get preferredSize => AppBar().preferredSize;
}
class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
late TabController _tabController;
late int _index;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_index = _tabController.index;
return SafeArea(
child: Scaffold(
body: TabBarView(
controller: _tabController,
children: [
Pages(pageName: "Page 0"),
Pages(pageName: "Page 1"),
Pages(pageName: "Page 2"),
],
),
appBar: AppBar(
elevation: 0.0,
bottom: TabBar(
onTap: (value) {
setState(() {
value = _index;
print("TabIndex: $value");
});
},
labelColor: Colors.black,
indicator: BoxDecoration(
color: Colors.white,
borderRadius: _index == 0
? BorderRadius.only(topRight: Radius.circular(10.0))
: _index == 1
? BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0))
: BorderRadius.only(topLeft: Radius.circular(10.0)),
),
controller: _tabController,
tabs: [
Tab(text: "Tab0"),
Tab(text: "Tab1"),
Tab(text: "Tab2"),
],
),
),
),
);
}
}
class Pages extends StatelessWidget {
late final String pageName;
Pages({required this.pageName});
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
pageName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 60.0,
),
),
),
);
}
}
我在这里错过了什么?你能告诉我吗!!
这种混淆可以通过添加 addListners()
和 _tabController
来解决。
以下是我的回答:
我从 TabBar()
中删除了 onTap:(){}
属性,因为它不是必需的,在 initState(){}
中,我添加了 addListener()
方法和 _tabController
。这解决了所有问题,Tabs()
样式从 TabBarView()
监听滑动功能
import 'package:flutter/material.dart';
class Demo extends StatefulWidget with PreferredSizeWidget {
@override
_DemoState createState() => _DemoState();
@override
Size get preferredSize => AppBar().preferredSize;
}
class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
late TabController _tabController;
int _selectedIndex = 1;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
_tabController.addListener(() {
setState(() {
_selectedIndex = _tabController.index;
});
print("Current Index: $_selectedIndex");
});
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: TabBarView(
controller: _tabController,
children: [
Pages(pageName: "Page 0"),
Pages(pageName: "Page 1"),
Pages(pageName: "Page 2"),
],
),
appBar: AppBar(
elevation: 0.0,
bottom: TabBar(
labelColor: Colors.black,
indicator: BoxDecoration(
color: Colors.white,
borderRadius: _selectedIndex == 0
? BorderRadius.only(topRight: Radius.circular(10.0))
: _selectedIndex == 1
? BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0))
: BorderRadius.only(topLeft: Radius.circular(10.0)),
),
controller: _tabController,
tabs: [
Tab(text: "Tab0"),
Tab(text: "Tab1"),
Tab(text: "Tab2"),
],
),
),
),
);
}
}
class Pages extends StatelessWidget {
late final String pageName;
Pages({required this.pageName});
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
pageName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 60.0,
),
),
),
);
}
}
我一直在尝试更改 TabBar()
中的 Tabs()
的形状,并且我在 TabBar()
中使用 onTap:(){}
属性 做到了。
但问题是我在TabBarView()
中滑动页面时想要相同的功能,所以我也尝试这样做,但我做不到。
我想在TabBarView()
中滑动页面时改变Tabs()
的形状。
下面是我的代码实现:
import 'package:flutter/material.dart';
class Demo extends StatefulWidget with PreferredSizeWidget {
@override
_DemoState createState() => _DemoState();
@override
Size get preferredSize => AppBar().preferredSize;
}
class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
late TabController _tabController;
late int _index;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
_index = _tabController.index;
return SafeArea(
child: Scaffold(
body: TabBarView(
controller: _tabController,
children: [
Pages(pageName: "Page 0"),
Pages(pageName: "Page 1"),
Pages(pageName: "Page 2"),
],
),
appBar: AppBar(
elevation: 0.0,
bottom: TabBar(
onTap: (value) {
setState(() {
value = _index;
print("TabIndex: $value");
});
},
labelColor: Colors.black,
indicator: BoxDecoration(
color: Colors.white,
borderRadius: _index == 0
? BorderRadius.only(topRight: Radius.circular(10.0))
: _index == 1
? BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0))
: BorderRadius.only(topLeft: Radius.circular(10.0)),
),
controller: _tabController,
tabs: [
Tab(text: "Tab0"),
Tab(text: "Tab1"),
Tab(text: "Tab2"),
],
),
),
),
);
}
}
class Pages extends StatelessWidget {
late final String pageName;
Pages({required this.pageName});
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
pageName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 60.0,
),
),
),
);
}
}
我在这里错过了什么?你能告诉我吗!!
这种混淆可以通过添加 addListners()
和 _tabController
来解决。
以下是我的回答:
我从 TabBar()
中删除了 onTap:(){}
属性,因为它不是必需的,在 initState(){}
中,我添加了 addListener()
方法和 _tabController
。这解决了所有问题,Tabs()
样式从 TabBarView()
import 'package:flutter/material.dart';
class Demo extends StatefulWidget with PreferredSizeWidget {
@override
_DemoState createState() => _DemoState();
@override
Size get preferredSize => AppBar().preferredSize;
}
class _DemoState extends State<Demo> with SingleTickerProviderStateMixin {
late TabController _tabController;
int _selectedIndex = 1;
@override
void initState() {
super.initState();
_tabController = TabController(length: 3, vsync: this, initialIndex: 1);
_tabController.addListener(() {
setState(() {
_selectedIndex = _tabController.index;
});
print("Current Index: $_selectedIndex");
});
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return SafeArea(
child: Scaffold(
body: TabBarView(
controller: _tabController,
children: [
Pages(pageName: "Page 0"),
Pages(pageName: "Page 1"),
Pages(pageName: "Page 2"),
],
),
appBar: AppBar(
elevation: 0.0,
bottom: TabBar(
labelColor: Colors.black,
indicator: BoxDecoration(
color: Colors.white,
borderRadius: _selectedIndex == 0
? BorderRadius.only(topRight: Radius.circular(10.0))
: _selectedIndex == 1
? BorderRadius.only(
topLeft: Radius.circular(10.0),
topRight: Radius.circular(10.0))
: BorderRadius.only(topLeft: Radius.circular(10.0)),
),
controller: _tabController,
tabs: [
Tab(text: "Tab0"),
Tab(text: "Tab1"),
Tab(text: "Tab2"),
],
),
),
),
);
}
}
class Pages extends StatelessWidget {
late final String pageName;
Pages({required this.pageName});
@override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Text(
pageName,
style: TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 60.0,
),
),
),
);
}
}