【发布时间】:2019-08-31 10:26:42
【问题描述】:
我想从列表加载页面,当用户点击抽屉中的项目时,他可以转到该页面(如果它已经打开),否则小部件将加载到所选页面中。
但我找不到该小部件是否已存在于列表中 if(myList.contains(Widget1())) => print('it exist'); 一个人告诉我覆盖 hashCode 和 operator==
class Widget6 extends StatelessWidget {
final String title = 'Widget6';
final Icon icon = Icon(Icons.assessment);
@override
Widget build(BuildContext context) {
return Center(
child: icon,
);
}
@override
bool operator ==(dynamic other) {
final Widget6 typedOther = other;
return title == typedOther.title && icon == typedOther.icon;
}
@override
int get hashCode => hashValues(title, icon);
}
如果我这样做,我就不能对这些小部件使用任何子小部件。获取异常,例如:type 'Center' is not a subtype of type 'Widget6'。我从颤振画廊复制了这个我没有找到好的文档/指南。对不起,我是初学者。
下面的完整代码
class _MyHomePageState extends State<MyHomePage> {
List pageList = [
Widget1(),
Widget2(),
Widget3(),
Widget4(),
];
PageController _pageController;
int _selectedIndex = 0;
@override
void initState() {
_pageController = PageController(
initialPage: _selectedIndex,
);
super.initState();
}
void navigatePage(Widget widget) {
// problem is here
if (pageList.contains(widget)) {
_pageController.animateToPage(pageList.indexOf(widget, 0),
duration: Duration(milliseconds: 300), curve: Curves.ease);
}
else {
setState(() {
pageList.removeAt(_pageController.page.toInt());
pageList.insert(_pageController.page.toInt(), widget);
});
_pageController.animateToPage(_pageController.page.toInt(),
duration: Duration(milliseconds: 300), curve: Curves.ease);
}
Navigator.pop(context);
}
@override
Widget build(BuildContext context) {
return Scaffold(
drawer: Drawer(
child: ListView(
children: <Widget>[
ListTile(
title: Text('Widget1'),
onTap: () => navigatePage(
Widget1(),
),
),
ListTile(
title: Text('Widget2'),
onTap: () => navigatePage(
Widget2(),
),
),
ListTile(
title: Text('Widget3'),
onTap: () => navigatePage(
Widget3(),
),
),
ListTile(
title: Text('Widget4'),
onTap: () => navigatePage(
Widget4(),
),
),
ListTile(
title: Text('Widget5'),
onTap: () => navigatePage(
Widget5(),
),
),
ListTile(
title: Text('Widget6'),
onTap: () => navigatePage(
Widget6(),
),
),
],
),
),
appBar: AppBar(
title: Text(widget.title),
),
body: PageView.builder(
onPageChanged: (newPage) {
setState(() {
this._selectedIndex = newPage;
});
},
controller: _pageController,
itemBuilder: (context, index) {
return Container(
child: pageList[index],
);
},
itemCount: pageList.length,
),
bottomNavigationBar: BottomNavigationBar(
currentIndex: _selectedIndex,
onTap: (index) => setState(() {
_selectedIndex = index;
_pageController.animateToPage(index,
duration: Duration(milliseconds: 300), curve: Curves.ease);
}),
items: pageList.map((page) {
return BottomNavigationBarItem(
backgroundColor: Colors.deepOrangeAccent,
icon: page.icon,
title: Text(page.title));
}).toList(),
),
);
}
}
这里的虚拟小部件列表
class Widget1 extends StatelessWidget {
final String title = 'Widget1';
final Icon icon = Icon(Icons.school);
@override
Widget build(BuildContext context) {
return Center(
child: icon,
);
}
}
class Widget2 extends StatelessWidget {
// only title and icon are changed
}
class Widget3 extends StatelessWidget {
// only title and icon are changed
}
class Widget4 extends StatelessWidget {
// only title and icon are changed
}
class Widget5 extends StatelessWidget {
// only title and icon are changed
}
class Widget6 extends StatelessWidget {
// only title and icon are changed
}
【问题讨论】:
-
一种解决方案是在您的小部件中使用一个键,然后使用 removeWhere 等方法来查找/删除...特定的小部件。
-
对不起,如果我不明白。为什么要使用 if(page List.contains(widget)? Widget 不在 List pageList=[Widget1(),...] 中创建?
-
@NorbertoMartínAfonso 要检查页面是否已打开,基于此我导航到该页面或打开所选页面。
-
您是否想过使用 SharedPreferences 并保存访问过的页面,例如:widgetonevisited=true 并在 initstate 时检查 SharedPreferences?
-
@NorbertoMartínAfonso 如果我在那些虚拟小部件中使用
operator ==,现在一切正常。但我得到了一个例外,@ 987654329@。更具体地说,我不能使用任何子小部件。
标签: flutter