【问题标题】:flutter on click function for individual Drawer option个别抽屉选项的点击功能颤振
【发布时间】:2023-02-08 19:28:04
【问题描述】:

导入“包:buggy/theme/theme.dart”; 导入“包:flutter/material.dart”;

类 MyDrawer 扩展 StatelessWidget { 小部件 listTile({IconData? icon, String? title}){ 返回 InkWell( onTap: () {

  },
  child: ListTile(
    leading: Icon(
      icon,
      size: 32,
    ),
    title: Text(title!, style: TextStyle(color: Colors.black),),
  ),
);
}@overrideWidget build(BuildContext context) {return Drawer(child: Container(color: MyTheme.lightBluishColor,child: ListView(children: [DrawerHeader(child: Row(children: [CircleAvatar(backgroundColor: MyTheme.bluishColor,radius: 43,child: CircleAvatar(radius: 40,backgroundColor: MyTheme.creamColor,child: Image.network("https://images.unsplash.com/photo-1618641986557-1ecd230959aa?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8NXx8cHJvZmlsZXxlbnwwfHwwfHw%3D&auto=format&fit=crop&w=1000&q=60", height: 65,),),),Padding(padding: const EdgeInsets.all(8.0),child: Column(mainAxisAlignment: MainAxisAlignment.center,children: [
                        Text("Welcome Guest"),
                        Padding(
                          padding: const EdgeInsets.all(8.0),
                          child: ElevatedButton(onPressed: (){

                          },style: ElevatedButton.styleFrom(
                            backgroundColor: MyTheme.bluishColor
                          ), child: Text("Login")),
                        )
                      \],
                    ),
                  )
                \],
              ) 
            ),
            listTile(icon: Icons.home_outlined, title: "Home"),
            listTile(icon: Icons.person_outline, title: "Profile"),
            listTile(icon: Icons.notifications_outlined, title: "Notification"),
            listTile(icon: Icons.star_border_outlined, title: "Rating & Review"),
            listTile(icon: Icons.saved_search_sharp, title: "Saved"),
            listTile(icon: Icons.file_copy_outlined, title: "Raise to Complaint"),
            listTile(icon: Icons.format_quote_outlined, title: "FAQs"),
        ],
      ),
    ),

);

} }

这是我的抽屉代码。 如何为 List Tile 制作单独的 dart 文件,如通知、保存、配置文件等???

我希望每个 ListTile 都可以点击并打开一个新页面。

【问题讨论】:

    标签: android flutter flutter-dependencies flutter-animation


    【解决方案1】:

    您可以创建一个自定义小部件,我将分享一个示例代码

    tile({String? label = "", VoidCallback? onTap, IconData? iconData}) {
        return Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            ListTile(
              visualDensity: VisualDensity(vertical: -2),
              leading: Icon(iconData!),
              title: Text(label!),
              trailing: Icon(Icons.arrow_forward_ios, size: 15),
              onTap: onTap,
            ),
            Divider(thickness: 1, height: 1),
          ],
        );
      }
    }
    

    现在您可以在代码中调用此函数来代替 listTile,例如

    tile(
                        label: "Home",
                        iconData: Icons.home_outlined,
                        onTap: () {
                           //here you can write the code of which page to open on OnTap of Home tile
                        }),
    tile(
                        label: "Profile",
                        iconData: Icons.person_outline,
                        onTap: () {
                           //here you can write the code of which page to open on OnTap of Profile tile
                        }),
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-19
      • 1970-01-01
      • 1970-01-01
      • 2021-02-02
      • 2019-10-30
      • 1970-01-01
      • 2019-11-03
      • 2023-02-13
      相关资源
      最近更新 更多