【发布时间】:2022-01-25 03:32:58
【问题描述】:
老实说,我是 Flutter 和开发的新手。我一直在尝试让每个按钮在点击时改变颜色;它确实有效,但我遇到的问题是按钮占用了所有水平空间。我尝试并寻找可行的方法。有没有办法改变宽度?
看起来像这样:https://ibb.co/rFNfSf4
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<bool> isSelected = [false, false, false, false];
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
backgroundColor: Colors.grey[850],
appBar: AppBar(
title: Text(
"A Sample Quiz",
style: TextStyle(
letterSpacing: 1.3,
fontFamily: "SourceCodePro",
),
),
centerTitle: true,
backgroundColor: Colors.grey[900],
elevation: 2.0,
),
body: Center(
child: Column(
children: <Widget>[
SizedBox(
height: 20.0,
),
Text(
"Choose all that apply:",
style: TextStyle(
fontFamily: "NotoSans",
letterSpacing: 1.2,
fontSize: 18.0,
color: Colors.white,
),
),
SizedBox(
height: 9.0,
),
answerButtonBoxes("Answer"),
],
),
),
),
);
}
Column answerButtonBoxes(String label) {
return Column(
children: [
ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: isSelected.length,
itemBuilder: (BuildContext context, int index) {
return ElevatedButton(
onPressed: () {
setState(() => isSelected[index] = !isSelected[index]);
},
child: Text(
"$label ${index + 1}",
style: TextStyle(fontFamily: "NotoSans", fontSize: 15.0),
),
style: ElevatedButton.styleFrom(
primary:
isSelected[index] ? Colors.blue : Colors.transparent,
elevation: 0.0,
side: BorderSide(
color: Colors.white,
width: isSelected[index] ? 0 : 1,
style: isSelected[index]
? BorderStyle.none
: BorderStyle.solid,
),
),
);
}),
],
);
}
}
【问题讨论】:
-
固定宽度的按钮是否需要根据文字的宽度?
-
@Diwyansh 我不需要特定的宽度,但我不希望它与页面一样宽。
-
检查我的答案一次。
标签: flutter button flutter-layout