flutter - 单击后如何使CircleAvatar更改颜色?

标签 flutter dart

 c
我尝试了在StackOverflow此处发布的其他解决方案,但是它们不起作用。有什么建议吗? `
Widget _method(String day){

return InkWell(
  child: new Container(
    decoration: BoxDecoration(
      shape: BoxShape.circle,
      color: Colors.black,
    ),
    child: CircleAvatar(
      backgroundColor: Colors.white,
      radius: 16.0,
      child: Text(day,
          style: TextStyle(
            color: Colors.black,
            fontSize: 12.0,
            fontFamily: 'Product-Sans',
          )),
    ),
  ),
  onTap: () {
    setState(() {

    });
  }
);
}`

最佳答案

有一些插件可以为您完成此操作,但是如果您想从头开始学习,请执行以下操作:
基本上,您保留一个变量_currentSelectedIndex,以跟踪当前选定的图块。当用户按下该图块时,您将调用setState并更新当前图块。然后,您可以配置选定和未选定图块的颜色。

import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Weekday selector',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(),
    );
  }
}

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

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _currentSelectedIndex;

  final Map<int, String> _weekdayIndices = {
    0: 'S',
    1: 'M',
    2: 'T',
    3: 'W',
    4: 'T',
    5: 'F',
    6: 'S',
  };

  Widget _weekdayTile(int index) {
    bool selected = index == _currentSelectedIndex;

    Color tileColor = selected ? Colors.red : Colors.white;
    Color textColor = selected ? Colors.white : Colors.black;

    return Container(
      decoration: BoxDecoration(
        shape: BoxShape.circle,
        color: Colors.black,
      ),
      child: InkWell(
        onTap: () {
          setState(() {
            _currentSelectedIndex = index;
          });
        },
        child: CircleAvatar(
          backgroundColor: tileColor,
          radius: 16.0,
          child: Text(
            _weekdayIndices[index],
            style: TextStyle(
              color: textColor,
            ),
          ),
        ),
      ),
    );
  }

  Widget _weekdayPicker() {
    List<Widget> tiles = [];

    for (int i = 0; i < 7; i++) {
      tiles.add(_weekdayTile(i));
    }

    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: tiles,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: _weekdayPicker(),
      ),
    );
  }
}

关于flutter - 单击后如何使CircleAvatar更改颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63855287/

相关文章:

flutter - 创建一个带有圆形边框的按钮

Flutter:倒置 ClipOval

xml - 如何在 dart/flutter 中获取节点的文本

list - 如何过滤和删除重复的 JSON 列表?

android - 如何在 Flutter 应用中的 PopupMenuItem 开头添加一个图标

flutter - flutter -在日历插件中创建动态标记事件

android - 如何检查按钮是否被按下

dart - Flutter:如何使用 Image 插件调整图像大小

dart - 多级符号的用例是什么?

Flutter dart 调用父类(super class)方法的super