Flutter 在颤振中改变TabBar形状
我制作了一个底部应用程序条,其形状属性设置为“CircularNotchedRectangle”,它工作起来就像一个符咒!问题是,我正在寻找TabBar提供的“滑动以更改页面”功能,但我看不到任何方法可以将其形状更改为圆形。我能改变它的形状吗?或者我应该尝试自己的“刷换页面”功能吗? 谢谢 我当前的底部导航栏:Flutter 在颤振中改变TabBar形状,flutter,Flutter,我制作了一个底部应用程序条,其形状属性设置为“CircularNotchedRectangle”,它工作起来就像一个符咒!问题是,我正在寻找TabBar提供的“滑动以更改页面”功能,但我看不到任何方法可以将其形状更改为圆形。我能改变它的形状吗?或者我应该尝试自己的“刷换页面”功能吗? 谢谢 我当前的底部导航栏: BottomAppBar( shape: CircularNotchedRectangle(), notchMargin: 2.0, child: Stack(
BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 2.0,
child: Stack(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
icon: Icon(Icons.search),
iconSize: 35,
color: widget.currentTab == 0 ? Colors.purple[500] : Colors.black,
onPressed: (){
setState(() {
widget.currentTab = 0;
});
}
),
IconButton(
icon: Icon(Icons.account_circle),
iconSize: 35,
color: widget.currentTab == 1 ? Colors.purple[500] : Colors.black,
onPressed: (){
setState(() {
widget.currentTab = 1;
});
}
),
SizedBox(width: 40),
IconButton(
icon: Icon(Icons.group),
iconSize: 35,
color: widget.currentTab == 2 ? Colors.purple[500] : Colors.black,
onPressed: (){
setState(() {
widget.currentTab = 2;
});
}
),
IconButton(
icon: Icon(Icons.chat_bubble),
iconSize: 35,
color: widget.currentTab == 3 ? Colors.purple[500] : Colors.black,
onPressed: (){
setState(() {
widget.currentTab = 3;
});
}
),
]
)
]
)
);
这就是我用TabBar想要的形状
您可以复制下面的粘贴运行完整代码
要使用当前代码实现
滑动以更改页面
功能您可以直接使用
PageView
代码片段
PageController pageController = PageController(
initialPage: 0,
keepPage: true,
);
Widget buildPageView() {
return PageView(
controller: pageController,
onPageChanged: (index) {
pageChanged(index);
},
children: <Widget>[
Red(),
Blue(),
Yellow(),
Green(),
],
);
}
...
void bottomTapped(int index) {
setState(() {
currentTab = index;
pageController.animateToPage(index,
duration: Duration(milliseconds: 500), curve: Curves.ease);
});
}
...
IconButton(
icon: Icon(Icons.search),
iconSize: 35,
color: currentTab == 0 ? Colors.purple[500] : Colors.black,
onPressed: () {
bottomTapped(0);
}),
PageController-PageController=PageController(
初始页:0,
基帕奇:是的,
);
Widget buildPageView(){
返回页面视图(
控制器:页面控制器,
onPageChanged:(索引){
页面更改(索引);
},
儿童:[
红色(),
蓝色(),
黄色(),
绿色(),
],
);
}
...
无效(整数索引){
设置状态(){
currentTab=索引;
pageController.animateToPage(索引,
持续时间:持续时间(毫秒:500),曲线:Curves.ease);
});
}
...
图标按钮(
图标:图标(Icons.search),
iconSize:35,
颜色:currentTab==0?颜色。紫色[500]:颜色。黑色,
已按下:(){
底部抽头(0);
}),
工作演示
完整代码
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
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: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double width;
Color primaryColor = Colors.blue;
int currentTab = 0;
PageController pageController = PageController(
initialPage: 0,
keepPage: true,
);
Widget buildPageView() {
return PageView(
controller: pageController,
onPageChanged: (index) {
pageChanged(index);
},
children: <Widget>[
Red(),
Blue(),
Yellow(),
Green(),
],
);
}
@override
void initState() {
super.initState();
}
void pageChanged(int index) {
setState(() {
currentTab = index;
});
}
void bottomTapped(int index) {
setState(() {
currentTab = index;
pageController.animateToPage(index,
duration: Duration(milliseconds: 500), curve: Curves.ease);
});
}
@override
Widget build(BuildContext context) {
width = MediaQuery.of(context).size.width;
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: buildPageView(),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.red,
child: const Icon(
Icons.add,
),
onPressed: () {},
),
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 2.0,
child: Stack(children: [
Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
IconButton(
icon: Icon(Icons.search),
iconSize: 35,
color: currentTab == 0 ? Colors.purple[500] : Colors.black,
onPressed: () {
bottomTapped(0);
}),
IconButton(
icon: Icon(Icons.account_circle),
iconSize: 35,
color: currentTab == 1 ? Colors.purple[500] : Colors.black,
onPressed: () {
bottomTapped(1);
}),
SizedBox(width: 40),
IconButton(
icon: Icon(Icons.group),
iconSize: 35,
color: currentTab == 2 ? Colors.purple[500] : Colors.black,
onPressed: () {
bottomTapped(2);
}),
IconButton(
icon: Icon(Icons.chat_bubble),
iconSize: 35,
color: currentTab == 3 ? Colors.purple[500] : Colors.black,
onPressed: () {
bottomTapped(3);
}),
])
])),
);
}
}
class Red extends StatefulWidget {
@override
_RedState createState() => _RedState();
}
class _RedState extends State<Red> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.purple,
);
}
}
class Blue extends StatefulWidget {
@override
_BlueState createState() => _BlueState();
}
class _BlueState extends State<Blue> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.blueAccent,
);
}
}
class Yellow extends StatefulWidget {
@override
_YellowState createState() => _YellowState();
}
class _YellowState extends State<Yellow> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.yellowAccent,
);
}
}
class Green extends StatefulWidget {
@override
_GreenState createState() => _GreenState();
}
class _GreenState extends State<Green> {
@override
Widget build(BuildContext context) {
return Container(
color: Colors.greenAccent,
);
}
}
导入“包装:颤振/材料.省道”;
void main()=>runApp(MyApp());
类MyApp扩展了无状态小部件{
//此小部件是应用程序的根。
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
双倍宽度;
颜色primaryColor=Colors.blue;
int currentTab=0;
PageController=PageController(
初始页:0,
基帕奇:是的,
);
Widget buildPageView(){
返回页面视图(
控制器:页面控制器,
onPageChanged:(索引){
页面更改(索引);
},
儿童:[
红色(),
蓝色(),
黄色(),
绿色(),
],
);
}
@凌驾
void initState(){
super.initState();
}
无效页面已更改(整型索引){
设置状态(){
currentTab=索引;
});
}
无效(整数索引){
设置状态(){
currentTab=索引;
pageController.animateToPage(索引,
持续时间:持续时间(毫秒:500),曲线:Curves.ease);
});
}
@凌驾
小部件构建(构建上下文){
宽度=MediaQuery.of(context).size.width;
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:buildPageView(),
floatingActionButtonLocation:floatingActionButtonLocation.centerDocked,
浮动操作按钮:浮动操作按钮(
背景颜色:Colors.red,
子:常量图标(
Icons.add,
),
按下:(){},
),
bottomNavigationBar:BottomAppBar(
形状:CircularNotchedRectangle(),
notchMargin:2.0,
子:堆栈(子:[
行(mainAxisAlignment:mainAxisAlignment.spaceAround,子项:[
图标按钮(
图标:图标(Icons.search),
iconSize:35,
颜色:currentTab==0?颜色。紫色[500]:颜色。黑色,
已按下:(){
底部抽头(0);
}),
图标按钮(
图标:图标(图标、账户和圆圈),
iconSize:35,
颜色:currentTab==1?颜色。紫色[500]:颜色。黑色,
已按下:(){
底部抽头(1);
}),
尺寸箱(宽度:40),
图标按钮(
图标:图标(Icons.group),
iconSize:35,
颜色:currentTab==2?颜色。紫色[500]:颜色。黑色,
已按下:(){
底部抽头(2);
}),
图标按钮(
图标:图标(Icons.chat_bubble),
iconSize:35,
颜色:currentTab==3?颜色。紫色[500]:颜色。黑色,
已按下:(){
底部抽头(3);
}),
])
])),
);
}
}
类Red扩展StatefulWidget{
@凌驾
_RedState createState()=>\u RedState();
}
类_RedState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:颜色,紫色,
);
}
}
类Blue扩展了StatefulWidget{
@凌驾
_BlueState createState();
}
类_BlueState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:Colors.blueAccent,
);
}
}
类黄色扩展StatefulWidget{
@凌驾
_YellowState createState();
}
类_YellowState扩展了状态{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:Colors.yellowAccent,
);
}
}
类Green扩展了StatefulWidget{
@凌驾
_GreenState createState()=>\u GreenState();
}
类_GreenState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
颜色:Colors.greenAccent,
);
}
}
您可以复制粘贴运行下面的完整代码要使用当前的c语言实现
滑动以更改页面
功能
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,