如何从给定的代码创建自定义抽屉有状态小部件?

时间:2019-09-02 15:12:19

标签: flutter navigation-drawer flutter-layout statefulwidget flutter-listview

我已经根据现有问题的现有解决方案解决了上一个问题。

I want to change the color of CustomListTile which is child of ListView when onTap is clicked, and setting other children color into default one?

在为抽屉创建一个有状态的小部件时,我仍然遇到问题。

以下是Github指向项目的链接:https://github.com/Aaklii/flutter_app

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,
                    );
                  },
                ),
              ),
            )
          ],
        ),
      );
    }
  }

所以我目前正在使用buildDrawer()

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,
                  ),
                );
              },
            ),
          ),
        )
      ],
    ),
  ),
 );
}

0 个答案:

没有答案