我正在尝试使其工作,当我单击 Android 设备上的后退按钮以弹出当前屏幕并转到上一个屏幕时,现在当我从详细信息屏幕单击后退按钮时,它会完全退出应用程序。我在详细信息屏幕中使用 PopScope 并将 canPop 设置为 true。
这是我设置导航的地方
class MainPage extends StatefulWidget {
const MainPage({super.key});
@override
State<MainPage> createState() => _MainPageState();
}
class _MainPageState extends State<MainPage> {
.....
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFF12142d),
appBar: MyAppBar(
searchTextController: searchTextController,
updateSearchText: updateSearchText),
body: IndexedStack(
index: _currentIndex,
children: [
Navigator(
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => Search(
onSearchCompleted: updateSearchData,
roomsController: roomsController,
bathroomsController: bathroomsController,
),
),
),
Navigator(
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => HomeScreen(
searchCriteria: searchCriteria,
searchText: searchText,
showAppBar: false,
),
),
),
Navigator(
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => const Wishlist(),
),
),
Navigator(
onGenerateRoute: (settings) => MaterialPageRoute(
builder: (context) => const OptionsTab(),
),
),
],
),
bottomNavigationBar: BottomNavigationBar(
showSelectedLabels: false,
showUnselectedLabels: false,
type: BottomNavigationBarType.fixed,
backgroundColor: const Color(0xFF12142d),
selectedItemColor: const Color(0xFFB98B54),
unselectedItemColor: Colors.white,
elevation: 0.0,
currentIndex: _currentIndex,
onTap: (index) {
setState(() {
_currentIndex = index;
});
},
items: [
BottomNavigationBarItem(
icon: ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 30, maxWidth: 30),
child: SvgPicture.asset(
'assets/icons/search.svg',
color: _currentIndex == 0
? const Color(0xFFB98B54)
: Colors.white,
),
),
label: ""),
BottomNavigationBarItem(
icon: ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 30, maxWidth: 30),
child: SvgPicture.asset(
'assets/icons/home.svg',
color: _currentIndex == 1
? const Color(0xFFB98B54)
: Colors.white,
),
),
label: ""),
BottomNavigationBarItem(
icon: ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 30, maxWidth: 30),
child: SvgPicture.asset(
'assets/icons/heart.svg',
color: _currentIndex == 2
? const Color(0xFFB98B54)
: Colors.white,
),
),
label: ""),
BottomNavigationBarItem(
icon: ConstrainedBox(
constraints:
const BoxConstraints(maxHeight: 30, maxWidth: 30),
child: SvgPicture.asset(
'assets/icons/settings.svg',
color: _currentIndex == 3
? const Color(0xFFB98B54)
: Colors.white,
),
),
label: ""),
]),
);
}
}
主屏幕:
class HomeScreen extends StatefulWidget {
final Map<String, dynamic>? searchCriteria;
final String? searchText;
final bool showAppBar;
const HomeScreen(
{super.key,
this.searchCriteria,
this.searchText,
required this.showAppBar});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
.....
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: widget.showAppBar ? AppBar() : null,
backgroundColor: const Color(0xFF12142d),
body: isLoading
? const Center(child: MyCircularProgressIndicator())
: properties.isEmpty
? const Center(
child: Text(
"no search results",
style: TextStyle(color: Colors.white),
),
)
: ListView.builder(
itemCount: properties.length,
itemBuilder: (context, index) {
final property = properties[index];
return ListingCard(
property: property,
onLearnMoreTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailScreen(id: property.id),
),
);
},
);
},
),
);
}
}
和详细信息屏幕,我可以通过应用栏中的图标返回:
class DetailScreen extends StatefulWidget {
final int id;
final PropertyType? propertyType;
final Location? location;
final List<SellingType>? sellingTypes;
final List<Extra>? extras;
const DetailScreen(
{Key? key,
required this.id,
this.propertyType,
this.location,
this.sellingTypes,
this.extras})
: super(key: key);
@override
State<DetailScreen> createState() => _DetailScreenState();
}
class _DetailScreenState extends State<DetailScreen> {
void myDummyFunction() {
//print("Button was pressed!");
}
late final PageController _pageController;
@override
void initState() {
super.initState();
_pageController = PageController();
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return PopScope(
canPop: true,
child: Scaffold(
appBar: AppBar(
leading: IconButton(
icon: const Icon(Icons.arrow_back, color: Color(0xFFB98B54)),
onPressed: () {
Navigator.of(context).pop();
},
),
),
在 PopScope 小部件中包含以下代码。
PopScope(
canPop: true,
onPopInvoked: (didPop) {
Navigator.of(context).pop();
},
child: "your widget",
)