Flutter 参数类型';图像';can';不能分配给参数类型';字符串';。参数类型不可分配
我试图将图像添加到自定义的Flutter 参数类型';图像';can';不能分配给参数类型';字符串';。参数类型不可分配,flutter,dart,Flutter,Dart,我试图将图像添加到自定义的单选按钮,但发现以下错误: The argument type 'Image' can't be assigned to the parameter type 'String'.dartargument_type_not_assignable 如以下行中的错误所示: value: Image.asset('assets/images/tower_ic.png') 这是下面表示的值: final T value; 下面是TValue类的代码 /// Copyrigh
单选按钮
,但发现以下错误:
The argument type 'Image' can't be assigned to the parameter type 'String'.dartargument_type_not_assignable
如以下行中的错误所示:
value: Image.asset('assets/images/tower_ic.png')
这是下面表示的值:
final T value;
下面是T
Value类的代码
/// Copyright (c) 2018 Conrad Heidebrecht. All rights reserved.
/// Licensed under the MIT license. See LICENSE file in the project root for details.
library custom_radio;
import 'package:flutter/material.dart';
typedef AnimationsBuilder<T> = List<Animation<T>> Function(AnimationController);
typedef RadioBuilder<T, U> = Widget Function(BuildContext context, List<U> animValues, Function updateState, T value);
/// A custom radio widget.
///
/// Used to select between a number of mutually exclusive values. When one radio
/// widget in a group is selected, the other radios in the group cease to
/// be selected. The values are of type `T`, the first type parameter of the [CustomRadio]
/// class. Enums are commonly used for this purpose.
///
/// Animation values are of type `U`, the second type parameter of the [CustomRadio] class,
/// this allows for stronger typing if only one type of animation is required. This can
/// be set to `dynamic` if more than one type of animation is required.
class CustomRadio<T, U> extends StatefulWidget {
/// Builds the radio button with animation state.
///
/// [BuildContext] context into which to build,
/// [List<U>] animValues (current values of running animations),
/// [Function] updateState (call to manually update the state of the widget),
/// [T] copy of radio value of the widget
final RadioBuilder<T, U> builder;
/// The duration of the animation controller
final Duration duration;
/// Returns the list of child animations whose values will be passed to the builder.
/// Called on initState.
final AnimationsBuilder<U> animsBuilder;
/// The value represented by this radio button.
final T value;
/// The currently selected value for this group of radio buttons.
///
/// This radio button is considered selected if its [value] matches the
/// [groupValue].
final T groupValue;
bool get checked => value == groupValue;
/// Creates a custom radio widget.
///
/// The widget itself does not maintain any state. Instead, it is up to
/// the user to rebuild the radio widget when [groupValue] changes.
/// The widget will automatically update its animation controller when
/// it detects a change.
///
/// If no [animsBuilder] is passed, the widget will switch between selected
/// states with no animation and the [animValues] passed to [builder] will be a
/// list with the only element being whether the widget is checked or not.
///
/// The following arguments are required:
///
/// * [value] and [groupValue] together determine whether the radio button
/// is selected.
/// * [builder] creates the visual layout of the widget.
CustomRadio({
Key key,
this.animsBuilder,
this.duration = const Duration(milliseconds: 600),
@required this.builder,
@required this.value,
@required this.groupValue,
}) : assert(duration != null),
super(key: key);
@override
State<CustomRadio> createState() => _CustomRadioState<T, U>();
}
class _CustomRadioState<T, U> extends State<CustomRadio<T, U>>
with SingleTickerProviderStateMixin {
AnimationController _controller;
List<Animation> _animations;
@override
void initState() {
super.initState();
_controller = AnimationController(duration: widget.duration, vsync: this);
_animations = widget.animsBuilder(_controller);
_animations.forEach((anim) => anim.addListener(() => setState(() {})));
if (widget.checked)
_controller.value = 1.0;
else
_controller.value = 0.0;
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
void _updateState() {
setState(() {
if (widget.checked && _controller.status != AnimationStatus.completed) {
_controller.forward();
} else {
_controller.reverse();
}
});
}
@override
Widget build(BuildContext context) {
if ((widget.checked &&
(_controller.status == AnimationStatus.dismissed ||
_controller.status == AnimationStatus.reverse)) ||
(!widget.checked &&
(_controller.status == AnimationStatus.completed ||
_controller.status == AnimationStatus.forward))) {
_updateState();
}
final anims = _animations.map<U>((anim) => anim.value).toList();
return widget.builder(
context,
anims.length > 0 ? anims : [widget.checked].cast<dynamic>(),
_updateState,
widget.value,
);
}
}
还有这个
CustomRadio({
Key key,
this.animsBuilder,
this.duration = const Duration(milliseconds: 600),
@required this.builder,
@required this.value,
@required this.groupValue,
}) : assert(duration != null),
super(key: key);
这是我用这个字符串的部分:
CustomRadio<String, dynamic>(
value: 'Sim 2',
groupValue: widget.radioValue,
animsBuilder: (AnimationController controller) => [
CurvedAnimation(parent: controller, curve: Curves.easeInOut),
ColorTween(begin: Colors.grey.withOpacity(0.50), end: Colors.green.withOpacity(0.50))
.animate(controller),
ColorTween(begin: Colors.grey.withOpacity(0.50), end: Colors.green.withOpacity(0.50))
.animate(controller),
],
builder: dynamicBuilder,
),
CustomRadio(
值:“Sim 2”,
groupValue:widget.radioValue,
animsBuilder:(AnimationController)=>[
曲线动画(父对象:控制器,曲线:Curves.easeInOut),
ColorTween(开始:Colors.grey.withOpacity(0.50),结束:Colors.green.withOpacity(0.50))
.设置(控制器)动画,
ColorTween(开始:Colors.grey.withOpacity(0.50),结束:Colors.green.withOpacity(0.50))
.设置(控制器)动画,
],
生成器:dynamicBuilder,
),
那么我该如何解决这个问题:)
#编辑
请查看下图:
假设这是您的自定义按钮类:
class RadButton<T> {
final T value;
RadButton({this.value});
}
class按钮{
最终T值;
RadButton({this.value});
}
现在,这项工作没有任何错误:
RadButton<Image>(value: Image.asset('something_here'));
RadButton(值:Image.asset('something_here');
显示您声明了T值的类代码
@iLoveDocs我想我现在正在尝试解决它,我已经从T
类型更改为ImageIcon
类型,但我仍然被卡住:)CustomRadio(builder:null,value:Image.asset('something'),groupValue:null)代码>尝试此操作。请发布并突出显示引发此错误的代码行。很抱歉,您的错误消息与带下划线的行不匹配。请你退一步,写一篇文章,用当前和当前的错误信息概括你的问题,好吗?当我们在做这件事的时候,请加上你不理解的错误,因为它清楚地说明了问题所在。你能检查一下我的编辑吗?我想现在更清楚了:DI还加了相关的一行:)
RadButton<Image>(value: Image.asset('something_here'));