Home > database >  how to give a choice for a parameter with an already created widget group(class)?
how to give a choice for a parameter with an already created widget group(class)?

Time:01-09

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 or Widget , 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'.

CodePudding user response:

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

  • Related