更改选项的颜色并通过在颤振中点击它们来显示不同的小部件?

发布于 2025-01-09 07:36:13 字数 1538 浏览 1 评论 0原文

我正在使用颤振。我想在点击特定选项时更改其颜色,而不是同时更改所有选项。当我们按下一个选项时,它的颜色会发生变化,并在其下方显示不同的小部件(如选项卡栏)。代码附在下面。如果有人提供帮助,我很高兴。 ..

import 'package:flutter/material.dart';

class cards extends StatelessWidget {
const cards({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text("Cards"),
  ),
  body: Padding(
    padding: const EdgeInsets.only(top: 20),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        optionCards("A", "assets/icons/recycle.png", context),
        optionCards("B", "assets/icons/tools.png", context),
        optionCards("C", "assets/icons/file.png", context),
      ],
    ),
  ),
  );
  }

Widget optionCards(
String text,
String assetImage,
BuildContext context,
) {
return Container(
  width: 100,
  height: 100,
  decoration: const ShapeDecoration(
    color: Colors.grey,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(5),
      ),
    ),
  ),
  child: SingleChildScrollView(
    child: Column(
      children: [
        const Padding(
          padding: EdgeInsets.only(top: 13),
          child: IconButton(
            onPressed: null,
            icon: Icon(Icons.file_copy),
          ),
        ),
        Text(
          text,
          style: const TextStyle(
            fontSize: 14,
            fontFamily: 'CeraPro',
            color: Color.fromRGBO(0, 0, 0, 1),
          ),
        )
      ],
    ),
  ),
  );
 }
}

I am using flutter. I want to change the color of a particular option when I tap on it, not all the options at the same time. When we press an option its color changes and it shows different widgets below it(like tab bar). The code is attached below. I am glad if someone helps. ..

import 'package:flutter/material.dart';

class cards extends StatelessWidget {
const cards({Key? key}) : super(key: key);

@override
Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: const Text("Cards"),
  ),
  body: Padding(
    padding: const EdgeInsets.only(top: 20),
    child: Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        optionCards("A", "assets/icons/recycle.png", context),
        optionCards("B", "assets/icons/tools.png", context),
        optionCards("C", "assets/icons/file.png", context),
      ],
    ),
  ),
  );
  }

Widget optionCards(
String text,
String assetImage,
BuildContext context,
) {
return Container(
  width: 100,
  height: 100,
  decoration: const ShapeDecoration(
    color: Colors.grey,
    shape: RoundedRectangleBorder(
      borderRadius: BorderRadius.all(
        Radius.circular(5),
      ),
    ),
  ),
  child: SingleChildScrollView(
    child: Column(
      children: [
        const Padding(
          padding: EdgeInsets.only(top: 13),
          child: IconButton(
            onPressed: null,
            icon: Icon(Icons.file_copy),
          ),
        ),
        Text(
          text,
          style: const TextStyle(
            fontSize: 14,
            fontFamily: 'CeraPro',
            color: Color.fromRGBO(0, 0, 0, 1),
          ),
        )
      ],
    ),
  ),
  );
 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

始终不够 2025-01-16 07:36:13

保持保持选定选项卡的状态。

您的代码已使用选项卡栏可行性进行了更新。

更新的代码:

import 'package:flutter/material.dart';

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

  @override
  State<Card> createState() => _CardState();
}

class _CardState extends State<Card> {
  String selectedCard = '1';

  @override
  Widget build(BuildContext context) {
    final List<dynamic> tabOptionsList = [
      {"id": "1", "text": "A options"},
      {"id": "2", "text": "B options"},
      {"id": "3", "text": "C options"}
    ];

    final selectedTabValue = tabOptionsList
        .where((element) => element['id'] == selectedCard)
        .toList()[0]['text'];

    print(selectedTabValue);
    return Scaffold(
      appBar: AppBar(
        title: const Text("Cards"),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                optionCards("A", "assets/icons/recycle.png", context, "1"),
                optionCards("B", "assets/icons/tools.png", context, "2"),
                optionCards("C", "assets/icons/file.png", context, "3"),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              selectedTabValue,
              style: const TextStyle(color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }

  Widget optionCards(
      String text, String assetImage, BuildContext context, String cardId) {
    return GestureDetector(
      onTap: () {
        setState(() {
          selectedCard = cardId;
        });
      },
      child: Container(
        width: 100,
        height: 100,
        decoration: ShapeDecoration(
          color: cardId == selectedCard ? Colors.green : Colors.grey,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(5),
            ),
          ),
        ),
        child: SingleChildScrollView(
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.only(top: 13),
                child: IconButton(
                  onPressed: null,
                  icon: Icon(Icons.file_copy),
                ),
              ),
              Text(
                text,
                style: const TextStyle(
                  fontSize: 14,
                  fontFamily: 'CeraPro',
                  color: Color.fromRGBO(0, 0, 0, 1),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}


Maintain state for holding selected Tab.

Your code is updated with Tab bar Feasibility.

Updated Code:

import 'package:flutter/material.dart';

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

  @override
  State<Card> createState() => _CardState();
}

class _CardState extends State<Card> {
  String selectedCard = '1';

  @override
  Widget build(BuildContext context) {
    final List<dynamic> tabOptionsList = [
      {"id": "1", "text": "A options"},
      {"id": "2", "text": "B options"},
      {"id": "3", "text": "C options"}
    ];

    final selectedTabValue = tabOptionsList
        .where((element) => element['id'] == selectedCard)
        .toList()[0]['text'];

    print(selectedTabValue);
    return Scaffold(
      appBar: AppBar(
        title: const Text("Cards"),
      ),
      body: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 20),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                optionCards("A", "assets/icons/recycle.png", context, "1"),
                optionCards("B", "assets/icons/tools.png", context, "2"),
                optionCards("C", "assets/icons/file.png", context, "3"),
              ],
            ),
          ),
          Padding(
            padding: const EdgeInsets.all(8.0),
            child: Text(
              selectedTabValue,
              style: const TextStyle(color: Colors.red),
            ),
          ),
        ],
      ),
    );
  }

  Widget optionCards(
      String text, String assetImage, BuildContext context, String cardId) {
    return GestureDetector(
      onTap: () {
        setState(() {
          selectedCard = cardId;
        });
      },
      child: Container(
        width: 100,
        height: 100,
        decoration: ShapeDecoration(
          color: cardId == selectedCard ? Colors.green : Colors.grey,
          shape: const RoundedRectangleBorder(
            borderRadius: BorderRadius.all(
              Radius.circular(5),
            ),
          ),
        ),
        child: SingleChildScrollView(
          child: Column(
            children: [
              const Padding(
                padding: EdgeInsets.only(top: 13),
                child: IconButton(
                  onPressed: null,
                  icon: Icon(Icons.file_copy),
                ),
              ),
              Text(
                text,
                style: const TextStyle(
                  fontSize: 14,
                  fontFamily: 'CeraPro',
                  color: Color.fromRGBO(0, 0, 0, 1),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文