Flutter开发Todo App时如何设置Card内Text为白色
待办卡片文字颜色配置方法
可通过两种稳定生效的方式实现白色待办文字效果,不存在样式继承失效的问题:
- 最简便的方式:直接给承载待办文字的
Text组件单独设置文字样式。该方式不依赖父组件的主题继承规则,跨Flutter版本均可稳定生效。 - 批量适配方式:如果需要统一调整当前页面所有待办卡片的文字样式,可在外层组件配置
ListTileTheme的默认文字颜色,减少重复代码。
你只需要在现有代码的ListTile标题位置追加样式配置即可,修改后的完整代码如下:
Widget _buildTodoList() { return ListView.builder( itemBuilder: (context, index) { if (index < _todoItems.length) { final item = _todoItems[index]; return Dismissible( key: Key(item), onDismissed: (direction) { setState(() { _todoItems.removeAt(index); }); Scaffold.of(context).showSnackBar( SnackBar(content: Text("$item 已被删除"))); }, background: Container(color: Colors.red), child: Container( margin: const EdgeInsets.all(7.0), child: Card( child: ListTile( title: Text( "$item", style: TextStyle(color: Colors.white), ), ), color: Colors.blueGrey[900], shape: RoundedRectangleBorder( borderRadius: BorderRadius.circular(10.0), ))), ); } }, ); }
后续如果需要调整文字字号、字重等属性,直接在上述TextStyle配置项中追加参数即可。
内容的提问来源于stack exchange,提问作者elios_cama
相关产品推荐
相关产品推荐

