0

I have a class MyButton derived from RaisedButton as follows.

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,
      );
}

I use it in the main method as follows.

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: () {}),
        ),
      ),
    );
  }
}

It produces a blank white page with some errors

The following StackOverflowError was thrown building MyButton:

enter image description here

Question

How to wrap the derived RaisedButton with SizedBox in its own class?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165

1 Answers1

1

Why do you want to extend RaisedButton?

Another way is to simply create a custom widget:

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),
      );
}
Farhan Syah
  • 753
  • 3
  • 14