Anonymous
Как вернуться на домашнюю страницу при нажатии во Flutter
Сообщение
Anonymous » 16 апр 2024, 14:41
Это моя настройка нижней панели навигации
Код: Выделить всё
List labels = ['Home', 'Cart', 'Notification', 'Account'];
@override
Widget build(BuildContext context) {
return PopScope(
canPop: false,
onPopInvoked: ((didPop) {
if (selctedIndex != 0) {
setState(() {
selctedIndex = 0;
});
} else{
final now = DateTime.now();
if(currentPress == null || now.difference(currentPress!) > const Duration(seconds: 2)){
currentPress = now;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
elevation: 1,
behavior: SnackBarBehavior.floating,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)),
backgroundColor: Colors.black,
content: const Text('Double Press to exit', style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),),
duration: const Duration(seconds: 1),
),
);
} else {
SystemNavigator.pop();
}
}
}),
child: Scaffold(
body: items[selctedIndex],
bottomNavigationBar: BottomNavigationBar(
type: BottomNavigationBarType.fixed,
currentIndex: selctedIndex,
selectedLabelStyle: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w600,
color: Colors.black
),
elevation: 3,
items: List.generate(items.length, (index) {
return BottomNavigationBarItem(
icon: CustomIcon(
icon: icons[index],
isSelected: index == selctedIndex,
),
label: labels[index],
);
}),
onTap: (value) {
setState(() {
selctedIndex = value;
});
},
),
),
);
}
Это пример моего главного экрана
Код: Выделить всё
class _HomeScreenState extends State {
bool isExit = false;
bool categorySelected = false;
String categoryTitle ='';
CartItems categoryProduct = items[0];
@override
Widget build(BuildContext context) {
// double height = MediaQuery.of(context).size.height;
double width = MediaQuery.of(context).size.width;
return PopScope(
canPop: (categorySelected || isExit) ? false : true,
onPopInvoked: (didPop) {
setState(() {
categorySelected = false;
isExit = false;
});
},
child: categorySelected
? CategoryProductsScreen(title: categoryTitle, items: categoryProduct)
: Scaffold(
appBar: homeAppBar(
icon: CupertinoIcons.square_stack_3d_down_dottedline,
navigate: (){
// Navigator.of(context).push(MaterialPageRoute(builder: (context) => BottomBar(index: 3),));
}
),
// other widgets goes here
)
)
)
}
}
У меня здесь проблема, мне нужна одна вещь. Если я коснусь главного экрана на других экранах нижней панели, он правильно перейдет на главный экран, но на главном экране я покажу категорию продукты на главный экран при его вызове. В этом контексте, если я нажму на главный экран, он не перейдет на главный экран по умолчанию. как это установить
Подробнее здесь:
https://stackoverflow.com/questions/783 ... in-flutter
1713267669
Anonymous
Это моя настройка нижней панели навигации [code] List labels = ['Home', 'Cart', 'Notification', 'Account']; @override Widget build(BuildContext context) { return PopScope( canPop: false, onPopInvoked: ((didPop) { if (selctedIndex != 0) { setState(() { selctedIndex = 0; }); } else{ final now = DateTime.now(); if(currentPress == null || now.difference(currentPress!) > const Duration(seconds: 2)){ currentPress = now; ScaffoldMessenger.of(context).showSnackBar( SnackBar( elevation: 1, behavior: SnackBarBehavior.floating, shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(6)), backgroundColor: Colors.black, content: const Text('Double Press to exit', style: TextStyle(color: Colors.white, fontWeight: FontWeight.w600),), duration: const Duration(seconds: 1), ), ); } else { SystemNavigator.pop(); } } }), child: Scaffold( body: items[selctedIndex], bottomNavigationBar: BottomNavigationBar( type: BottomNavigationBarType.fixed, currentIndex: selctedIndex, selectedLabelStyle: const TextStyle( fontSize: 12, fontWeight: FontWeight.w600, color: Colors.black ), elevation: 3, items: List.generate(items.length, (index) { return BottomNavigationBarItem( icon: CustomIcon( icon: icons[index], isSelected: index == selctedIndex, ), label: labels[index], ); }), onTap: (value) { setState(() { selctedIndex = value; }); }, ), ), ); } [/code] Это пример моего главного экрана [code]class _HomeScreenState extends State { bool isExit = false; bool categorySelected = false; String categoryTitle =''; CartItems categoryProduct = items[0]; @override Widget build(BuildContext context) { // double height = MediaQuery.of(context).size.height; double width = MediaQuery.of(context).size.width; return PopScope( canPop: (categorySelected || isExit) ? false : true, onPopInvoked: (didPop) { setState(() { categorySelected = false; isExit = false; }); }, child: categorySelected ? CategoryProductsScreen(title: categoryTitle, items: categoryProduct) : Scaffold( appBar: homeAppBar( icon: CupertinoIcons.square_stack_3d_down_dottedline, navigate: (){ // Navigator.of(context).push(MaterialPageRoute(builder: (context) => BottomBar(index: 3),)); } ), // other widgets goes here ) ) ) } } [/code] У меня здесь проблема, мне нужна одна вещь. Если я коснусь главного экрана на других экранах нижней панели, он правильно перейдет на главный экран, но на главном экране я покажу категорию продукты на главный экран при его вызове. В этом контексте, если я нажму на главный экран, он не перейдет на главный экран по умолчанию. как это установить Подробнее здесь: [url]https://stackoverflow.com/questions/78334294/how-to-come-back-to-home-page-on-tap-in-flutter[/url]