Access Values From Model In Flutter - string

Im not able to fetch these values and Display.
This is my Model Class
enum ConnectionStatus {
connected,
addNewProduct,
failed;
}
enum IsOnlineOfflineStatus {
online,
offline;
}
class ConnectionStatusY {
String statusTitle;
IsOnlineOfflineStatus statusIndicatorDot;
IsOnlineOfflineStatus status;
String dateTime;
ConnectionStatus notificationBackgroundColor;
ConnectionStatusY({
required this.statusTitle,
required this.statusIndicatorDot,
required this.status,
required this.dateTime,
required this.notificationBackgroundColor,
});
Color getNotificationBarStatus() {
ConnectionStatus connectionStatus = ConnectionStatus.connected;
switch (connectionStatus) {
case ConnectionStatus.connected:
return Colors.white;
case ConnectionStatus.addNewProduct:
return Colors.blue;
case ConnectionStatus.failed:
return Colors.red;
}
}
String getOnlineOfflineIndicator() {
IsOnlineOfflineStatus isOnlineOfflineStatus = IsOnlineOfflineStatus.offline;
switch (isOnlineOfflineStatus) {
case IsOnlineOfflineStatus.online:
return 'Online';
case IsOnlineOfflineStatus.offline:
return 'Offline';
}
}
Color getNotificationDotStatusDot() {
IsOnlineOfflineStatus isOnlineOfflineStatus = IsOnlineOfflineStatus.offline;
switch (isOnlineOfflineStatus) {
case IsOnlineOfflineStatus.online:
return AppColors.statusColor;
case IsOnlineOfflineStatus.offline:
return Colors.red;
}
}
}
So in this StatefulWidget
class NotificationBar extends StatefulWidget {
const NotificationBar({Key? key}) : super(key: key);
#override
State<NotificationBar> createState() => _NotificationBarState();
}
class _NotificationBarState extends State<NotificationBar> {
ConnectionStatusY connectionStatusY = ConnectionStatusY(
statusTitle: StringConstants.statusTitle,
statusIndicatorDot: IsOnlineOfflineStatus.online,
status: IsOnlineOfflineStatus.online,
dateTime: AppDateAndTime().getDateAndTime(),
notificationBackgroundColor: ConnectionStatus.connected,
);
}
So, how can I access those values from my Model Class to this StatefulWidget
For example
status: IsOnlineOfflineStatus.online,
Like I want to change to Offline and Online Status, which is present in getOnlineOfflineIndicator() String, To Display in Text Widget.

Related

flutter Equatable with GetxController

I found an excellent way to make a filter using both Equatable and Block
But I want to convert it to Get. I tried a lot for days, but I couldn't..Can I get help converting it to Get, please ?
The filter method consists of three classes, the first FilterEvent :
abstract class FilterEvent extends Equatable {
const FilterEvent();
#override
List<Object> get props => [];
}
class FilterLoad extends FilterEvent {
#override
List<Object> get props => [];
}
class LocationFilterUpdated extends FilterEvent {
final LocationFilter locationFilter;
const LocationFilterUpdated({required this.locationFilter});
#override
List<Object> get props => [locationFilter];
}
Second FilterState :
class FilterState extends Equatable {
const FilterState();
#override
List<Object> get props => [];
}
class FilterLoading extends FilterState {}
class FilterLoaded extends FilterState {
final Filter filter;
const FilterLoaded({this.filter = const Filter()});
#override
List<Object> get props => [filter];
}
Finally Block:
class FilterBlock extends Block<FilterEvent, FilterState> {
FilterBlock() : super(FilterLoading());
#override
Stream<FilterState> mapEventToState(FilterEvent event) async* {
if (event is FilterLoad) {
yield* _mapFilterLoadToState();
}
if(event is LocationFilterUpdated){
yield* _mapLocationFilterUpdated(event, state)
}
}
Stream<FilterState> _mapFilterLoadToState() async*{
yield* FilterLoaded(filter: Filter(locationsFilter: updatedLocationFilters));
}
Stream<FilterState> _mapLocationFilterUpdated(LocationFilterUpdated event , FilterState state) async*{
if(state is FilterLoaded){
final List<LocationFilter> updatedLocationFilters =
state.filter.locationsFilter.map((locationFilter) {
return locationFilter.id == event.locationFilter.id
? event.locationFilter
: locationFilter;
}).toList();
FilterLoaded(filter: Filter(locationsFilter: updatedLocationFilters));
}
}
}

Call flutter widgets dynamically by their name (with a string)?

I have several custom widgets which alle accept the same data type as a parameter:
class Widget1 extends StatelessWidget {
Widget1({#required this.data});
final data;
}
class Widget2 extends StatelessWidget {
Widget2({#required this.data});
final data;
}
...
I have another widget which acts like a widget manager:
class WidgetManager extends StatelessWidget {
final data;
final type;
WidgetManager({this.data, this.type});
#override
Widget build(BuildContext context) {
if (this.type == 'Widget1') {
return Widget1(data: this.data);
} elseif (this.type == 'Widget2') {
return Widget2(data: this.data);
}
}
}
In PHP I could call the widgets with a string like this:
$widget_name = 'Widget1';
$widget = new $widget_name($data);
Is there any way to do a similar thing in flutter?
Like return this.type(data: this.data);?

How to pass a string value from a class to another in the same activity in Flutter?

// I cant find a way to pass a string from a class to another. They are both in the same screen and i want to pass the index from a pageview on class to another. Any help would be grateful!
//on the first class//
onPageChanged: (int index) {
_sendDataToAnotherClass(context, index);
_currentPageNotifier.value = index;
}),
),
);
}
void _sendDataToAnotherClass(BuildContext context, int index) {
final String texttosend = index.toString();
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return new personaname(text: texttosend);
}
));
}
//on the second class//
final String text ;
personaname({Key key, #required this.text}) : super(key: key);
if your personaname is a class (just fyi if that is indeed a class the proper format is to capitalize the first letters of each word - PersonaName), and that class is a stateful widget you could pass the data down to it and do a call within the state to override the didUpdateWidget method to setState on data changes. This would mean you don't need the value notifier.
Something like this might do what you're looking for:
class PageViewHolder extends StatelessWidget{
PersonaName name = PersonaName(text: 'text',);
#override
Widget build(BuildContext context) {
return PageView(
onPageChanged: (p){
var textToSend = 'do logic to decide name';
name.text = textToSend;
},
);
}
}
class PersonaName extends StatefulWidget{
String text;
PersonaName({Key key, this.text}) : super(key: key);
#override
State<StatefulWidget> createState() =>_PersonaName();
}
class _PersonaName extends State<PersonaName>{
#override
Widget build(BuildContext context) {
return Text(widget.text);
}
#override
void didUpdateWidget(PersonaName oldWidget) {
super.didUpdateWidget(oldWidget);
if(widget.text != oldWidget.text){
setState(() {});
}
}
}

Ignore member of base class in YamlDotNet

I have a class which I want to serialize with YamlDotNet:
public class AwesomeClass : PropertyChangedBase
{
private bool _element1;
private bool _enabled;
public bool Element1
{
get { return _element1; }
set
{
_element1 = value;
NotifyOfPropertyChange(() => Element1);
}
}
public bool Enabled
{
get { return _enabled; }
set
{
_enabled = value;
NotifyOfPropertyChange(() => Enabled);
}
}
}
My problem is, in the base class is an element named: IsNotifying
Is there a way to exclude this element from serialization, without the change of the base class?
You could override the property in the derived class and apply the YamlIgnore attribute there. While the sample below works, I suspect for more complicated class hierarchies you would really need to ensure no behavior changes.
public class AwesomeClass : PropertyChangedBase
{
[YamlIgnore]
public new bool IsNotifying
{
get { return base.IsNotifying; }
set { base.IsNotifying = value; }
}
[YamlIgnore]
public override bool Blah
{
get { return base.Blah; }
set { base.Blah = value; }
}
}
public class PropertyChangedBase
{
public bool IsNotifying
{
get;
set;
}
public virtual bool Blah
{
get;
set;
}
}
I had a similar problem (needed to filter properties of a particular type from classes I couldn't change, so using the attribute was not an option) and is what I came up with:
Create a custom type inspector:
public class MyTypeInspector : TypeInspectorSkeleton
{
private readonly ITypeInspector _innerTypeDescriptor;
public MyTypeInspector(ITypeInspector innerTypeDescriptor)
{
_innerTypeDescriptor = innerTypeDescriptor;
}
public override IEnumerable<IPropertyDescriptor> GetProperties(Type type, object container)
{
var props = _innerTypeDescriptor.GetProperties(type, container);
props = props.Where(p => !(p.Type == typeof(Dictionary<string, object>) && p.Name == "extensions"));
props = props.Where(p => p.Name != "operation-id");
return props;
}
}
Create the serializer as follows:
var builder = new SerializerBuilder();
builder.WithTypeInspector(inspector => new MyTypeInspector(inspector));
var serializer = builder.Build();

Autoroute URL patterns containing custom tokens

I'm having trouble getting my custom tokens to work with my ContentPart. My problem is the same as what is described here:
Is it possible to create an orchard autoroute using contents of a custom type property?
I have created my tokens:
namespace MyNS.Types.Providers
{
public class BioPartTokens : ITokenProvider
{
public BioPartTokens() {
T = NullLocalizer.Instance;
}
public Localizer T { get; set; }
public void Describe(dynamic context) {
context.For("Bio", T("Bio"), T("Tokens for the Bio content type"))
.Token("FirstName", T("FirstName"), T("First name of person."))
.Token("LastName", T("LastName"), T("Last name of person."));
}
public void Evaluate(dynamic context) {
context.For<BioPart>("Bio")
.Token("FirstName", (Func<BioPart, object>) (f => f.ContentItem.Parts.OfType<BioPart>().First().FirstName.ToLower()))
.Chain("FirstName", "FirstName", (Func<BioPart, object>)(f => f.ContentItem.Parts.OfType<BioPart>().First().FirstName.ToLower()))
.Token("LastName", (Func<BioPart, object>)(f => f.ContentItem.Parts.OfType<BioPart>().First().LastName.ToLower()))
.Chain("LastName", "LastName", (Func<BioPart, object>)(f => f.ContentItem.Parts.OfType<BioPart>().First().LastName.ToLower()))
;
}
}
}
My model:
namespace MyNS.Types.Models
{
public class BioPart: ContentPart<BioPartRecord>
{
public string FirstName {
get { return Record.FirstName; }
set { Record.FirstName = value; }
}
public string LastName
{
get { return Record.LastName; }
set { Record.LastName = value; }
}
public RoleInSchool RoleInSchool
{
get { return Record.RoleInSchool; }
set { Record.RoleInSchool = value; }
}
public bool IsBlogger {
get { return Record.IsBlogger; }
set { Record.IsBlogger = value; }
}
}
}
Though I've tried URL patterns with all of the following tokens, I've not been able to get a value back from the form that I've submitted:
{Content.Bio.FirstName}
{Content.BioPart.FirstName}
{Bio.FirstName}
{BioPart.FirstName}
No errors are being logged.

Resources