简体   繁体   English

Flutter web:选择时列表图块的背景颜色

[英]Flutter web : Background Color of list tile upon selection

I am trying to change the background color of a selected tile using a ListTile.我正在尝试使用 ListTile 更改所选图块的背景颜色。

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:righteous/pages/dashboard/screens/menu1/data1.dart';
import 'package:righteous/pages/dashboard/screens/menu2/data2.dart';
import '../pages/landing.dart';
class SideMenu extends StatelessWidget {
   SideMenu({
    Key? key,
  }) : super(key: key);
  int _selected = -1; // none selected index is -1
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          DrawerHeader(
            child: Image.asset("assets/icons/logo.png"),
          ),
          DrawerListTile(
            index: 0,
            selectedIndex: _selected,
            title: "Dashboard",
            svgSrc: "assets/icons/menu_dashbord.svg",
            press: () {
              Get.to(WelcomePage());
            },
          ),
          DrawerListTile(
            index: 1,
            selectedIndex: _selected,
            title: "Transaction",
            svgSrc: "assets/icons/menu_tran.svg",
            press: () {
              print(_selected);
              Get.to(Data1());
            },
          ),
          DrawerListTile(
            index: 2,
            selectedIndex: _selected,
            title: "Task",
            svgSrc: "assets/icons/menu_task.svg",
            press: () {
              Get.to(Data2());
            },
          ),
        ],
      ),
    );
  }
}

class DrawerListTile extends StatelessWidget {
  const DrawerListTile({
    Key? key,
    required this.title,
    required this.svgSrc,
    required this.press,
    required this.index,
    required this.selectedIndex,
  }) : super(key: key);

  final String title, svgSrc;
  final VoidCallback press;
  final int index, selectedIndex;
  @override
  Widget build(BuildContext context) {
    return ListTile(
      selected: index == selectedIndex,
      selectedColor: Colors.green ,
      onTap: press,
      horizontalTitleGap: 0.0,
      title: Text(
        title,
        style: const TextStyle(color: Colors.black),
      ),
    );
  }
}

It is side navbar on a flutter web project.I need to update ListTile background color based on selection.它是 flutter web 项目的侧面导航栏。我需要根据选择更新 ListTile 背景颜色。 Upon selection it will be directed to another page with the same sidebar but that selected tile should be colored.选择后,它将被定向到具有相同侧边栏的另一个页面,但所选的图块应该是彩色的。 Can someone give me a solution/updated code?有人可以给我一个解决方案/更新代码吗?

Try this:尝试这个:

import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:righteous/pages/dashboard/screens/menu1/data1.dart';
import 'package:righteous/pages/dashboard/screens/menu2/data2.dart';
import '../pages/landing.dart';


  class SideMenu extends StatefulWidget {
  const SideMenu({Key key}) : super(key: key);

  @override
  State<SideMenu> createState() => _SideMenuState();
}

class _SideMenuState extends State<SideMenu> {
  int _selected = -1; // none selected index is -1
  @override
  Widget build(BuildContext context) {
    return Drawer(
      child: ListView(
        children: [
          DrawerHeader(
            child: Image.asset("assets/icons/logo.png"),
          ),
          DrawerListTile(
            index: 0,
            selectedIndex: _selected,
            title: "Dashboard",
            svgSrc: "assets/icons/menu_dashbord.svg",
            press: () {
              setState(() {
                _selected = 0;
              });
              Get.to(WelcomePage());
            },
          ),
          DrawerListTile(
            index: 1,
            selectedIndex: _selected,
            title: "Transaction",
            svgSrc: "assets/icons/menu_tran.svg",
            press: () {
              setState(() {
                _selected = 1;
              });
              print(_selected);
              Get.to(Data1());
            },
          ),
          DrawerListTile(
            index: 2,
            selectedIndex: _selected,
            title: "Task",
            svgSrc: "assets/icons/menu_task.svg",
            press: () {
              setState(() {
                _selected = 2;
              });
              Get.to(Data2());
            },
          ),
        ],
      ),
    );
  }
}

class DrawerListTile extends StatelessWidget {
  const DrawerListTile({
    Key? key,
    // For selecting those three line once press "Command+D"
    required this.title,
    required this.svgSrc,
    required this.press, 
    required this.index, 
    required this.selectedIndex, 
  }) : super(key: key);

  final String title, svgSrc;
  final VoidCallback press;
  final int index, selectedIndex;
  @override
  Widget build(BuildContext context) {
    return ListTile(
      selectedColor: Colors.white,
      selectedTileColor: Colors.blue,
      selected: index == selectedIndex,
      onTap: press,
      horizontalTitleGap: 0.0,
      // leading: SvgPicture.asset(
      //   svgSrc,
      //   color: Colors.white54,
      //   height: 16,
      // ),
      title: Text(
        title,
        style: const TextStyle(color: Colors.black),
      ),
    );
  }
}

also you can set background color for selected one with selectedTileColor and change text and icon color of selected one with selectedColor .您还可以使用 selectedTileColor 为选定的一项设置背景颜色,并使用selectedTileColor更改selectedColor一项的文本和图标颜色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM