flutter - 如何使我的文本可选择并更改标题颜色?
问题描述
我想让ListView
文本可选择并让第一个文本成为默认值。我希望它看起来像 Discover 文本下方的文本:https ://cdn.dribbble.com/users/2884070/screenshots/12198574/media/cfe620d3b9b4e5f6665cb00703b92101.png
这就是我所拥有的:
https://cdn.discordapp.com/attachments/626321327791538188/725049838793654405/img1.PNG
这是我想要的部分的代码:
Widget catagoriesSection = Container(
decoration: BoxDecoration(
color: Color.fromRGBO(236, 239, 248, 1),
borderRadius: BorderRadius.only(bottomLeft: Radius.circular(45.0)),
),
padding: const EdgeInsets.fromLTRB(30, 5, 30, 0),
child: Row(
children: <Widget>[
Expanded(
child: SizedBox(
height: 350,
child: ListView(
scrollDirection: Axis.horizontal,
children: <Widget>[
_buildTravelName(categories[0]),
SizedBox(width: 40),
_buildTravelName(categories[1]),
SizedBox(width: 40),
_buildTravelName(categories[2])
])))
],
));
Widget _buildTravelName(String title) {
return GestureDetector(
onTap: () {
print("Button pressed");
},
child: Text(title,
style: GoogleFonts.ubuntu(
fontSize: 18,
fontWeight: FontWeight.w600,
color: Color.fromRGBO(139, 98, 168, .4))));
}
解决方案
创建一个变量来跟踪单击了哪个类别。我还更改了您的函数参数,以便我们可以使用索引来获取标题文本和类别列表中的位置。像下面的 fontWeight 一样应用装饰。
int _currentCat = 0;
Widget _buildTravelName(int index) {
return GestureDetector(
onTap: () {
setState(() => _currentCat = index);
print("Button pressed");
},
child: Text(categories[index],
style: GoogleFonts.ubuntu(
fontSize: 18,
fontWeight: _currentCat == index ? FontWeight.w600 : FontWeight.bold,
color: Color.fromRGBO(139, 98, 168, .4))));
推荐阅读
- python-3.x - 我不明白这个语法错误。(python 3 的新手)
- c# - 为什么不能在 C# 中设置“this”
- c - CS50 凯撒分段故障说明
- python - 熊猫变换方法执行缓慢
- redis - 切换到 Redis Enterprise 后出现上千个 TimeoutExceptions
- swift - SwiftUI @Binding 值不能改变并调用 init
- javascript - 在 Javascript 中将 ForLoop 隐藏到 ForEach
- postgresql - 将关系“类型”添加到 SQL 中的多对多表是不好的做法吗?
- python - 链接列表 - 添加新节点不起作用
- python-3.x - 如何将连续编号的数组转换为时间戳