嗨,我正在使用Curved Navigation Bar构建页面,对于setState()可能需要StatefulWidget;显示所需的页面。
我下面的代码在StatelessWidget上工作正常,在这里我不能使用setState();表示我的BottomNavBar可见,但无法导航。 如果我切换到StatefullWidget,它会给我“错误:找不到Getter:'userId'。搜索(userId:userId,)”。斜体下方也出现红色下划线。
Search(userId: userId ,)
class BottomNavBar extends StatefulWidget {
final String userId;
const BottomNavBar({this.userId});
@override
_BottomNavBarState createState() => _BottomNavBarState();
}
class _BottomNavBarState extends State<BottomNavBar> {
int _page = 2;
final pages = [Settings(),
Favorite(),
Search(userId: userId,),
Chats(),
Account()
,];
@override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: CurvedNavigationBar(
index: 2,
height: 70.0,
color: Colors.pinkAccent,
buttonBackgroundColor: Colors.pink,
animationDuration: Duration(milliseconds: 500),
backgroundColor: Colors.white,
animationCurve: Curves.easeInOut,
onTap:(index){
setState(() {
_page = index;
});
},
items: <Widget>[
Icon(Icons.settings, color: Colors.white, size: 30),
Icon(Icons.favorite_border,color: Colors.white, size: 30),
Icon(Icons.home,color: Colors.white, size: 30),
Icon(Icons.chat_bubble_outline,color: Colors.white, size: 30),
Icon(Icons.perm_identity,color: Colors.white, size: 30),
]),
body: pages [_page],
);
}
}
这是Search()类的代码
class Search extends StatefulWidget {
final String userId;
const Search({this.userId});
@override
_SearchState createState() => _SearchState();
}
class _SearchState extends State<Search> {
final SearchReposit _searchReposit = SearchReposit();
SearchBloc _searchBloc;
User _user, _currentUser;
int difference;
getDifference(GeoPoint userLocation) async {
Position position = await Geolocator().getCurrentPosition();
double location = await Geolocator().distanceBetween(userLocation.latitude,
userLocation.longitude, position.latitude, position.longitude);
difference = location.toInt();
}
@override
void initState() {
_searchBloc = SearchBloc(searchReposit: _searchReposit);
super.initState();
}
@override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return BlocBuilder<SearchBloc, SearchState>(
bloc: _searchBloc,
builder: (context, state) {
if (state is InitialSearchState) {
_searchBloc.add(
LoadUserEvent(userId: widget.userId),
);
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.blueGrey),
),
);
}
if (state is LoadingState) {
return Center(
child: CircularProgressIndicator(
valueColor: AlwaysStoppedAnimation(Colors.blueGrey),
),
);
}
if (state is LoadUserState) {
_user = state.user;
_currentUser = state.currentUser;
getDifference(_user.location);
if (_user.location == null) {
return Text(
"No One Here",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
color: Colors.black),
);
} else
return profileWidget(
padding: size.height * 0.035,
photoHeight: size.height * 0.70,
photoWidth: size.width * 0.80,
photo: _user.photo,
clipRadius: size.height * 0.02,
containerHeight: size.height * 0.3,
containerWidth: size.width * 0.9,
child: Padding(
padding: EdgeInsets.symmetric(horizontal: size.width * 0.02),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(
height: size.height * 0.06,
),
Row(
children: <Widget>[
userGender(_user.gender),
Expanded(
child: Text(
" " +
_user.name +
", " +
(DateTime.now().year - _user.age.toDate().year)
.toString(),
style: TextStyle(
color: Colors.white,
fontSize: size.height * 0.05),
),
)
],
),
Row(
children: <Widget>[
Icon(
Icons.location_on,
color: Colors.white,
),
Text(
difference != null
? (difference / 1000).floor().toString() +
"km away"
: "away",
style: TextStyle(color: Colors.white),
)
],
),
SizedBox(
height: size.height * 0.05,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
iconWidget(EvaIcons.flash, () {}, size.height * 0.04,
Colors.yellow),
iconWidget(Icons.clear, () {
_searchBloc
.add(PassUserEvent(widget.userId, _user.uid));
}, size.height * 0.08, Colors.blue),
iconWidget(FontAwesomeIcons.solidHeart, () {
_searchBloc.add(
SelectUserEvent(
name: _currentUser.name,
photoUrl: _currentUser.photo,
currentUserId: widget.userId,
selectedUserId: _user.uid),
);
}, size.height * 0.06, Colors.red),
iconWidget(EvaIcons.options2, () {}, size.height * 0.04,
Colors.white)
],
)
],
),
),
);
} else
return Container();
},
);
}
}
带有Statelesswidget的代码可以正常工作,但是由于不包含setState,所以我无法从BottomNavBar更改页面。
class BottomNavBar extends StatelessWidget {
final userId;
const BottomNavBar({this.userId});
@override
Widget build(BuildContext context) {
int _page = 2;
final pages = [
Settings(),
Favorite(),
Search( userId: userId,
),
Chats(),
Account()
,
];
return Scaffold(
bottomNavigationBar: CurvedNavigationBar(
index: 2,
height: 70.0,
color: Colors.pinkAccent,
buttonBackgroundColor: Colors.pink,
animationDuration: Duration(milliseconds: 500),
backgroundColor: Colors.white,
animationCurve: Curves.easeInOut,
//onTap:(index){
// setState(() {
// _page = index;
// });
// },
onTap: (index) {
// setState(() {
// _page = index;
// });
},
items: <Widget>[
Icon(Icons.settings, color: Colors.white, size: 30,),
Icon(Icons.favorite_border, color: Colors.white, size: 30),
Icon(Icons.home, color: Colors.white, size: 30),
Icon(Icons.chat_bubble_outline, color: Colors.white, size: 30),
Icon(Icons.perm_identity, color: Colors.white, size: 30),
]),
body: pages [_page],
);
}
}
答案 0 :(得分:0)
变量ItemList
在类userId
中不存在,它是_BottomNavBarState
的一部分。为了从状态访问它,您需要执行BottomNavBar
另外,将widget.userId
的初始化移动到pages
initState