flutter 如何为已创建的小部件组(类)给予参数选择?

7d7tgy0s  于 2023-01-09  发布在  Flutter
关注(0)|答案(1)|浏览(122)

我有一些stless Package 小部件:

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;
);
  }
}

这里是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: ()=>{}))
}

正确的方法是什么来使用这个"选择"的一个值?
用法:

SomeWrap(MyValue: StatusTextOrder.delivered)

现在到我的价值,我可以把任何小部件,它不是我在寻找。
我尝试使用非抽象类,并放置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"。

fiei3ece

fiei3ece1#

由于评论部分包括欲望行为,因此可以
Package 类,

//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');
  }
}

你需要用

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);
  }
}

您可以找到有关类变量和方法的更多信息

相关问题