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. 一篇文章讲清楚synchronized关键字的作用及原理

    概述 在应用Sychronized关键字时需要把握如下注意点: 一把锁只能同时被一个线程获取,没有获得锁的线程只能等待: 每个实例都对应有自己的一把锁(this),不同实例之间互不影响:例外:锁对象是 ...

  2. 深入理解 Nuxt.js 中的 app:error:cleared 钩子

    title: 深入理解 Nuxt.js 中的 app:error:cleared 钩子 date: 2024/9/28 updated: 2024/9/28 author: cmdragon exce ...

  3. 《Vue.js 设计与实现》读书笔记 - 第5章、非原始值的响应式方案

    第5章.非原始值的响应式方案 5.1 理解 Proxy 和 Reflect Proxy Proxy 只能代理对象,不能代理非对象原始值,比如字符串. Proxy 会拦截对对象的基本语义,并重新定义对象 ...

  4. 深度学习 - Torch-TensorRT 推理加速

    深度学习 - Torch-TensorRT 推理加速 Torch-TensorRT 作为 TorchScript 的扩展. 它优化并执行兼容的子图,让 PyTorch 执行剩余的图. PyTorch ...

  5. Nodejs C++插件(N-API)

    Nodejs C++插件(N-API) 0. 环境搭建 1. JS中调用C++方法 1.1 JS中调用源文件的C++方法 1.2 JS中调用动态库的C++方法 2. C++中调用JS方法 2.1. C ...

  6. PRT预计算辐射传输方法

    PRT(Precomputed Radiance Transfer)技术是一种用于实时渲染全局光照的方法.它通过预计算光照传输来节省时间,并能够实时重现面积光源下3D模型的全局光照效果. 由于PRT方 ...

  7. C#的函数使用 和参数修饰符 out ref params

    // 函数和方法 // 函数好比对象的动作行为 在定义函数的时候,职责(作用/功能)越单一越好 满足高内聚 低耦合的开发思路 // 变量的命名规则 小驼峰 // 函数的命名规则 大驼峰 动词开头 // ...

  8. google 搜索技巧

    size qq.com inurl : size 指定网站 inurl 这个网站的指定内容 filetype 指定搜索的文件类型

  9. go~wasm插件的开发

    Go和TinyGo是两种不同的Go语言编译器,它们之间有以下几点区别: 目标平台: Go:Go语言编译器主要面向通用计算机平台,如Windows.Linux.macOS等. TinyGo:TinyGo ...

  10. Docker高阶篇(一)

    本篇章主要为工作实践过程中对高端应用的处理和把控 1.Docker复杂安装 mysql的主从复制 https://www.bilibili.com/video/BV1gr4y1U7CY?p=41&am ...