Flutter 如何从给定代码创建自定义抽屉有状态小部件?
我已经在现有问题解决方案的基础上解决了前面的问题 我仍然面临为抽屉创建有状态小部件的问题。 以下是指向项目的Github链接: 我想在稍后阶段为抽屉项提供更多功能,因此我想创建一个有状态的小部件,但此代码不起作用:Flutter 如何从给定代码创建自定义抽屉有状态小部件?,flutter,navigation-drawer,flutter-layout,statefulwidget,flutter-listview,Flutter,Navigation Drawer,Flutter Layout,Statefulwidget,Flutter Listview,我已经在现有问题解决方案的基础上解决了前面的问题 我仍然面临为抽屉创建有状态小部件的问题。 以下是指向项目的Github链接: 我想在稍后阶段为抽屉项提供更多功能,因此我想创建一个有状态的小部件,但此代码不起作用: 类ListDrawer扩展StatefulWidget{ 列出清单项目; ListDrawer({this.listOjects}); @凌驾 _ListDroperState createState()=>\u ListDroperState(); } 类_ListDrawers
类ListDrawer扩展StatefulWidget{
列出清单项目;
ListDrawer({this.listOjects});
@凌驾
_ListDroperState createState()=>\u ListDroperState();
}
类_ListDrawersState扩展状态{
@凌驾
小部件构建(构建上下文){
返回安全区(
子:列(
儿童:[
扩大(
孩子:填充(
填充:边缘设置。全部(20),
子项:ListView.builder(
itemCount:widget.listOjects.length,
itemBuilder:(上下文,索引){
返回CustomListTile(
listObject:widget.ListObjects[index],
//当选:
//selectedIndex!=null&&selectedIndex==index
//真的吗
//:错,
索引:索引,,
);
},
),
),
)
],
),
);
}
}
因此,我目前正在使用buildDrawer()
Drawer buildDrawer(BuildContext上下文){
回程抽屉(
儿童:安全区(
子:列(
儿童:[
扩大(
孩子:填充(
填充:边缘设置。全部(20),
子项:ListView.builder(
itemCount:ListObjects.length,
itemBuilder:(上下文,索引){
回墨槽(
onTap:(){
设置状态(){
当选(索引);
});
Navigator.of(context.pop();
},
子:CustomListTile(
listObject:ListObjects[索引],
当选:
selectedIndex!=null&&selectedIndex==index
真的吗
:错,
索引:索引,,
),
);
},
),
),
)
],
),
),
);
}
我认为这里有太多的代码和text@Eugene我把问题缩短了。只需要解决颤振应用程序抽屉部分的问题。
drawer: buildDrawer(context),
class ListDrawer extends StatefulWidget {
List<ListClass> listOjects;
ListDrawer({this.listOjects});
@override
_ListDrawerState createState() => _ListDrawerState();
}
class _ListDrawerState extends State<ListDrawer> {
@override
Widget build(BuildContext context) {
return SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.all(20),
child: ListView.builder(
itemCount: widget.listOjects.length,
itemBuilder: (context, index) {
return CustomListTile(
listObject: widget.listOjects[index],
// isSelected:
// selectedIndex != null && selectedIndex == index
// ? true
// : false,
index: index,
);
},
),
),
)
],
),
);
}
}
Drawer buildDrawer(BuildContext context) {
return Drawer(
child: SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Padding(
padding: EdgeInsets.all(20),
child: ListView.builder(
itemCount: listOjects.length,
itemBuilder: (context, index) {
return InkWell(
onTap: () {
setState(() {
onSelected(index);
});
Navigator.of(context).pop();
},
child: CustomListTile(
listObject: listOjects[index],
isSelected:
selectedIndex != null && selectedIndex == index
? true
: false,
index: index,
),
);
},
),
),
)
],
),
),
);
}