如何在Flutter中为类的示例使用“Positioned”中的不同“right:“值?

k4aesqcs  于 2022-11-30  发布在  Flutter
关注(0)|答案(1)|浏览(137)

我还在学习Flutter,所以我很抱歉在正确解释我想做什么时遇到任何困难
在此程式码中:

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

我试图在另一个dart文件中创建一个新的“Divisions()”对象,除了“right:“属性外,它具有相同的“Stack”子对象,我希望“right:“在新的“Divisions()”中是不同的
任何帮助都将不胜感激

5f0d552i

5f0d552i1#

可以在构造函数中传递Right值,如下所示:

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

然后当你想要新的right时,使用这个:

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

相关问题