我使用了一個包
uj5u.com熱心網友回復:
您可以使用按鈕文本串列并保留selectedIndex.
在dartPad 上運行
int? _selectedValueIndex;
List<String> buttonText = ["ForSale", "For rent"];
Widget button({required String text, required int index}) {
return InkWell(
splashColor: Colors.cyanAccent,
onTap: () {
setState(() {
_selectedValueIndex = index;
});
},
child: Container(
padding: const EdgeInsets.all(12),
color: index == _selectedValueIndex ? Colors.blue : Colors.white,
child: Text(
text,
style: TextStyle(
color: index == _selectedValueIndex ? Colors.white : Colors.black,
),
),
),
);
}
內部構建方法使用這個,
Row(
children: [
...List.generate(
buttonText.length,
(index) => button(
index: index,
text: buttonText[index],
),
)
],
),
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/421016.html
標籤:
上一篇:當串列包含特定字串時顯示影像
