gpt4 book ai didi

flutter - 如何使用SizedBox在其自己的类中包装派生的RaisedButton对象?

转载 作者:行者123 更新时间:2023-12-03 04:32:47 25 4
gpt4 key购买 nike

我有一个从MyButton派生的RaisedButton类,如下所示。

class MyButton extends RaisedButton {
final double height;

MyButton({
String text,
VoidCallback onPressed,
this.height,
}) : super(child: Text(text), onPressed: onPressed);

@override
Widget build(BuildContext context) => SizedBox(
height: height,
child: this,
);
}
我在 main方法中使用它,如下所示。
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('Hello World'),
),
body: Center(
child: MyButton(text: 'Press Me', height: 80, onPressed: () {}),
),
),
);
}
}
产生空白页并出现一些错误

The following StackOverflowError was thrown building MyButton:


enter image description here



如何在自己的类中将派生的 RaisedButtonSizedBox包装在一起?

最佳答案

为什么要扩展RaisedButton?
另一种方法是简单地创建自定义窗口小部件:

class MyButton {
final double height;
final String text;
final VoidCallback onPressed;

MyButton({
this.text,
this.onPressed,
this.height,
});

@override
Widget build(BuildContext context) => SizedBox(
height: height,
child: RaisedButton(child: Text(text), onPressed: onPressed),
);
}

关于flutter - 如何使用SizedBox在其自己的类中包装派生的RaisedButton对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64661497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com