How do I toggle dark mode in a flutter app - flutter

I have a flutter app which has some code like the following:
MaterialApp(
home: Scaffold(/*...*/)
)
and a ThemeData(). However, I don't want the Scaffold's child Widget to be rebuilt and it doesn't have a const constructor either. I know that I can wrap the scaffold in a new widget and set its state but that would rebuild the children! What do I do?

Related

Flutter some default styling on new project

i'm starting on flutter, already sorry if it's a noob question
Basically started with a new project and there I am getting an already applied theme
How to remove this?
Thanks
Right now, you have a completely black screen because you don't have a Scaffold widget.
The Scaffold widget is screen that will be displayed on your display.
You can add your Text widget in the body property of your Scaffold
return const MaterialApp(
home: Scaffold(
body: Text("Hello")
)
);

Where to place the provider widget in the widget tree?

I'am trying to understand how to use provider and where to place the provider widget.
Therefore I build an app from a tutorial using provider. Here you can see the widget tree according to the tutorial. The provider widget is wrapped around the MaterialApp widget and the arrows show, which widget uses which other widget.
As far as I understand, in general, the provider widget is supposed to be placed as far down the widget tree as possible and only as far up as neccessary. As neither MaterialApp, NavigationBase, Scaffold nor IndexedStack need information from the providers, I thought I could move those down the tree. But this doesn't work: When I wrap MultiProvider around Scaffold (or any other widget below MaterialApp as far as I tried it) I get an exception.
Error: Could not find the correct Provider<StudentsProvider> above this StudentsListScreen Widget
Apparently - although having read a lot on provider - I haven't fully understood how provider interacts. What did I miss?
The provider must be located at the very top of the tree above the MaterialApp widget and declared once. Then in this case the provider's data will be available from any widget within the tree.
...
return MultiProvider(
providers: [...],
child: MaterialApp(
...someAppSettings,
home: HomeWidget()
)
Everything inside the MaterialApp widget (even when moving between different routes) will have access to the data from the provider.
I use this structure in my application and it works great.

Flutter | How to pause / unpause a widget and all its descendants?

In my Flutter app, I have a tabbar. Each tap is its own widget with lots of descendants widgets (see diagram).
In the Scaffold building the tabbar, the children are located in a IndexedStack widget - this is super cool, since it allows the user to change tabs without losing state.
Problem: Since we use IndexedStack, the entire widget tree for inactive (i.e in background) tabs are preserved, and all animations/servercalls are executed regardless.
Question: Is it possible to pause a widget + all the widgets descendants?
Expected result: Widgets gets paused/unpaused if they are in foreground or background, preserving resources but still not losing state.
Example:
Scaffold(
body: IndexedStack(index: i, children: [TabA(), TabB(), TabC()], // <- How to pause/unpause TabA, TabB, TabC?
bottomNavigationBar: // Tabbar widget here
)

MaterialApp ThemeData iconTheme

I have some ListTile widgets around my app, and all of them have an icon.
From here I see that I need to override my ListTile with
ListTileTheme(
iconColor: Colors.blue,
child: ..
)
since the ListTile.iconColor is gray by default and doesn't fallback to ThemeData.iconTheme.iconColor.
I wonder if there is a way to specify the list tile theme in ThemeData, so I don't have to create a new widget just for that.
For now, there isn't, but this feature may arrive in the next flutter release: https://github.com/flutter/flutter/issues/31247

How to work with a CustomAppBar in flutter when the phone has a notch?

does anyone know how to use the SafeArea widget in a custom app bar. I am having a red overflow flex box appear on my custom app bar due to a notch in my emulator. I want to still show my custom app bar without the red overflow box in a way that looks presentable. When I tried surrounding my appbar in a safeArea widget I received an error in my Scaffold widget where I had placed my appBar. The code looked like this: Scaffold( appBar: customAppBar(context)). The error read: The argument type 'SafeArea' can't be assigned to the parameter type 'PreferredSizeWidget'. I believe that the Scaffold expects a PreferredSizeWidget but is receiving a safeArea widget instead. Please let me know if there are any other solutions out there other than using the SafeArea widget. Below is a photo of what my emulator looks like with the custom app bar and the red overflow box.
If you always want to make sure notches and punch holes don't get into your AppBar or UI, use the builder function from MaterialApp like this:
#override
Widget build(BuildContext context) {
return MaterialApp(
builder: (context, child) => SafeArea(child: child),
home: MyApp(),
);
}
Now every single Page/Screen will be safe from being cut on the top.