I would like to create a class that will contain a few functions to be used in all the code.
I'm doing this:
MAIN CLASS
class ListaAbitudini extends StatefulWidget {
final Function scegliIcona;
ListaAbitudini({this.scegliIcona});
#override
State<StatefulWidget> createState() {
return new _ListaAbitudiniState();
}
}
class _ListaAbitudiniState extends State<ListaAbitudini> {
#override
Widget build(BuildContext context) {
widget.scegliIcona();
...
}
}
CLASS WITH THE FUNCTION
class FindIcone {
Icon scegliIcona(){ ... }
ListaAbitudini(scegliIcona);
}
The problem is that I have an error on this line: 'ListaAbitudini(scegliIcona);' saying that "ListaAbitudini must have a method body because FindIcone isn't abstract".
What am I doing wrong? HELP
The method you use is for parent widget to child widget. In your case you can Create new object of FindIcone and call the method.
class ListaAbitudini extends StatefulWidget {
#override
State<StatefulWidget> createState() {
return new _ListaAbitudiniState();
}
}
class _ListaAbitudiniState extends State<ListaAbitudini> {
#override
Widget build(BuildContext context) {
FindIcone().scegliIcona();
}
}
class FindIcone {
Icon scegliIcona() {
return Icon(Icons.ac_unit);
}
Also you can use provider.
Related
I have created an abstract wrapper class around Electron's native BrowserWindow.
It defines two abstract accessors for the BrowserWindow. But whenever I inherit from BaseWindow, the window turns out to be undefined (note comments)!
import { BrowserWindow, BrowserWindowConstructorOptions } from "electron";
abstract class BaseWindow {
public constructor(options?: BrowserWindowConstructorOptions) {
this.setWindow(new BrowserWindow(options));
console.log("From Base", this.getWindow()); // > From Base BrowserWindow { ... }
}
public abstract getWindow(): BrowserWindow;
protected abstract setWindow(newWindow: BrowserWindow): void;
}
class MainWindow extends BaseWindow{
private window!: BrowserWindow;
public override getWindow(): BrowserWindow {
return this.window;
}
protected override setWindow(newWindow: BrowserWindow): void {
this.window = newWindow;
}
public constructor() {
super({
//...
});
console.log("From Main", this.getWindow()); // > From Main undefined
}
}
// Inside ready event
const window = new MainWindow();
console.log("From App", window.getWindow()) // > From App undefined
Am I doing something wrong? Are there any ways of fixing it?
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));
}
}
}
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);?
// 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(() {});
}
}
}
I have pasted the whole class below from android studio. Im trying to create a gallery using image view and GridView.
package com.example.sc.loginappexercise;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
/**
* Created by sc on 16/03/2017.
*/
public class ImageAdapter extends BaseAdapter {
public Context context;
public int[] images = {
R.drawable.gallery1, R.drawable.gallery2,
R.drawable.gallery3, R.drawable.gallery4,
R.drawable.gallery5, R.drawable.gallery6,
R.drawable.gallery7, R.drawable.gallery8,
R.drawable.gallery9, R.drawable.gallery10,
};
public ImageAdapter(Context c){
context = c;
}
#Override
public int getCount() {
return images.length;
}
#Override
public Object getItem(int position) {
return images [position];
}
#Override
public long getItemId(int position) {
return 0;
}
}
**Below where it says setLayoutParams - it stays red and throws an error. cannot be referenced from a static context? **
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(images[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
ImageView.setLayoutParams(new GridView.LayoutParams(240, 240));
return imageView;
}
}
ImageView.setLayoutParams(new GridView.LayoutParams(240, 240)); => imageView.setLayoutParams(new GridView.LayoutParams(240, 240));
I => i
That's all!