我制作了一个底部的应用程序栏,其形状属性设置为“ CircularNotchedRectangle”,它的工作就像一个魅力!问题是我正在寻找TabBar提供的“滑动更改页面”功能,但是我看不到将其形状更改为CircularNotchedRectangle的任何方法。我可以改变形状吗?还是应该尝试制作自己的“滑动更改页面”功能? 谢谢!
我当前的BottomNavigationBar:
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获得的形状
答案 0 :(得分:0)
您可以在下面复制粘贴运行完整代码
使用当前代码实现swipe to change page
功能
您可以直接使用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);
}),
工作演示
完整代码
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,
);
}
}
答案 1 :(得分:0)
您需要做的就是这样定义Scaffold中的Fab位置:
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,