如何在 "Positioned" Flutter 中为类的实例使用不同的"right:"值?



我还在学习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,
),
],
),
),
),
),
],
);
}
}

我想做一个新的"division ()"对象在另一个dart文件中,具有相同的"堆栈"。儿童除"权利"外:"权利";财产,我想要"权利"在新的"事业部()"中有所不同

如有任何帮助,不胜感激

您可以像这样在构造函数中传递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,
)

最新更新