I am developing a flutter app, and simply want to change the default scrolling physics to BouncingScrollPhysics so my listviews are more modified.
I know that there is already an answered question but it doesn't work on the latest version of flutter.
I wouldn't want to go around my app changing ListView properties an doing bad practices, any help would be appreciated <3
Similar to this solution:
Default ScrollPhysics
class MyScrollBehavior extends ScrollBehavior {
const MyScrollBehavior();
#override
ScrollPhysics getScrollPhysics(BuildContext context) => const BouncingScrollPhysics();
}
MaterialApp(
scrollBehavior: MyScrollBehavior(),
// ...
);
Related
Hi a use auto_route package in flutter app. How to restart the build method on TabPage or not save the stack state when switching tabs. I should have an animation run in the build method on TabPage every time I switch a tab in the navigation bar. But the build method is not called when I click the buttons in the navigation bar
I guess you are the same user that commented on my article. I'll give the same answer here. this is not related to the autoroute package itself but it is the default behavior of a bottombar navigation. The objective being to always minimize the number of rebuilds necessary for your UI.
I didn't test it but you could try to make your tab a stateful widget (if it isn't already) and modify this behavior with the AutomaticKeepAliveClientMixin<YourWidget>:
class _YourWidgetState extends State<YourWidget> with AutomaticKeepAliveClientMixin<YourWidget>{
// Here you build your Widget after calling the super method
#override Widget build(BuildContext context) {
super.build(context)
return Container();
}
// the important part is returning false here
#override bool get wantKeepAlive => false;
}
I have a widget that contains a "save" button. After pressing that button several other widgets (not only ancestor ones) must save its state to the file. I don't know how to inform them. What is the best way to achieve that in flutter? I was thinking about using Notification in the "save" widget, closest shared ancestor would contain a NotificationListener that triggers an event to which every widget will subscribe. For me, it doesn't look like a solution.
Provider is the recommended way to do State Management for apps of all sizes. -–Chris Sells – Product Manager, Flutter. June 19, 2019
It's pretty complicated at first, best to check out Simple app state management
The ChangeNotifier uses the notifyListeners function whenever there is any change in the class:
class ChangeNotifierModel extends ChangeNotifier {
String stringThatChanges = 'startValue';
void changeTheString(String value) {
stringThatChanges = value;
notifyListeners();
}
}
The only code that is specific to ChangeNotifier is the call to notifyListeners(). Call this method any time the model changes in a way that might change your app’s UI.
I'm pretty new to Flutter myself, but the way I understand it so, it kinda acts like a wrapper, e.g. wrapping the child of main.dart
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => SingleNotifier())
],
child: MyApp(),
));
don't forget the dependencies
dependencies:
provider: ^4.3.2+2
In flutter creating named routes is easy and logical, but only when returning a MaterialApp.
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
initialRoute: "/";
return MaterialApp( //gives errors when I return a Container
routes: {
"/" : (context) => FirstRoute(),
"/second route" : (context) => SecondRoute()
}
);
}
}
I am not that big a fan of Material Design and want to create a UI from my own design.
But when I apply the same pattern when returning a container I get an error.
So my qustion is how do I have named route setup with a vanilla Flutter App or am I being forced to use MaterialApp for my project?
Thanks in advance
MaterialApp is just a collection of commonly used components such as a navigator. You could also use a CupertinoApp. Material uses iOS navigation animations on iOS and Android animations on android. Though you’re not stuck to the UI design because you’re using a MaterialApp as a foundation. You are able to build whatever UI you want with a material app or even use Cupertino widgets. It’s all up to you.
class AppWidget extends StatefulWidget {
#override
_AppWidgetState createState() => _AppWidgetState();
}
class _AppWidgetState extends State<AppWidget> {
int _Count = 0;
build(context){}
}
I'm familiar with stateless and stateful widget in flutter but I'm curious why we're not defining stateful widget same as stateless widget? Why we need to declare 2 different class one for createstate method and one for actual state implementation?
I guess may be flutter team use this implementation because when app re-run we can get old state back without losing it if this is the case then How flutter knows?
And if application have more then 1 stateful widget then how flutter manage state for each stateful widget? Again my guess is flutter manage state based on State<AppWidget> but again HOW?
First class is the one that rebuilds the widget if you change anything in it. So for example if you have a text widget in the stateful widget and in the runtime you pressed a button and changed the text from 'Hello World' to 'Hello User' actually what happens is that flutter destroys the second class and rebuild it again with the new text using the first one.
You can find more information in this video from the Flutter Team:
Stateful Widgets Video
I use the inherited widget at the top of my app to update my application whenever a new location is setted. This results in a refresh of my location coordinates at every possible place in my app.
However I also have a textfield. When i tap on the textfield some seconds later
the keyboard will be hidden by the update of the inherited widget.
Is there a way to prevent flutter to hide the keyboard or to reinitialize the state so that the update of the inherited widgets works together with my search field?
It should be also possible that when i enter some text that this trigger the inherited widget for a new update but the search bar and keyboard should stay open.
new TextField(
controller: _controller,
autocorrect: false,
autofocus: true
...
)
Without seeing all of your code, it's a little bit difficult to know what's going on.
My best guess though is that you're using a context somewhere above the TextField to register for updates from the inherited widget.
What I'd advise you to do though is go up the widget tree from your TextField and make sure that you're not using the inherited widget in a context anywhere above it. It might be worth putting some debug statements at the beginning of your build methods to determine exactly where the build is being triggered from (the inherited widget shouldn't trigger a rebuild immediately below it if you're using it right, but stranger things have happened).
This includes if you have something like this:
MyWidget extends StatelessWidget {
Widget build(BuildContext context) {
Location location = MyInheritedWidget.of(context).location;
return new Row(
children: [
new LocationDisplayer(location),
new TextField( ..... ),
],
);
}
}
The reason I say this is that from my understanding of how inherited widgets work; when it changes, whichever widgets whose contexts were used to get the inherited widget get rebuilt.
That means that even though your textfield technically isn't changing any properties, it might actually be a new textfield depending on how the build went (there's a bit of flutter magic I don't fully understand around how they decide when to make new vs re-build things) and therefore it hasn't requested focus.
Rather than having to write a bunch of new widgets to enclose whatever is using MyInheritedWidget, I believe you could use something like this:
MyWidget extends StatelessWidget {
Widget build(BuildContext context) {
return new Row(
children: [
new Builder(
builder: (context) {
Location location = MyInheritedWidget.of(context).location;
return new LocationDisplayer(location);
},
),
new TextField( ..... ),
],
);
}
}
If you're 100% sure you're not doing this anywhere, then you could go for the non-flutter way of doing things... your location class could expose a subscribe and unsubscribe method. All of the widgets where you're actually using the location call subscribe (which should also be stateful widgets) with a callback in their initState() and unsubscribe in their dispose(). The location class would simply call each of the callbacks instead of changing state as you're doing now, and within each of the callbacks you should call setState for the widget that actually shows the location.