MaterialStateProperty<Color?>?和Color

  • 当为TextButton等button添加颜色时,使用ButtonStyle为其添加颜色
TextButton(
onPressed: () {},
child: Text('text'),
style:
ButtonStyle(backgroundColor:Colors.white),
);
  • 这样设置会报错,如题

MaterialStateProperty.all() 方法是设置点击事件所有状态下的样式。

MaterialStateProperty.resolveWith() 可拦截分别设置不同状态下的样式。

  • 如果所有的状态时的颜色都相同,使用MaterialStateProperty.all(),如果不同状态要使用不同的颜色时,用MaterialStateProperty.resolveWith(),例如:
TextButton(
onPressed: () {},
child: Text('text'),
style: ButtonStyle(
//backgroundColor:MaterialStateProperty.all(Colors.white)
backgroundColor: MaterialStateProperty.resolveWith(
(states) {
if (states.contains(MaterialState.focused) &&
!states.contains(MaterialState.pressed)) {
//获取焦点时的颜色
return Colors.blue;
} else if (states.contains(MaterialState.pressed)) {
//按下时的颜色
return Colors.deepPurple;
}
//默认状态使用灰色
return Colors.grey;
},
),
),
);

参考:https://zhuanlan.zhihu.com/p/278330232

Flutter 错误The argument type 'Color' can't be assigned to the parameter type 'MaterialStateProperty<Color?>?'.dart(argument_type_not_assignable)的更多相关文章

  1. passing argument 3 of ‘wtk_hlv_rec_init’ discards ‘const’ qualifier from pointer target type

    -Werror,编译出现如下错误: src/wtk/exam/wtk_ndx.c:154:6: error: passing argument 3 of ‘wtk_hlv_rec_init’ disc ...

  2. Spring错误之org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'bookService' is expected to be of type 'pw.fengya.tx.BookService' but was actually of type 'com.sun.proxy.$Proxy1

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cas ...

  3. 错误:Required request parameter 'XXX' for method parameter type String is not present

    错误信息:Required request parameter 'XXX' for method parameter type String is not present 这种都是前端请求方式不同,后 ...

  4. flutter报错The type of the function literal can't be inferred because the literal has a block as its body.A value of type 'String?' can't be assigned to a variable of type 'String'.

    flutter有一些报错如下 The type of the function literal can't be inferred because the literal has a block as ...

  5. Jekins 插件Extended Choice Parameter显示Json Parameter Type遇到的问题

    在jenkins中使用Extended Choice Parameter插件用来显示自定义的多选项,尝试通过groovy script来显示,正常,但查看它的例子,发现它例子中多选是通过类型 Json ...

  6. default parameter value for ‘color’ must be a compile-time constant

    定义了一个函数,函数有一个参数是Color类型的可选参数,想要设置其默认值为Color.Black http://stackoverflow.com/questions/2804395/c-sharp ...

  7. Remote error: VAR and OUT arguments must match parameter type exactly'

    在XE10中 downloadfile(filename: string; out FileStream: TStream; out FileSize: int64)是没有问题的,升级到delphi ...

  8. Does the parameter type of the setter match the return type of the getter?

    JDK 1.8, dubbo-admin版本是2.5.4-SNAPSHOT,tomcat8.5启动,报错: ERROR context.ContextLoader - Context initiali ...

  9. Refused to execute script from '....js' because its MIME type ('text/html') is not executable, and strict MIME type checking is enabled.md

    目录 问题描述 解决过程 总结 问题描述 在整合 Spring Boot.Spring Security.Thymeleaf 的练习中,对页面进行调试时,发现如下错误提示: Refused to ex ...

  10. because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checkin

    1 前言 浏览器报错误(chrome和firefox都会):because its MIME type ('text/html') is not a supported stylesheet MIME ...

随机推荐

  1. 线段树介绍(segment tree)

    1.引入 给定一个区间\([1, n]\),希望你实现一种数据结构,支持以下操作: 1.修改\(i\)号节点的值. 2.询问区间\([i, j]\)中所有节点的和. 这不是树状数组板子 3.修改区间\ ...

  2. wpf之样式

    在Window.Resources中书写样式 : <Window.Resources> <Style TargetType="Button" > </ ...

  3. 【技术分析】恶意 SPL 代币识别指南

    背景 在 EVM 生态上,存在各式各样的 ERC20 代币,因其实现方式有着极高的自由度,也催生了花样繁多的恶意代币.这些恶意代币通常会在代码中实现一些恶意的逻辑(禁止用户卖出,特权铸造或销毁等),其 ...

  4. HTB打靶记录-Cicada

    Nmap Scan nmap扫描一下ip nmap -sT -sV -O -Pn 10.10.11.35 Nmap scan report for 10.10.11.35 Host is up (0. ...

  5. Windows 使用 Mingw-w64 配置GCC套件

    Mingw-w64里的gcc和g++应该是很多人常用的编译工具.但是他的下载资源很乱,不好找(注意:MinGW和MinGW-w64是同宗同源的两款软件!),要么就是版本太老.SourceForge上有 ...

  6. C++刷题小知识点

    数据结构定义 struct ListNode { int val; ListNode *next; ListNode() : val(0), next(nullptr) {} ListNode(int ...

  7. 全网最适合入门的面向对象编程教程:58 Python字符串与序列化-序列化Web对象的定义与实现

    全网最适合入门的面向对象编程教程:58 Python 字符串与序列化-序列化 Web 对象的定义与实现 摘要: 如果我们要在不同的编程语言之间传递对象,就必须把对象序列化为标准格式,比如XML\YAM ...

  8. 3-2 C++ string类型

    目录 3.2.1 定义和初始化 3.2.2 对string的操作 (一)重要操作一览 (二)读写操作 cin/cout getline : 可读取整行 (三)获取大小 .size() (四)比较.赋值 ...

  9. CAD Plus 移动端使用帮助

    Mac端使用帮助 English help 如果您有疑问或需要帮助请发送邮件至 3167292926@qq.com 1. 权限要求 1.1 获取位置信息 使用文件管理功能时显示网络信息需要获取位置信息 ...

  10. 【THUPC 2024 初赛】 E 转化

    [THUPC 2024 初赛] 转化 我都能做出来,那就是大水题了. 思路 首先我们要确定最大可以变色的球的数量 \(tot\). 有如下两个贪心步骤: 所有颜色使用分裂操作,并更新 \(a_i\). ...