我的home.dart
中有一个PageView,里面有多个有状态的小部件。当我转到SearchPageTest
页面时,有一个文本字段。每当我进入该文本字段时,我总是返回到PageView的第一页,在我的情况下是ImageFeedPage
。
下面是我的代码,将不胜感激。
home.dart
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
PageController pageController;
int pageIndex = 0;
@override
void initState() {
super.initState();
pageController = PageController();
}
@override
void dispose() {
pageController.dispose();
super.dispose();
}
onPageChanged(int pageIndex) {
if (pageIndex != 2) {
setState(() {
this.pageIndex = pageIndex;
});
}
}
onTap(int pageIndex, User user) {
if (user.documentID == null) {
Navigator.pushNamed(context, '/loginRegister');
} else {
if (pageIndex == 2) {
AddProductPage.show(context);
} else {
pageController.animateToPage(
pageIndex,
duration: Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
}
}
}
@override
Widget build(BuildContext context) {
final user = Provider.of<User>(context);
return Scaffold(
body: PageView(
children: <Widget>[
ImageFeedPage(), // <-- Tapping on text field always comes back here
SearchPageTest(),
new Container(),
ClosetPage(),
ChatPage(),
],
controller: pageController,
onPageChanged: onPageChanged,
physics: NeverScrollableScrollPhysics(),
),
bottomNavigationBar: CupertinoTabBar(
backgroundColor: Colors.white,
currentIndex: pageIndex,
onTap: (pageIndex) => onTap(pageIndex, user),
activeColor: Theme.of(context).primaryColor,
items: _buildMenuItems(),
),
);
}
List<BottomNavigationBarItem> _buildMenuItems() {
return [
BottomNavigationBarItem(
icon: Icon(Icons.home),
),
BottomNavigationBarItem(
icon: Icon(Icons.search),
),
BottomNavigationBarItem(
backgroundColor: Colors.green,
title: Text(
'Sell Now',
style: TextStyle(
color: Colors.cyan,
fontWeight: FontWeight.bold,
),
),
icon: Container(
child: Icon(
Icons.photo_camera,
size: 40.0,
color: Colors.cyan,
),
),
),
BottomNavigationBarItem(
icon: Icon(Icons.favorite),
),
BottomNavigationBarItem(
icon: Icon(Icons.chat),
),
];
}
}
searchPageTest.dart
class SearchPageTest extends StatefulWidget {
@override
_SearchPageTestState createState() => _SearchPageTestState();
}
class _SearchPageTestState extends State<SearchPageTest> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Search Page'),
actions: <Widget>[],
),
body: _buildBody(context),
drawer: Sidebar(),
);
}
_buildBody(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.start,
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.fromLTRB(10.0, 0, 10.0, 10.0),
child: TextField(),
),
],
);
}
}