class Reusable extends StatelessWidget {
const Reusable({
Key? key,
required this.color,
this.cardchild,
}) : super(key: key);
final Color color;
final Widget cardchild;
use:
final Color? color;
final Widget? cardchild;
Related
final Widget child;
final Colors color;
const FlexibleContainer({required this.child, Colors? this.color, Key? key}) : super(key: key);
I get error in this.color - parameter type Colors is incompatible with field type Colors
final Widget child;
final Color? color;
const NotificationHandler({required this.child, this.color, Key? key});
class colorSymbol extends StatelessWidget {
final Colors color;
colorSymbol(Colors this.color, {Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Icon(
Icons.accessibility,
color: ,
size: 64.0,
);
}
}
The code simply return a accessibility icon from Icons and I want this widget to change its color which is given by user. I created a variable from Colors class called color but I cannot move on. Thanks in advance
You like to have Color instead of Colors.
class colorSymbol extends StatelessWidget {
final Color color;
const colorSymbol(this.color, {super.key}) ;
You should change your parameter type from Colors to Color :
import 'package:flutter/material.dart';
class colorSymbol extends StatelessWidget {
final Color color;
colorSymbol(Color this.color, {Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Icon(
Icons.accessibility,
color: color,
size: 64.0,
);
}
}
I'm trying to improve myself to write better quality code. However, I have a question. For example, does it make sense to create an appbar class like the one below and call that code where I need it? Will it benefit me? Does it provide a performance advantage? Or should I call and use the appbar widget wherever I want instead?
import 'package:flutter/material.dart';
class MyAppBar extends StatelessWidget with PreferredSizeWidget {
final Widget? leading;
final Widget? title;
final List<Widget>? actions;
final bool? centerTitle;
final PreferredSizeWidget? bottom;
final Color? backgroundColor;
final TextStyle? titleTextStyle;
const MyAppBar(
{super.key,
this.leading,
this.title,
this.actions,
this.centerTitle,
this.bottom,
this.backgroundColor,
this.titleTextStyle});
#override
AppBar build(BuildContext context) {
return AppBar(
leading: leading,
title: title,
actions: actions,
centerTitle: centerTitle,
bottom: bottom,
backgroundColor: backgroundColor,
titleTextStyle: titleTextStyle);
}
#override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);
}
Before null-safety, I can pass widget, if check it is null, will show other widget. On null-safety environment, is there a way if I didn't pass widget image or note?
class Message extends StatelessWidget {
const Message({
Key key,
this.image,
this.title = "",
this.subtitle = "",
this.note,
this.onTap
}) : super(key: key);
final Widget image;
final String title;
final String subtitle;
final Widget note;
final Function onTap
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
children:[
image ?? Container(),
if(note != null) note
]
)
);
}
};
Use ? if the child can be null.
class FooWidget extends StatelessWidget {
final Widget? child; // <-- Nullable, use '?'
FooWidget({
this.child,
});
// ...
}
Use required if the child can't be null.
class FooWidget extends StatelessWidget {
final Widget child; // <-- Non-nullable
FooWidget({
required this.child, // <-- Use 'required'
});
// ...
}
You can declare your image and note as nullable types. If you need to make any of the field mandatory, you can mark it as required.
class Message extends StatelessWidget {
const Message({
Key? key,
this.image,
this.title = "",
this.subtitle = "",
this.note,
required this.onTap,
}) : super(key: key);
final Widget? image;
final String title;
final String subtitle;
final Widget? note;
final VoidCallback onTap;
#override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Column(
children:[
image ?? Container(),
note ?? Container(),
]
)
);
}
}
I want to show the image and the video in the common widget, Are there any common widget in the flutter to show image and the video.
I don't think there are any such widgets in-built flutter.
You can create a custom widget.PhotoVideoViewWidget as shown in the code.
class PhotoVideoViewWidget extends StatelessWidget {
final String type;
final String url;
const PhotoVideoViewWidget({Key key, this.type, this.url}) : super(key: key);
#override
Widget build(BuildContext context) {
return type == "video"
? VideoViewWidget(
videoUrl: url,
)
: PhotoViewWidget(
imageUrl: url,
);
}
}
class PhotoViewWidget extends StatelessWidget {
final String imageUrl;
const PhotoViewWidget({Key key, this.imageUrl}) : super(key: key);
#override
Widget build(BuildContext context) {
return PhotoViewer(imageUrl);
}
}
class VideoViewWidget extends StatelessWidget {
final String videoUrl;
const VideoViewWidget({Key key, this.videoUrl}) : super(key: key);
#override
Widget build(BuildContext context) {
return VideoViewer(videoUrl);
}
}