英文:
how to give a choice for a parameter with an already created widget group(class)?
问题
我有一些无状态的包装小部件:
```dart
class SomeWrap extends StatelessWidget {
// 它工作正常,但我可以放置任何小部件,但我只想要来自MyChoises类的小部件
Widget MyValue;
const SomeWrap({
required this.MyValue,
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
// 这里是UI的一些设置
// 这里是我的值作为小部件
child: MyValue,
);
}
}
这里有一个类MyChoises,它返回了一些小部件:
abstract class StatusTextOrder {
Text processing = Text('Processing', style: TextStyle(color: Colors.yellow));
Text delivered = Text('Delivered', style: TextStyle(color: Colors.green));
IconButton canceled = IconButton(icon: Text('Canceled', onPressed: () => {}))
}
如何正确使用这些"choices"作为值?
用法:
SomeWrap(MyValue: StatusTextOrder.delivered)
现在,我可以放置任何小部件到MyValue中,这不是我要找的。
我尝试使用非抽象类,并将StatusTextOrder
有人说这样会起作用:
import 'package:flutter/material.dart';
class StatusTextOrder {
static final processing =
Text('Processing', style: TextStyle(color: Colors.yellow));
static final delivered =
Text('Delivered', style: TextStyle(color: Colors.green));
static final canceled = IconButton(
onPressed: () {},
icon: Icon(
Icons.cancel,
color: Colors.red,
));
}
class Wrapper extends StatelessWidget {
StatusTextOrder widget;
Wrapper({Key? key, required this.widget}) : super(key: key);
@override
Widget build(BuildContext context) {
return Placeholder(
child: widget,
);
}
}
class ErrorPage extends StatelessWidget {
const ErrorPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child:Row(
children: [
Wrapper(widget: StatusTextOrder.delivered),
],
)),
);
}
}
不,它会产生错误:
无法将参数类型'StatusTextOrder'分配给参数类型'Widget?'。
无法将参数类型'Text'分配给参数类型'StatusTextOrder'。
<details>
<summary>英文:</summary>
I have some some stless wrap widget:
class SomeWrap extends StatelessWidget {
// it's work, but i can put any widget, but I want only widget form class MyChoises
Widget MyValue
const SomeWrap({
required this.MyValue,
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(
// Here some settings for UI
// here my value as a widget
child: MyValue;
);
}
}
Here class MyChoises, that return couple widgets:
abstract class StatusTextOrder {
Text processing = Text('Processing',style: TextStyle(color:Colors.Yellow)));
Text delivered = Text('Delivered',style: TextStyle(color:Colors.Green)));
IconButton canceled = IconButton(icon: Text('Canceled', onPressed: ()=>{}))
}
What the correct way to use this "choices" for a value ?
usage:
SomeWrap(MyValue: StatusTextOrder.delivered)
Now into MyValue I can put any Widget, its not that im looking for.
I tried to use none abstract class, and put StatusTextOrder<Widget> or Widget<StatusTextOrder> , but all of this gives me an errors.
Someone said that it will work :
import 'package:flutter/material.dart';
class StatusTextOrder {
static final processing =
Text('Processing', style: TextStyle(color: Colors.yellow));
static final delivered =
Text('Delivered', style: TextStyle(color: Colors.green));
static final canceled = IconButton(
onPressed: () {},
icon: Icon(
Icons.cancel,
color: Colors.red,
));
}
class Wrapper extends StatelessWidget {
StatusTextOrder widget;
Wrapper({Key? key, required this.widget}) : super(key: key);
@override
Widget build(BuildContext context) {
return Placeholder(
child: widget,
);
}
}
class ErrorPage extends StatelessWidget {
const ErrorPage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(child:Row(
children: [
Wrapper(widget: StatusTextOrder.delivered),
],
)),
);
}
}
no, it gives errors:
The argument type 'StatusTextOrder' can't be assigned to the parameter type 'Widget?'.
The argument type 'Text' can't be assigned to the parameter type 'StatusTextOrder'.
</details>
# 答案1
**得分**: 0
以下是您要翻译的部分:
```dart
作为注释部分包括所需行为,它可以是包装器类,
//包装器类
class SomeWrap extends StatelessWidget {
final StatusTextOrder statusTextOrder;
const SomeWrap({required this.statusTextOrder, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return statusTextOrder;
}
}
//基类
abstract class StatusTextOrder extends Widget {
const StatusTextOrder({super.key});
}
/// 具体类
class Processing extends StatelessWidget implements StatusTextOrder {
const Processing({super.key});
@override
Widget build(BuildContext context) {
return const Text('processing');
}
}
并且您需要像这样使用它:
```dart
SomeWrap(statusTextOrder: Processing());
也可以是这样的:
abstract class StatusTextOrder extends StatelessWidget {
const StatusTextOrder({super.key});
}
class Processing extends StatusTextOrder {
const Processing({super.key});
@override
Widget build(BuildContext context) {
return const Text('processing');
}
}
您可以创建另一个类,其中包含这些具体类作为静态变量,并像旧部分一样传递。
旧部分:
要像StatusTextOrder.delivered
这样使用,您需要将这些变量设为静态变量,
abstract class StatusTextOrder {
static Text processing = Text('Processing');
static Text delivered = Text('Delivered');
static IconButton canceled = IconButton(icon: Text('Canceled'), onPressed: () => {});
}
class SomeWrap extends StatelessWidget {
final Widget MyValue;
const SomeWrap({required this.MyValue, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(child: MyValue);
}
}
您可以在class-variables-and-methods中找到更多信息。
英文:
As comment section included desire behavior, It can be
wrapper class,
//wrapper class
class SomeWrap extends StatelessWidget {
final StatusTextOrder statusTextOrder;
const SomeWrap({required this.statusTextOrder, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return statusTextOrder;
}
}
//base class
abstract class StatusTextOrder extends Widget {
const StatusTextOrder({super.key});
}
/// concrete class
class Processing extends StatelessWidget implements StatusTextOrder {
const Processing({super.key});
@override
Widget build(BuildContext context) {
return const Text('processing');
}
}
And you need to use like
SomeWrap(statusTextOrder: Processing());
Also it can be
abstract class StatusTextOrder extends StatelessWidget {
const StatusTextOrder({super.key});
}
class Processing extends StatusTextOrder {
const Processing({super.key});
@override
Widget build(BuildContext context) {
return const Text('processing');
}
}
You can create another class with theses concrete Class as static variable and pass like old part.
old:
To use like StatusTextOrder.delivered
, you need to make those variable as statics,
abstract class StatusTextOrder {
static Text processing = Text('Processing');
static Text delivered = Text('Delivered');
static IconButton canceled = IconButton(icon: Text('Canceled'), onPressed: () => {});
}
class SomeWrap extends StatelessWidget {
final Widget MyValue;
const SomeWrap({required this.MyValue, Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Container(child: MyValue);
}
}
You can find more about class-variables-and-methods
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论