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?

1 Answer 1

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),
      );
}
Sign up to request clarification or add additional context in comments.

3 Comments

I know this approach. I want to learn how to extend RaisedButton and do composition.
By default, you should not extend any widget, but if you have the need and still want to use that way, try reading this post stackoverflow.com/questions/51476234/…
Thank you very much. Let me wait for a couple of hours or days to accept the best answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.