Keal Mlj
Keal Mlj

Reputation: 103

Flutter : create a custom reusable widget image

I try to create a reusable widget but some error happens...

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

class CustomLogo extends StatelessWidget {

  @override
  Widget showLogo(var nameImage, double radiusImage, double LeftPadding) {
    return new Hero(
      tag: 'hero',
      child: Padding(
        padding: EdgeInsets.fromLTRB(LeftPadding, 70.0, 0.0, 0.0),
        child: CircleAvatar(
          backgroundColor: Colors.transparent,
          radius: radiusImage,
          child: Image.asset('assets/' + name),
        ),
      ),
    );
  }
}

And I don't understand override and the "construction" of the widget, how I can use var in the widget

Upvotes: 1

Views: 1797

Answers (1)

Viren V Varasadiya
Viren V Varasadiya

Reputation: 27177

You have to create a constructor to get values from where you are trying to call.

In following way you can create separate widget and pass arguments.

Moreover, here one mistake is left and it is hero tag. you are setting constant hero tag, which is fine if you are calling this widget once in a screen. if you are using this widget twice then it will not work because two hero's can’t have same tag in one screen. so, i also suggest you. to assign tag dynamically.

class CustomLogo extends StatelessWidget {
  final nameImage;
  final radiusImage;
  final leftPadding;

  CustomLogo({this.leftPadding, this.nameImage, this.radiusImage});
  @override
  Widget build(BuildContext context) {
    return new Hero(
      tag: 'hero',
      child: Padding(
        padding: EdgeInsets.fromLTRB(leftPadding, 70.0, 0.0, 0.0),
        child: CircleAvatar(
          backgroundColor: Colors.transparent,
          radius: radiusImage,
          child: Image.asset('assets/' + nameImage),
        ),
      ),
    );
  }
}

How you can call or use this widget.

CustomLogo(leftPadding: 10,radiusImage: 5,nameImage: "hello",)

Upvotes: 3

Related Questions