Flutter – 自定义底部导航栏
底部导航栏是应用程序底部的材质小部件,用于选择或导航到应用程序的不同页面。它通常与 Scaffold 一起使用,它作为Scaffold.bottomNavigationBar参数提供。
虽然Flutter 为您提供了 BottomNavigationBarflutter,但在本文中您将学习如何创建自己的底部导航栏。这将是一个深入的教程。
从底部导航属性的实现中可以看出, bottomNavigationBar并没有特别提到小部件。这使我们可以灵活地将我们选择的小部件分配给 Scaffold 的bottomNavigationBar属性。
让我们开始吧。
一旦你启动并运行了基本的flutter应用程序,你的 Material 应用程序就会将HomePage()类分配给 home 属性。
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Bottom NavBar Demo',
debugShowCheckedModeBanner: false,
home: HomePage(),
);
}
}
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Dart
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 1",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 2",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page3 extends StatelessWidget {
const Page3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 3",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page4 extends StatelessWidget {
const Page4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 4",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [],
),
),
);
}
}
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
),
);
}
}
Dart
theme: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
),
Dart
Theme(
data: Theme.of(context).copyWith (
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
)
child: child,
)
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffC4DFCB),
appBar: AppBar(
leading: Icon(
Icons.menu,
color: Theme.of(context).primaryColor,
),
title: Text(
"Geeks For Geeks",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
centerTitle: true,
backgroundColor: Colors.white,
),
body: pages[pageIndex],
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
),
);
}
}
Dart
Container buildMyNavBar(BuildContext context) {
return Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
);
}
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bottom NavBar Demo',
theme: ThemeData(
primaryColor: const Color(0xff2F8D46),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffC4DFCB),
appBar: AppBar(
leading: Icon(
Icons.menu,
color: Theme.of(context).primaryColor,
),
title: Text(
"Geeks For Geeks",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
centerTitle: true,
backgroundColor: Colors.white,
),
body: pages[pageIndex],
bottomNavigationBar: buildMyNavBar(context),
);
}
Container buildMyNavBar(BuildContext context) {
return Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
);
}
}
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 1",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 2",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page3 extends StatelessWidget {
const Page3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 3",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page4 extends StatelessWidget {
const Page4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 4",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
现在创建一个有状态的小部件并将其命名为 HomePage。从HomePage类返回一个 Scaffold。现在这个 Scaffold 是包含我们底部导航栏的主要元素。
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
在开始创建我们的底部导航栏之前,创建 2-5 个页面。始终尽量保持底部导航栏最少,最多包含 5 个项目(页面)。
页面是应用程序的不同屏幕。在本文中,我们将使用 4 个页面,所有这些页面都是无状态小部件,您可以拥有任何小部件,例如,您可以拥有有状态小部件、容器、中心小部件等。在本教程中,我们制作了 4 个基本的无状态小部件,它们返回一个基本的带有一些文字的页面。
Dart
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 1",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 2",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page3 extends StatelessWidget {
const Page3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 3",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page4 extends StatelessWidget {
const Page4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 4",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
在HomePage类中声明了一个 int 变量作为pageIndex将其初始化为 0。每次,我们打开我们从第一页开始的应用程序。您可以根据需要命名变量。 pageIndex是保存当前页面的索引。现在定义一个最终列表作为页面,该列表将包含我们应用程序的所有页面。
添加我们创建的所有 4 个页面。
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold();
}
}
现在让我们创建底部导航栏。
在HomePage类中,让我们定义bottomNavigationBar属性并为其分配一个 Container。使用一些BoxDecoration (Pixels) 给它一个 60 的高度,添加一个 Row 作为 Container 的子级。将主轴对齐设置为空间周围。
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [],
),
),
);
}
}
对于 Row 的 children 属性,添加IconButton小部件,这将是我们处理应用程序导航的按钮。现在在 Row 的子列表中添加 4 个按钮,添加所有必需的参数。一些必需的参数是 Icon, onTap回调,为了有一个干净流畅的界面,我们必须处理一些元素。首先,我们将在IconButtons 中将 enableFeedback属性设置为 false。
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {},
icon: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
),
);
}
}
现在我们将移动到材质应用程序的(主题:ThemeData)并添加以下属性。
Dart
theme: ThemeData(
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
),
但是有一个问题,上述操作将在整个应用程序中应用这些属性。如果您希望将这些功能应用于特定的小部件或小部件子树,只需使用主题小部件包装您的目标小部件并提供上述数据。
它看起来像这样:
Dart
Theme(
data: Theme.of(context).copyWith (
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
)
child: child,
)
简而言之,与将这些属性设置为默认值相比,这些属性将使用户体验更好。
谈到 onTap属性的实现,我们现在要做的是,我们将每个IconButton的pageIndex变量的状态分别设置为 (0, 1, 2, 3)。每次我们点击底部导航栏项目时,这将更新我们的 Scaffold 的主体。此外,我们添加了一些元素,例如一旦页面处于活动状态,我们将使用条件运算符更改图标。
Dart
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffC4DFCB),
appBar: AppBar(
leading: Icon(
Icons.menu,
color: Theme.of(context).primaryColor,
),
title: Text(
"Geeks For Geeks",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
centerTitle: true,
backgroundColor: Colors.white,
),
body: pages[pageIndex],
bottomNavigationBar: Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
),
);
}
}
至此,您已经成功创建了自己的自定义底部导航栏。您可以以任何您想要的方式自定义小部件,例如您可以提供边框半径颜色、间距、填充等。
谈到一些最佳实践,抽象你的代码总是好的。因此,为此,我们将编写一个函数(方法)来返回我们的目标底部导航栏小部件,而不是在脚手架中编写整个代码。
Dart
Container buildMyNavBar(BuildContext context) {
return Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
);
}
下面给出了完整的源代码以及一些额外的元素,如颜色等。
Dart
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Bottom NavBar Demo',
theme: ThemeData(
primaryColor: const Color(0xff2F8D46),
splashColor: Colors.transparent,
highlightColor: Colors.transparent,
hoverColor: Colors.transparent,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
class HomePage extends StatefulWidget {
const HomePage({Key? key}) : super(key: key);
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State {
int pageIndex = 0;
final pages = [
const Page1(),
const Page2(),
const Page3(),
const Page4(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xffC4DFCB),
appBar: AppBar(
leading: Icon(
Icons.menu,
color: Theme.of(context).primaryColor,
),
title: Text(
"Geeks For Geeks",
style: TextStyle(
color: Theme.of(context).primaryColor,
fontSize: 25,
fontWeight: FontWeight.w600,
),
),
centerTitle: true,
backgroundColor: Colors.white,
),
body: pages[pageIndex],
bottomNavigationBar: buildMyNavBar(context),
);
}
Container buildMyNavBar(BuildContext context) {
return Container(
height: 60,
decoration: BoxDecoration(
color: Theme.of(context).primaryColor,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(20),
topRight: Radius.circular(20),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 0;
});
},
icon: pageIndex == 0
? const Icon(
Icons.home_filled,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.home_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 1;
});
},
icon: pageIndex == 1
? const Icon(
Icons.work_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.work_outline_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 2;
});
},
icon: pageIndex == 2
? const Icon(
Icons.widgets_rounded,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.widgets_outlined,
color: Colors.white,
size: 35,
),
),
IconButton(
enableFeedback: false,
onPressed: () {
setState(() {
pageIndex = 3;
});
},
icon: pageIndex == 3
? const Icon(
Icons.person,
color: Colors.white,
size: 35,
)
: const Icon(
Icons.person_outline,
color: Colors.white,
size: 35,
),
),
],
),
);
}
}
class Page1 extends StatelessWidget {
const Page1({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 1",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page2 extends StatelessWidget {
const Page2({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 2",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page3 extends StatelessWidget {
const Page3({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 3",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
class Page4 extends StatelessWidget {
const Page4({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
color: const Color(0xffC4DFCB),
child: Center(
child: Text(
"Page Number 4",
style: TextStyle(
color: Colors.green[900],
fontSize: 45,
fontWeight: FontWeight.w500,
),
),
),
);
}
}
输出: