如何为已创建的小部件组(类)提供一个参数选择?

huangapple go评论65阅读模式
英文:

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或Widget放入其中,但所有这些都给我带来了错误。

有人说这样会起作用:

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 &quot;choices&quot; 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&lt;Widget&gt; or Widget&lt;StatusTextOrder&gt; , 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 &#39;StatusTextOrder&#39; can&#39;t be assigned to the parameter type &#39;Widget?&#39;.
The argument type &#39;Text&#39; can&#39;t be assigned to the parameter type &#39;StatusTextOrder&#39;.


</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(&#39;processing&#39;);
  }
}

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(&#39;processing&#39;);
  }
}

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(&#39;Processing&#39;);
  static Text delivered = Text(&#39;Delivered&#39;);
 static IconButton canceled = IconButton(icon: Text(&#39;Canceled&#39;), onPressed: () =&gt; {});
}

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

huangapple
  • 本文由 发表于 2023年1月9日 04:36:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75051094.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定