Should I make a page stateful if contains stateful widget inside it? - dart

I am in a confused situation if I should make a page/screen stateful or stateless. Elaborating it, I made a widget named MyHomePage which is MaterialApp. This contains only one widget which will change its value. That is Text.
Now just for only one stateful widget i.e. Text here do i have to extend MyHomePage from StatefullWidget or StatelessWidget? I know what is a stateful and stateless widget but I am confused in this type of situation.
Please make my concepts clear.

If the children of your stateless widget are immutable i.e don't change, then you should go for using a stateless widget. If you have a Stateful widgets children, you shouldn't care about the parent. Because each StatefulWidget has an internal state that will update(change the values inside the state) irrespective of the parent widget of the StatefulWidget

If the Text is going to be changed i.e. showing a different string (after some action like you have a button which changes the text), you need to use StatefulWidget.
And if Text is not going to be changed (immutable), you can use StatelessWidget.

Actually, you could use a StatelessWidget with a widget called StatefulBuilder as a parent of Text. This widget has a builder parameter with setState callback inside.
(EDITED)

Related

How can I return either a specific container widget, or nothing at all

I am using the Dismissible widget for swiping within my application. However, I wish to reuse the swiping activity, without the ability for the user to swipe. The whole class is wrapped in the dismissible.
So the overall code looks something like this:
Dimissible(
Row(
Column(
// random code
),
),
);
Whereas I want to be able to either have everything wrapped inside the dismissble, or not, without having to copy paste the entire code into a new class. Any suggestions?
How can I return either a specific container widget, or nothing at all
How would I go about that. Either return the dismissible or instead null? Wouldn't that leave me with an exception?
You can return a blank Container() or a SizedBox() widget (when you condition meets to return null).
Flutter will not throw an error then.

What is the significance of the settings variable in MaterialPageRoute?

I have a list of ListTile and whenever I tapped them, a new page will appear. Currently, the new page will slide up (when appearing) and slide down (when removed). I wanted to change the transition animation to Fade.
I've read the solution of that in here then I edited the code from the link and here is the result.
class MyCustomRoute<T> extends MaterialPageRoute<T> {
MyCustomRoute({ WidgetBuilder builder})
: super(builder: builder);
#override
Widget buildTransitions(BuildContext context,
Animation<double> animation,
Animation<double> secondaryAnimation,
Widget child) {
if (settings.isInitialRoute)
return child;
return new FadeTransition(opacity: animation, child: child);
}
}
The only difference from the link's solution to mine was that I never supplied the "settings" variable to the MaterialPageRoute class.
And here is the part of the code where I've used the Navigator.push:
new ListTile(
onTap: (){
Navigator.push(context, new MyCustomRoute(builder: (context) => new SecondPage("someTitle", "someDescription") ));
},
//The rest of the code goes here
I've tried to run this code and I've never expected that this will run smoothly since I never provided the settings variable to the MaterialPageRoute widget but it ran perfectly.
My question is, is this the right way to do it? or
Should I provide settings for the MaterialPageRoute class?
And also since I didn't provide a settings variable for the MaterialPageRoute class, where did it get its settings? In this part of the code:
if (settings.isInitialRoute)
return child;
I would appreciate any enlightment. Thanks in advance.
The settings are only meant for two things - specifying the name, and letting the route know whether it's the first page to be opened.
The isInitialRoute simply tells the route whether it's the first page to be opened; this is important because you don't want a slide up animation on the very first page.
Since it seems your Custom Route is really only used after the first page, you don't need to worry about this. So you're probably fine ignoring the settings, unless you start to use the page as your first page (and even then, fading in might not be the worst thing).

How can I add a PersistentBottomSheet right away when a Widget gets created?

How can I add a PersistentBottomSheet right away when a Widget gets created, instead of putting the code to show it in a callback to a button press or something else?
You can use a StatefulWidget's initState to do actions on first render.
Normally it's not possible to rebuild the layout inside initState. But you can instead schedule a rebuild right after the fist render using Scheduler.
#override
void initState() {
SchedulerBinding.instance.addPostFrameCallback((_) {
Scaffold.of(context).showBottomSheet(builder)
});
super.initState();
}
Don't forget to import import 'package:flutter/scheduler.dart';

Is it possible to set ExpansionPanelList in stateless widget?

Is it possible to set ExpansionPanelList in stateless widget ?
I can't make it because in statelesswidget we can't setstate, so my expansion panel list won't expand

Differrence between drawn & created functionalities of VLayout SmartGWT

I've found three functionalities in VLayout:
vLayout.isDrawn()
vLayout.isAttached()
vLayout.isCreated().
These functionalities are inherited from its super classes. Can anyone tell me the difference between the three? Or when the VLayout is said to be drawn, created, attached?
Thanks in advance.
To my understanding this three properties are inherited from different superclass:
isAttached from Widget
isCreated from BaseWidget
and isDrawn from Canvas
We are going from close to the dom to a DHtml object(the canvas)
To me created mean the Widget had been instanciated, attached would mean that the rendered html elements are attached to the dom and drawn when the whole set of element representing a widget is on the screen. It's only my perception, I never found any details on that. These properties don't seem to be for high level operations...
Regards
Alain

Resources