Home > Blockchain >  How to use different "right:" value in "Positioned" for an instance of a class i
How to use different "right:" value in "Positioned" for an instance of a class i

Time:11-28

I'm still learning Flutter, so I apologize for any difficulty in properly explaining what I want to do

In this code:

import 'package:flutter/material.dart';

import '../utilities/constants.dart';

class Divisions extends StatelessWidget {
  final String? image;
  final String? title;

  const Divisions({
    Key? key,
    this.image,
    this.title,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          right: -10,
          child: Image.asset(
            '${kImagesPath}stars.png',
            color: Colors.blue,
          ),
        ),
        Container(
          padding: const EdgeInsets.all(20),
          width: 145,
          height: 145,
          decoration: const BoxDecoration(
            shape: BoxShape.circle,
          ),
          child: Container(
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.transparent,
              border: Border.all(
                width: 4,
                color: Colors.blue,
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: [
                  ImageIcon(
                    size: 45,
                    AssetImage(image!),
                    color: Colors.blue,
                  ),
                  Text(
                    title!,
                    style: kDefaultFontStyle,
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
}

I'm trying to make a new "Divisions()" object in another dart file, with the same "Stack" children except for the "right:" property, I want "right:" to be different in that new "Divisions()"

Any help would be appreciated

CodePudding user response:

You can pass Right value in constructor like this:

class Divisions extends StatelessWidget {
  final String? image;
  final String? title;
  final double? right;//<--- add this

  const Divisions({
    Key? key,
    this.image,
    this.title,
    this.right,//<--- add this
  }) : super(key: key);

@override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        Positioned(
          right:right ?? (context.isPortrait ? -4 : 40),//<--- add this
          child: Image.asset(
            '${kImagesPath}stars.png',
            color: Colors.blue,
          ),
        ),
        Container(
          padding: const EdgeInsets.all(20),
          width: 145,
          height: 145,
          decoration: const BoxDecoration(
            shape: BoxShape.circle,
          ),
          child: Container(
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.transparent,
              border: Border.all(
                width: 4,
                color: Colors.blue,
              ),
            ),
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Column(
                children: [
                  ImageIcon(
                    size: 45,
                    AssetImage(image!),
                    color: Colors.blue,
                  ),
                  Text(
                    title!,
                    style: kDefaultFontStyle,
                  ),
                ],
              ),
            ),
          ),
        ),
      ],
    );
  }
}

and then when ever you want new right use this:

Divisions(
  image:'something',
  title:'something',
  right: -12,
)
  • Related