我在尝试实现此功能时遇到了麻烦。我看过很多建议使用WillPopScope
的教程,但对于Fragments
来说,我认为这不是合适的方法。
我只有一页,即HomeScreen
,其中包含一个BottomNavigation
和5个Fragments
。
我基本上想要的是如果用户位于 FirstFragment 中,那将是应用程序退出的时间。否则,它只会转到上一个片段。
这是我的主屏幕:
class HomeScreen extends StatefulWidget {
final List<String> fragmentItems = [
"feed",
"search",
"upload",
"stars",
"profile"
];
@override
_HomeScreenState createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool isScrolled = false;
static bool isStoriesShown = false;
static bool isHomeSelected = true;
static bool isUploadSelected = false;
static bool isSearchSelected = false;
static bool isStarsSelected = false;
static bool isProfileSelected = false;
BottomNavWidget navWidget;
int fragmentIndex;
int _currentIndex = 0;
final List<int> _backstack = [0];
@override
void initState() {
super.initState();
fragmentIndex = 0;
_getFragmentIndex(0);
isHomeSelected = true;
isSearchSelected = false;
isUploadSelected = false;
isStarsSelected = false;
isProfileSelected = false;
}
_getFragmentIndex(int pos) {
switch (pos) {
case 0:
return FeedFragment();
case 1:
return SearchFragment();
case 2:
return UploadFragment();
case 3:
return NotificationFragment();
case 4:
return ProfileFragment();
default:
return FeedFragment();
}
}
@override
Widget build(BuildContext context) {
return WillPopScope(
onWillPop: () {
return _onWillPop(context);
},
child: MaterialApp(
home: Scaffold(
bottomNavigationBar: CurvedNavigationBar(
height: 50,
animationDuration: Duration(milliseconds: 500),
animationCurve: Curves.easeInOut,
backgroundColor: Color(colorSecondary),
color: Color(colorBackground),
items: <Widget>[
isHomeSelected
? Container(
child: SvgPicture.asset(
"assets/icons/home_icon_solid.svg",
color: Color(colorPrimary),
fit: BoxFit.scaleDown,
height: 25,
width: 25,
))
: Container(
padding: EdgeInsets.all(25),
child: SvgPicture.asset(
"assets/icons/home_icon_stroke.svg",
color: Color(colorText),
fit: BoxFit.fitHeight,
)),
isSearchSelected
? Container(
child: SvgPicture.asset(
"assets/icons/search_icon_solid.svg",
color: Color(colorPrimary),
fit: BoxFit.scaleDown,
height: 25,
width: 25,
))
: Container(
padding: EdgeInsets.all(25),
child: SvgPicture.asset(
"assets/icons/search_icon_stroke.svg",
color: Color(colorText),
fit: BoxFit.fitHeight,
)),
isUploadSelected
? Container(
child: SvgPicture.asset(
"assets/icons/post_icon_solid.svg",
color: Color(colorPrimary),
fit: BoxFit.scaleDown,
height: 25,
width: 25,
))
: Container(
padding: EdgeInsets.all(25),
child: SvgPicture.asset(
"assets/icons/post_icon_stroke.svg",
color: Color(colorText),
fit: BoxFit.fitHeight,
)),
isStarsSelected
? Container(
child: SvgPicture.asset(
"assets/icons/star_icon_solid.svg",
color: Color(colorPrimary),
fit: BoxFit.scaleDown,
height: 25,
width: 25,
))
: Container(
padding: EdgeInsets.all(25),
child: SvgPicture.asset(
"assets/icons/star_icon_stroke.svg",
color: Color(colorText),
fit: BoxFit.fitHeight,
)),
isProfileSelected
? Container(
child: SvgPicture.asset(
"assets/icons/profile_icon_solid.svg",
color: Color(colorPrimary),
fit: BoxFit.scaleDown,
height: 25,
width: 25,
))
: Container(
padding: EdgeInsets.all(25),
child: SvgPicture.asset(
"assets/icons/profile_icon_stroke.svg",
color: Color(colorText),
fit: BoxFit.fitHeight,
))
],
onTap: (index) {
updateFragment(index);
},
),
resizeToAvoidBottomPadding: false,
appBar: AppBar(
elevation: 2,
backgroundColor: Color(colorBackground),
centerTitle: false,
title: Container(
width: MediaQuery.of(context).size.width,
alignment: Alignment.centerLeft,
padding: EdgeInsets.only(top: 18, bottom: 18),
child: Image.asset(
'assets/app/name.png',
fit: BoxFit.cover,
)),
leading: Container(
padding: EdgeInsets.only(top: 12, left: 12, bottom: 12),
child: Image.asset(
"assets/app/app_icon.png",
fit: BoxFit.contain,
)),
actions: <Widget>[
Container(
padding: EdgeInsets.all(10),
width: 50,
height: 50,
child: GestureDetector(
onTap: () {
Utils().logoutDialog(context);
},
child: SvgPicture.asset("assets/icons/logout.svg",
color: Color(colorPrimary)),
)),
],
),
body: _getFragmentIndex(fragmentIndex),
)));
}
updateFragment(int i) {
_backstack.add(i);
switch (i) {
case 0:
{
setState(() {
isHomeSelected = !isHomeSelected;
isProfileSelected = false;
isSearchSelected = false;
isStarsSelected = false;
isUploadSelected = false;
fragmentIndex = i;
});
break;
}
case 1:
{
setState(() {
isSearchSelected = !isSearchSelected;
isHomeSelected = false;
isProfileSelected = false;
isStarsSelected = false;
isUploadSelected = false;
fragmentIndex = i;
});
break;
}
case 2:
{
setState(() {
isUploadSelected = !isUploadSelected;
isHomeSelected = false;
isSearchSelected = false;
isStarsSelected = false;
isProfileSelected = false;
fragmentIndex = i;
});
break;
}
case 3:
{
setState(() {
isStarsSelected = !isStarsSelected;
isHomeSelected = false;
isSearchSelected = false;
isProfileSelected = false;
isUploadSelected = false;
fragmentIndex = i;
});
break;
}
case 4:
{
setState(() {
isProfileSelected = !isProfileSelected;
isHomeSelected = false;
isSearchSelected = false;
isStarsSelected = false;
isUploadSelected = false;
fragmentIndex = i;
});
break;
}
default:
return Text("Error");
}
}
答案 0 :(得分:0)
您只需要编写以下代码即可回到ScreenB
至ScreenA
,
Navigator.pop(context);