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. 如何创建一个Java游戏客户端

    创建一个完整的Java游戏客户端示例是一个相对复杂的任务,因为它通常涉及图形用户界面(GUI).事件处理.游戏逻辑等多个方面.为了简化,我将提供一个基于Java Swing的简单游戏客户端示例:一个简 ...

  2. 暑假集训CSP提高模拟8

    一看见题目列表就吓晕了,还好我是体育生,后面忘了 唉这场比赛没啥好写的,要不就是太难要不就是太简单要不就是拉出去写在专题里了 A. 基础的生成函数练习题 考虑到只有奇偶性相同才能尝试加二,因此先用加一 ...

  3. 【赵渝强老师】什么是Redis Cluster

    (一)什么是Redis Cluster? Redis Cluster是Redis的分布式解决方案,在Redis 3.0版本正式推出的,有效解决了Redis分布式方面的需求.当遇到单机内存.并发.流量等 ...

  4. 更新预警(bushi)

    一回首,上次更新已经是将近3个月前了.但是博主不是似了,也不是逍遥快活游山玩水纸醉金迷乐不思蜀,而是上班太忙还是单休,所以没什么时间更新博客.但是今天我要开始忏悔了!预计更新以下几个专题(不一定真的会 ...

  5. 2. react项目【前端】+C#【后端】从0到1

    前端 1. 删除默认的src下所有文件,替换如下文件目录 2. src/index.js:

  6. vue 中 slot 的使用方式,以及作用域插槽的用法

    分类:插槽又分为匿名插槽.具名插槽以及作用域插槽 : 匿名插槽,我们又可以叫它单个插槽或者默认插槽 因为组件标签中间是不允许写内容的,但是可以插入 插槽 :template 标签 : 插槽的使用方法 ...

  7. Android复习(四)权限—>定义自定义应用权限

    定义自定义应用权限 本文档介绍了应用开发者如何使用 Android 提供的安全功能来定义自己的权限.通过定义自定义权限,应用可以与其他应用共享其资源和功能.如需详细了解权限,请参阅权限概览. 背景 A ...

  8. KubeSphere 宣布开源 Thanos 的企业级发行版 Whizard

    日前,青云科技宣布开源 Thanos 的企业级发行版 Whizard,为企业带来真正高可用.可扩展.可存储与查询海量监控数据.易运维.安全的 Prometheus 长期存储方案. Prometheus ...

  9. 立即报名 | 云原生 + AI Meetup 成都站 11.4 正式开启

    2023 年,KubeSphere 社区已经在深圳.杭州.上海三个城市各组织了一场线下 Meetup.第四站,我们将走进天府成都. 11 月 4 日,云原生 + AI Meetup 成都站将正式开启! ...

  10. (系列八).net8 webApi后端框架轮子,欢迎下载。

    说明 该文章是属于OverallAuth2.0系列文章,每周更新一篇该系列文章(从0到1完成系统开发). 该系统文章,我会尽量说的非常详细,做到不管新手.老手都能看懂. 说明:OverallAuth2 ...