我的目标是生成多个不同颜色的Stickynote,我已经构建了一个六种不同颜色的列表。我想把他们洗牌。
错误
"message":“无法在初始化器中访问实例成员'color‘。\n尝试用不同的表达式替换对实例成员的引用”,
以下是代码:
import 'dart:math';
import 'package:flutter/cupertino.dart';
class Sticky {
final String note;
List color = [
const Color(0xffB85252),
const Color(0xffB4C6A6),
const Color(0xffF4ABC4),
const Color(0xff346751),
const Color(0xffFFC947),
const Color(0xff3282B8),
];
var colorItem = (color.toList()..shuffle()).first;
Sticky({required this.note, required this.color});
}
class StickyNote extends StatefulWidget {
final color;
final String note;
const StickyNote({Key? key, this.color, required this.note})
: super(key: key);
@override
_StickyNoteState createState() => _StickyNoteState();
}
class _StickyNoteState extends State<StickyNote> {
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 300,
color: Sticky.colorItem,
);
}
}发布于 2021-11-15 14:57:14
这看起来像一个非常糟糕的省道代码,对我来说没有任何意义,但我认为它只是你代码的一部分。
这里有几个错误,我将在下面进行总结,而且我还假设这段代码是部分的,您在示例中所做的使用是没有意义的:
required
class StickyNote extends StatefulWidget {
final Color color;
final String note;
const StickyNote({Key? key, required this.color, required this.note})
: super(key: key);您使用类Sticky作为静态类,但任何适当性都是静态的。
一个可行的解决方案如下
import 'dart:math';
import 'package:flutter/cupertino.dart';
class Sticky {
final String note;
static List color = [
const Color(0xffB85252),
const Color(0xffB4C6A6),
const Color(0xffF4ABC4),
const Color(0xff346751),
const Color(0xffFFC947),
const Color(0xff3282B8),
];
Sticky({required this.note});
static Color getColorItem() => (color.toList()..shuffle()).first;
}
class StickyNote extends StatefulWidget {
final Color color;
final String note;
const StickyNote({Key? key, required this.color, required this.note})
: super(key: key);
@override
_StickyNoteState createState() => _StickyNoteState();
}
class _StickyNoteState extends State<StickyNote> {
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 300,
color: Sticky.colorItem(),
);
}
}此外,如果您的颜色没有改变,那么使用这段代码会更有意义,否则,您需要使用对象而不是类
发布于 2021-11-15 15:02:05
您可能想试试这个:
import 'dart:math';
import 'package:flutter/cupertino.dart';
class StickyColors {
static final List colors = [
const Color(0xffB85252),
const Color(0xffB4C6A6),
const Color(0xffF4ABC4),
const Color(0xff346751),
const Color(0xffFFC947),
const Color(0xff3282B8),
];
}
class StickyNote extends StatefulWidget {
final String note;
const StickyNote({Key? key, required this.note})
: super(key: key);
@override
_StickyNoteState createState() => _StickyNoteState();
}
class _StickyNoteState extends State<StickyNote> {
final _random = Random();
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 300,
color: StickyColors.colors[_random.nextInt(6)],
);
}
}我并没有真正测试它,但我的应用程序中有类似的东西。
发布于 2021-11-15 15:54:53
如果你只是想要一个随机的粘性笔记颜色,没有任何状态逻辑。您还可以将其转换为一个可以轻松重用的statelesswidget:
class StickNote extends StatelessWidget {
final List colors = [
const Color(0xffB85252),
const Color(0xffB4C6A6),
const Color(0xffF4ABC4),
const Color(0xff346751),
const Color(0xffFFC947),
const Color(0xff3282B8),
];
final _random = Random();
@override
Widget build(BuildContext context) {
return Container(
width: 300,
height: 300,
color: colors[_random.nextInt(6)],
);
}
}https://stackoverflow.com/questions/69976263
复制相似问题