为什么 flutter BottomNavigationBar 改变了图标但没有改变页面?
why flutter BottomNavigationBar changes the icon but does not change the page?
我是编码和 flutter 的新手,我一直在尝试让我的 bottomNavigationBar
将页面从 home page
更改为 chatroom
但是我尝试过的方法都不起作用,我看了很多视频,但他们的 bottomNavigationBar
在他们的 body 内,当我应用他们正在做的事情时,我在 body 和 return Scaffold(
之后的 padding
下得到了红线在我的 appBar:
image link 之前的括号下是代码的末尾
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:trading/core/const.dart';
import 'package:trading/models/apartment_model.dart';
import 'package:trading/pages/chat.dart';
import 'package:trading/pages/detail_page.dart';
class Homepage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var data = ApartmentModel.list;
int _selectedIndex = 0;
List<Widget> `enter code here`pages=[
Homepage(),
Chatroom(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: Text(
"find your product",
style: TextStyle(color: Colors.black87, fontWeight: FontWeight.bold),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.search,
color: Colors.black38,
),
onPressed: () {}),
IconButton(
icon: Icon(
Icons.filter_list,
color: Colors.black38,
),
onPressed: () {}),
],
),
bottomNavigationBar:
BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
type: BottomNavigationBarType.fixed,
selectedItemColor: AppColors.stylecolor,
unselectedItemColor: Colors.black38,
currentIndex: _selectedIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text("datsa")),
BottomNavigationBarItem(
icon: Icon(Icons.chat), title: Text("data"),),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text("data")),
],
),
body:
pages.elementAt(_selectedIndex),
Padding(
padding: EdgeInsets.all(16.0),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"65 result in your area",
style: TextStyle(color: Colors.black38),
),
Expanded(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => DetailPage(
data[index]
),
),
);
},
child: _buildItem(context, index));
},
),
),
// Container( child: _builBottomNavigationBar),
],
),
),
);
}
here is a pic of the red line that I was saying
您必须制作一个包含您的页面的 Widgets
列表,并在 body
中根据 Selected Index
.[=16= 填充 Widget
]
示例:
List<Widget> pages=[
Dashboard(),
Profile(),
Settings(),
];
然后将正文中的页面显示为:
body:
pages.elementAt(_selectedIndex),
body 不能带一个 widget,你可以指定页面和 padding 如果你想要两者你必须使用一列
我是编码和 flutter 的新手,我一直在尝试让我的 bottomNavigationBar
将页面从 home page
更改为 chatroom
但是我尝试过的方法都不起作用,我看了很多视频,但他们的 bottomNavigationBar
在他们的 body 内,当我应用他们正在做的事情时,我在 body 和 return Scaffold(
之后的 padding
下得到了红线在我的 appBar:
image link 之前的括号下是代码的末尾
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_rating_bar/flutter_rating_bar.dart';
import 'package:trading/core/const.dart';
import 'package:trading/models/apartment_model.dart';
import 'package:trading/pages/chat.dart';
import 'package:trading/pages/detail_page.dart';
class Homepage extends StatefulWidget {
@override
_HomepageState createState() => _HomepageState();
}
class _HomepageState extends State<Homepage> {
var data = ApartmentModel.list;
int _selectedIndex = 0;
List<Widget> `enter code here`pages=[
Homepage(),
Chatroom(),
];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.transparent,
elevation: 0,
title: Text(
"find your product",
style: TextStyle(color: Colors.black87, fontWeight: FontWeight.bold),
),
actions: <Widget>[
IconButton(
icon: Icon(
Icons.search,
color: Colors.black38,
),
onPressed: () {}),
IconButton(
icon: Icon(
Icons.filter_list,
color: Colors.black38,
),
onPressed: () {}),
],
),
bottomNavigationBar:
BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
type: BottomNavigationBarType.fixed,
selectedItemColor: AppColors.stylecolor,
unselectedItemColor: Colors.black38,
currentIndex: _selectedIndex,
items: [
BottomNavigationBarItem(
icon: Icon(Icons.home), title: Text("datsa")),
BottomNavigationBarItem(
icon: Icon(Icons.chat), title: Text("data"),),
BottomNavigationBarItem(
icon: Icon(Icons.person), title: Text("data")),
],
),
body:
pages.elementAt(_selectedIndex),
Padding(
padding: EdgeInsets.all(16.0),
child:
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
"65 result in your area",
style: TextStyle(color: Colors.black38),
),
Expanded(
child: ListView.builder(
physics: BouncingScrollPhysics(),
itemCount: data.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => DetailPage(
data[index]
),
),
);
},
child: _buildItem(context, index));
},
),
),
// Container( child: _builBottomNavigationBar),
],
),
),
);
}
here is a pic of the red line that I was saying
您必须制作一个包含您的页面的 Widgets
列表,并在 body
中根据 Selected Index
.[=16= 填充 Widget
]
示例:
List<Widget> pages=[
Dashboard(),
Profile(),
Settings(),
];
然后将正文中的页面显示为:
body:
pages.elementAt(_selectedIndex),
body 不能带一个 widget,你可以指定页面和 padding 如果你想要两者你必须使用一列