[英]How do I call or find a way to get bottom nav bar to work with stateless widgets in Flutter?
static List<Widget> _pages = <Widget>[
inputPage(),
leaderboardsPage(),
userInfo(),
];
I have the above widget list and the one of the stateless widget class is initialised as follows:我有上面的小部件列表,无状态小部件 class 之一初始化如下:
class userInfo extends StatelessWidget {
and they are called in a bottom nav bar as follows并且它们在底部导航栏中被调用如下
child: _pages.elementAt(_selectedIndex),
However, I need to change userInfo to但是,我需要将 userInfo 更改为
class userInfo extends State<ProfilePage> {
because I need to use setState() and I cannot use setState if userInfo() is a Stateless Widget.因为我需要使用 setState(),如果 userInfo() 是无状态小部件,我不能使用 setState。
Any workaround that you can think of.您能想到的任何解决方法。 I am fairly new to Flutter and Dart so I don't know all about Widgets and States.我对 Flutter 和 Dart 相当陌生,所以我对小部件和状态一无所知。 Here's the needed code:这是所需的代码:
class ProfilePage extends StatefulWidget {
final User user;
ProfilePage({required this.user});
@override
_ProfilePageState createState() => _ProfilePageState();
}
class _ProfilePageState extends State<ProfilePage> {
int _selectedIndex = 0;
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
}
static List<Widget> _pages = <Widget>[
inputPage(),
leaderboardsPage(),
userInfo(),
];
class userInfo extends State<ProfilePage> {
userInfo();
bool _isSendingVerification = false;
bool _isSigningOut = false;
late User _currentUser;
@override
void initState() {
_currentUser = widget.user;
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sakirt Meter'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(bottom: 50.0),
child: Image.asset('assets/icon.png', fit: BoxFit.contain, height: 100),
//child: Text('Login', style: Theme.of(context).textTheme.headline1,),
),
Text(
'NAME: ${_currentUser.displayName}',
.
.
.
I don't completely understand what you are doing but I will let you know you should absolutely not declare your own State
class for another stateful widget, you should make a new stateful widget:我不完全理解你在做什么,但我会让你知道你绝对不应该为另一个有状态小部件声明你自己的State
class ,你应该制作一个新的有状态小部件:
class UserInfo extends StatefulWidget {
@override
_UserInfoState createState() => _UserInfoState();
}
class _UserInfoState extends State<UserInfo> {
...
}
If you need to access ProfilePage
's values from UserInfo
, you should do by passing said values as arguments:如果您需要从UserInfo
访问ProfilePage
的值,您应该将所述值作为 arguments 传递:
class UserInfo extends StatefulWidget {
const UserInfo({required this.user});
final User user;
@override
_UserInfoState createState() => _UserInfoState();
}
class _UserInfoState extends State<UserInfo> {
@override
void initState() {
_currentUser = widget.user;
super.initState();
}
}
then on ProfilePage
, you need to make some changes aswell:然后在ProfilePage
上,您还需要进行一些更改:
List<Widget> get _pages => <Widget>[
inputPage(),
leaderboardsPage(),
userInfo(user: widget.user),
];
_pages
can't be static because it uses user
, which is not static, I also made it into a getter so it works properly even if user
changes. _pages
不能是 static 因为它使用user
,而不是 static,我也将它变成了一个吸气剂,所以即使user
改变它也能正常工作。
If what you want is to rebuild or call setstate on ProfilePage
from UserInfo
, you can use a callback.如果您想要从UserInfo
重建或调用ProfilePage
上的 setstate,则可以使用回调。
Let's say I want ProfilePage
to rebuild on the press of a button on UserInfo
:假设我希望ProfilePage
在按下UserInfo
上的按钮时重建:
lass UserInfo extends StatefulWidget {
const UserInfo({required this.user, required this.onChanged});
final User user;
final VoidCallback? onChanged
@override
_UserInfoState createState() => _UserInfoState();
}
class _UserInfoState extends State<UserInfo> {
@override
void initState() {
_currentUser = widget.user;
super.initState();
}
@override
Widget build(BuildContext context) {
return ElevatedButton(
child: Text('press me!'),
onPressed: widget.onChanged,
);
}
}
Then on the pages getter:然后在页面吸气剂上:
List<Widget> get _pages = <Widget>[
inputPage(),
leaderboardsPage(),
userInfo(
user: widget.user,
onPressed: () => setState(() {
// some code or something :)
}),
),
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.