【发布时间】:2020-05-08 17:20:07
【问题描述】:
我正在尝试将自定义容器(具有背景颜色、标题和 onPressed 属性)传递到另一个自定义小部件,该小部件创建一行三个这些容器。目标是能够在第二个小部件中为每个按钮输入标题,例如 TriButton(title1, title2, title3)。任何提示或技巧将不胜感激!
自定义容器
class RectButton extends StatelessWidget {
RectButton({this.buttonChild, this.bgColor, this.onPress});
final Widget buttonChild;
final Color bgColor;
final Function onPress;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onPress,
child: Container(
constraints: BoxConstraints.expand(width: 100, height: 50),
child: Center(child: buttonChild),
decoration: BoxDecoration(
color: bgColor,
shape: BoxShape.rectangle,
border: Border.all(width: 1, color: Colors.white)),
padding: EdgeInsets.fromLTRB(12, 12, 12, 12),
),
);
}
}
`Tri-button code`
enum Weight {
ideal,
actual,
adjusted,
}
class TriButton extends StatefulWidget {
TriButton({this.title1, this.title2, this.title3, this.buttonChild});
final Text title1;
final Text title2;
final Text title3;
final RectButton buttonChild;
@override
_TriButtonState createState() => _TriButtonState();
}
class _TriButtonState extends State<TriButton> {
Weight selectedWeight;
@override
Widget build(BuildContext context) {
return Center(
child: Container(
constraints: BoxConstraints(maxWidth: 300),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Expanded(
child: RectButton(
buttonChild: GOAL TO ENTER TITLE HERE,
onPress: () {
setState(() {
selectedWeight = Weight.adjusted;
});
},
bgColor: selectedWeight == Weight.adjusted
? Colors.orange[600]
: Colors.grey[600],
),
),
【问题讨论】:
-
你不能在你的行中再添加两个 RectButtons 吗?
-
我确实有两个其他按钮(在上面的示例中将它们关闭以节省空间)但是当我将 RectButton 实现到 TriButton 类中时,它不会让我插入标题。我不知道如何为 RectButton 授予属性,以便我可以在 TriButton 中自定义它
标签: flutter enums widget final