Flutter: redirect factory - flutter

I try do some like fabric for my default view.
/// Providing create access to create default snapshot state view.
abstract class DefaultSnapshotView extends StatelessWidget {
/// Constructor.
const DefaultSnapshotView({Key? key}) : super(key: key)
/// Default loading view.
const factory DefaultSnapshotView.loading({
Key? key,
}) = _Loading;
// ... other factories like above.
}
class _Loading extends DefaultSnapshotView {
const _Loading({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return const CircularProgressIndicator();
}
}
but I have error
what I do wrong? and how I can fix it for const factory ... =

A bit of a late answer:
I think you're only missing a ; after
const DefaultSnapshotView({Key? key}) : super(key: key);

Related

is there a way to custom build a container in flutter?

I would create a custom container and add properties like id to it
I tried doing it like this.
what I would like is to create a container which has a unique id assigned to it.
class Tile extends Container
{
late int id;
late int position;
Tile({Key? key}) : super(key: key);
}
You can create custom class like:
class Tile extends StatelessWidget {
late int id;
late int position;
Tile({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container();
}
}
You can choose one of the following two codes:
class Tile extends Container {
Tile({Key? key, required int id, required int position})
: super(
key: key,
child: ......,
);
}
or
class Tile extends StatelessWidget {
const Tile({Key? key, required this.id, required this.position})
: super(key: key);
final int id;
final int position;
#override
Widget build(BuildContext context) {
return Container(
child: ......,
);
}
}

Flutter: Superclass variable to upper case without touching subclass

Here i have some pseudo code with this scenario.
The UpperCaseElement superclass has the text variable. If upperCase is true, the text variable should be turned into upperCase.
I have tried setting it in the constructor, but it is only the default value if it isn't defined.
import "package:flutter/material.dart";
abstract class UpperCaseElement extends StatelessWidget {
// Should be uppercase if upperCase = true
final String text;
final bool upperCase;
// My attempt to set this.text to upperCase, but it doesn't work.
const UpperCaseElement({Key? key, this.text = text.toUpperCase(), this.upperCase = false})
: super(key: key);
}
class TestWidget extends UpperCaseElement {
const TestWidget({
Key? key,
super.text,
super.upperCase,
}) : super(key: key);
#override
Widget build(context) {
// Yes, the checking and converting to upper case can be done here. But doing that on all subclasses would be a pain. That's why I want to do it on the superclass.
return Text(text);
}
}
class TestPage extends StatelessWidget {
const TestPage({
Key? key,
}) : super(key: key);
#override
Widget build(context) {
return Scaffold(
body: Column(children: const [
// Should be TEST
TestWidget(
text: "test",
upperCase: true,
)
]),
);
}
}
Update: force on abstract class, you can't use const constructor.
#immutable
abstract class UpperCaseElement extends StatelessWidget {
String text;
final bool upperCase;
UpperCaseElement({Key? key, required this.text, this.upperCase = false})
: super(key: key) {
text = upperCase ? text.toUpperCase() : text;
}
}
For the UpperCaseElement is an abstract class, we can handle conditional inside TestWidget.
class TestWidget extends UpperCaseElement {
const TestWidget({
Key? key,
required super.text,
super.upperCase,
}) : super(key: key);
#override
Widget build(context) {
return Text(upperCase ? text.toUpperCase() : text);
}
}
abstract class UpperCaseElement extends StatelessWidget {
final String text;
final bool upperCase;
const UpperCaseElement({
Key? key,
required this.text,
this.upperCase = false,
}) : super(key: key);
}
My solution will be defining a getter function which will return text base on bool value
abstract class UpperCaseElement extends StatelessWidget {
// Should be uppercase if upperCase = true
final String text;
final bool upperCase;
get getText => upperCase ? text.toUpperCase() : text;
// My attempt to set this.text to upperCase, but it doesn't work.
const UpperCaseElement({Key? key, required this.text, this.upperCase = false})
: super(key: key);
}
class TestWidget extends UpperCaseElement {
const TestWidget({
Key? key,
required String text,
required bool upperCase,
}) : super(key: key, text: text, upperCase: upperCase);
#override
Widget build(context) {
// calling getText function from superClass.
return Text(getText);
}
}
class TestPage extends StatelessWidget {
const TestPage({
Key? key,
}) : super(key: key);
#override
Widget build(context) {
return Scaffold(
body: Column(children: const [
// Should be TEST
TestWidget(
text: "test",
upperCase: true,
)
]),
);
}
}

How to do operations on state constructor variables in Flutter?

I'd like to show change of wordCount in homeWidget
return Scaffold(
body: Column(
children: [
...
Expanded(
flex: 1,
child: RecorderView(
onSaved: _onRecordComplete,
wordCount:wordCount,
),
),
],
),
);
the variablewordCount relies on RecordView method:
I write this to get state variable here
class RecorderView extends StatefulWidget {
final Function onSaved;
final int wordCount;
const RecorderView({Key key, #required this.onSaved,#required this.wordCount}) : super(key: key);
#override
_RecorderViewState createState() => _RecorderViewState();
}
class _RecorderViewState extends State<RecorderView> {
//in one method
widget.wordCount++;
}
I want to add wordCount here,but it says it's a final one.How could I inherit wordCount from homeWidget without final? I just want to change the word show in wordCount automatically,thanks!!
You cant inherit the variable, you can however initialize it to a value from the widget.
class RecorderView extends StatefulWidget {
final Function onSaved;
final int wordCount;
const RecorderView({Key key, #required this.onSaved,#required this.wordCount}) : super(key: key);
#override
_RecorderViewState createState() => _RecorderViewState();
}
class _RecorderViewState extends State<RecorderView> {
int _wordCount;
#override
void initState() {
super.initState();
_wordCount = widget.wordCount;
}
}

Giving the initial parameter from external to Statefulwidget

I want to make Page Widget with string "mytitle","Anime"
It might be too simple question but it is very confusing for me the relationship of StatefulWidget and State
return Navigator.pushReplacement(context,
NoAnimationMaterialPageRoute(
builder: (context,"mytitle","Anime") => AnimePage()));
class AnimePage extends StatefulWidget {
AnimePage({Key key, this.title, this.video}) : super(key: key);
final String title;
final String video;
#override
_AnimePageState createState() => _AnimePageState();
}
class _AnimePageState extends State<AnimePage> {
#override
void initState() {
super.initState();
print(widget.video) //want to show `Anime` here
};
This works:
return Navigator.pushReplacement(
context,
NoAnimationMaterialPageRoute(
builder: (context) => AnimePage(
// pass your parameter values
title: 'My Anime',
video: YOUR_VIDEO_PARAM,
),
),
);
You also have to add the constructor on the State, like this...
class AnimePage extends StatefulWidget {
AnimePage({Key key, this.title, this.video}) : super(key: key);
final String title;
final String video;
#override
_AnimePageState createState() => _AnimePageState(title,video);
}
class _AnimePageState extends State<AnimePage> {
final String title;
final String video;
_AnimePageState(this.title,this.video);
#override
void initState() {
super.initState();
print(widget.video) //want to show `Anime` here
};
I hope that was what you're looking for

ancestorInheritedElementForWidgetOfExactType is deprecated

After Flutter version 1.12.1 ancestorInheritedElementForWidgetOfExactType is deprecated.
We should use getElementForInheritedWidgetOfExactType now.
How do I edit this BlocProvider to work with this new method?
import 'package:flutter/material.dart';
Type _typeOf<T>() => T;
abstract class BlocBase {
void dispose();
}
class BlocProvider<T extends BlocBase> extends StatefulWidget {
BlocProvider({
Key key,
#required this.child,
#required this.bloc,
}) : super(key: key);
final Widget child;
final T bloc;
#override
_BlocProviderState<T> createState() => _BlocProviderState<T>();
static T of<T extends BlocBase>(BuildContext context) {
final type = _typeOf<_BlocProviderInherited<T>>();
_BlocProviderInherited<T> provider =
context.ancestorInheritedElementForWidgetOfExactType(type)?.widget; //deprecated
return provider?.bloc;
}
}
class _BlocProviderState<T extends BlocBase> extends State<BlocProvider<T>> {
#override
void dispose() {
widget.bloc?.dispose();
super.dispose();
}
#override
Widget build(BuildContext context) {
return new _BlocProviderInherited<T>(
bloc: widget.bloc,
child: widget.child,
);
}
}
class _BlocProviderInherited<T> extends InheritedWidget {
_BlocProviderInherited({
Key key,
#required Widget child,
#required this.bloc,
}) : super(key: key, child: child);
final T bloc;
#override
bool updateShouldNotify(_BlocProviderInherited oldWidget) => false;
}
Do I get rid of the _typeOf variable now?
What are the benefits of this change?
Use context.getElementForInheritedWidgetOfExactType<_BlocProviderInherited<T>>().widget and it should work.
As far as benefits go, the documentation basically states the same though someone can correct me but I don't see any difference. You should be able to get rid of the _typeOf variable.