【发布时间】:2021-03-08 20:12:34
【问题描述】:
我正在尝试使用 PageView 在 Flutter 中实现一个自定义的 BottomNavigationBar,以便在同一 Scaffold 中包含有状态小部件的不同页面之间滑动。
虽然我可以点击导航栏并更改页面,但我无法实现页面浏览滑动手势,同时还更改了指示所选页面的 icon_button 的颜色。我能够完美地使用 BottomNavigationBarItem 做到这一点,但我想使用自定义设计。
是否可以将包含不同 Icon_buttons 的 Stack Widget 转换为 BottomNavigationBarItem,以便我们可以使用 onTap 和 currentindex 函数来简化它?出路是什么?
import 'package:app/screens/screens.dart';
import 'package:flutter/material.dart';
class AppBottomNav extends StatefulWidget {
AppBottomNav({Key key}) : super(key: key);
@override
_AppBottomNavState createState() => _AppBottomNavState();
}
class _AppBottomNavState extends State<AppBottomNav> {
int currentIndex = 0;
var pages = [HomeScreen(), HistoryScreen(), ScanScreen(),
BleConnectionScreen(), AccountScreen()];
var _appPageController = PageController();
setBottomBarIndex(index) {
setState(() {
currentIndex = index;
});
}
@override
Widget build(BuildContext context) {
final Size size = MediaQuery.of(context).size;
return Scaffold(
backgroundColor: Colors.white.withAlpha(100),
body: PageView(
scrollDirection: Axis.horizontal,
children: pages,
onPageChanged: (index) {
setState(() {
currentIndex = index;
});
},
controller: _appPageController,
),
bottomNavigationBar:
Stack(
children: [
Positioned(
bottom: 0,
left: 0,
child: Container(
width: size.width,
height: 80,
child: Stack(
overflow: Overflow.visible,
children: [
CustomPaint(
size: Size(size.width, 80),
painter: BNBCustomPainter(),
),
Center(
heightFactor: 0.6,
child: FloatingActionButton(backgroundColor: Colors.green[700],
child: Icon(Icons.search), // Analyze Button
elevation: 0.1,
onPressed: () {}),
),
Container(
width: size.width,
height: 80,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
icon: Icon(
Icons.home,
color: currentIndex == 0 ? Colors.green[700] : Colors.grey.shade400,
),
onPressed: () {
setBottomBarIndex(0);
},
splashColor: Colors.white,
),
IconButton(
icon: Icon(
Icons.history,
color: currentIndex == 1 ? Colors.green[700] : Colors.grey.shade400,
),
onPressed: () {
setBottomBarIndex(1);
}),
Container(
width: size.width * 0.20,
),
IconButton(
icon: Icon(
Icons.bluetooth,
color: currentIndex == 2 ? Colors.green[700] : Colors.grey.shade400,
),
onPressed: () {
setBottomBarIndex(2);
}),
IconButton(
icon: Icon(
Icons.account_circle,
color: currentIndex == 3 ? Colors.green[700] : Colors.grey.shade400,
),
onPressed: () {
setBottomBarIndex(3);
}),
],
),
)
],
),
),
)
],
),
);
}
void _onTappedBar(int value) {
setState(() {
currentIndex = value;
});
_appPageController.jumpToPage(value);
}
}
class BNBCustomPainter extends CustomPainter {
@override
void paint(Canvas canvas, Size size) {
Paint paint = new Paint()
..color = Colors.white
..style = PaintingStyle.fill;
Path path = Path();
path.moveTo(0, 20); // Start
path.quadraticBezierTo(size.width * 0.20, 0, size.width * 0.35, 0);
path.quadraticBezierTo(size.width * 0.40, 0, size.width * 0.40, 20);
path.arcToPoint(Offset(size.width * 0.60, 20), radius: Radius.circular(20.0), clockwise: false);
path.quadraticBezierTo(size.width * 0.60, 0, size.width * 0.65, 0);
path.quadraticBezierTo(size.width * 0.80, 0, size.width, 20);
path.lineTo(size.width, size.height);
path.lineTo(0, size.height);
path.lineTo(0, 20);
canvas.drawShadow(path, Colors.black, 5, true);
canvas.drawPath(path, paint);
}
@override
bool shouldRepaint(CustomPainter oldDelegate) {
return false;
}
}
【问题讨论】:
-
我不久前做过类似的事情,但如果你想看看的话,这里使用不同的方法是 DartPad:DartPad