how to change the opacity of the snackbar in flutter? - flutter

I wish to change the opacity of the SnackBar. It has only the background property. Can it be customized or I have to create the custom widget for snack bar?

Try using the color property of snack bar like this,
backgroundColor: Colors.black.withOpacity(0.5)
This should work as you expected.

You can adjust the opactiy of your backgroundColor with
color.withAlpha(..),
color.withOpacity(..),
using a hexadecimal integer 0x33ffffff (the first pair of digits after the x represents the alpha value),
creating a Color using Color.fromARGB(...)
or by using Color.fromRGBO(...).
You can find information about this on this documentation page about the Color class.
Now, you face the following problem: Your content is not yet translucent.
This is easily adjustable using the Opcacity Widget.
In your Snackbar just surround your actual content with an Opacity Widget:
SnackBar(backgroundColor: Color(0x66bbbbbb),
content: Opacity(opacity: .7,
child: Container(), // your content
),
)

If you are planning to make the background transparent, this may help you.
Mostly you get a black background because of elevation.
SnackBar(
elevation: 0,
backgroundColor: Colors.transparent,
/.......
),

Related

Flutter: Remove brightness bar from flutter_colorpicker

I used the flutter_colorpicker package to implement a color picker in my app. My widget looks something like this:
ColorPicker(
pickerColor: ...,
paletteType: PaletteType.hueWheel,
onColorChanged: (color) {
...
},
enableAlpha: false,
labelTypes: const [],
)
In the UI it looks like this:
Now I want to remove the brightness bar on the bottom. I know that the color picker is not complete without that brighness bar but I will handle the brightness a different way. I have found no official documentation on how to achieve this.
How do I remove that bar? Hacks are also welcome or somehow extending the package with inheritance.
Here I attach some links which contains no brightnesss bar
Flutter Material Color Picker
Flex Color Picker
I have solved the problem by clipping away that bar using a ClipRect widget:
ClipRect(
child: Align(
alignment: Alignment.topCenter,
heightFactor: 0.75,
child: ColorPicker(
...
colorPickerWidth: 250,
),
),
);
I have not found any unwanted side effects with this approach yet.

Flutter: How do you set the text color for a ListTile that is 'selected'?

I am working on a Flutter app where I am using a drawer. The drawer consists of various menu options using the ListTile class. The documentation specifies that when the 'selected' property of a ListTile is true then 'By default the selected color is the theme's primary color.' but it makes no mention of the text color. Whenever a ListTile is selected the corresponding text changes to blue, which goes against my theme. How do I set the text color for a selected ListTile?
Thanks.
You can apply theme to ListTile by wrapping it with ListTileTheme.
ListTileTheme(
selectedColor: Colors.red, // text & icon color
selectedTileColor: Colors.green, // background color
child: ListTile(
title: Text('cats')
)
)

Flutter - how to get AppBar title foreground color

I have a Flutter app, where on the appBar, I have added a dropdown button. My app supports primary / secondary color changing.
The screenshot below shows an appBar for two different primary colors.
As you can see, the Flutter is able to decide what color to use for app bar text to keep proper readability - white for dark color and black for light color (second app bar).
I would like to set dropdown items' text color identical for the one, used by Flutter app bar text, hence, I would like to retrieve it.
Looking at Theme.of(context) properties, however, didn't give me a clue what color should I use to achieve what I need to.
Below is the code snippet:
final ThemeData _theme = Theme.of(context);
final int index = values.indexWhere((TimeSpan element) => element.value == initialValue.value);
return Theme(
data: _theme.copyWith(
canvasColor: _theme.primaryColor,
brightness: Brightness.dark,
),
child: DropdownButtonHideUnderline(
child: DropdownButton<TimeSpan>(
items: values
.map(
(value) => DropdownMenuItem(
child: Text(
value.toString(),
style: TextStyle(color: _theme.colorScheme.surface),
),
value: value,
),
)
.toList(),
onChanged: callback,
isExpanded: false,
value: values[index],
),
),
);
After a couple of days working with light / dark theme brightness, I figure out, a (possible) solution for the above case.
You can get the desired text color via one of the existing text themes, for instance Theme.of(context).primaryTextTheme.title.color returns color adjusted to current theme brightness.
Flutter renders the widget based on the final theme values it has.
So for example if the child widget has a different theme and the parent widget has a different theme, Flutter's rendering engine will first give preference to the child widget's theme over the parent widget's theme.
So DropdownButton has a theme hardcoded by default which cannot be directly changed as the widget does not accept any parameter to change the theme of the underlying widget(s) and as a result the Theme of the parent widget won't change/alter the theme of DropdownButton. That's the reason why the text Week remains black in both the cases.
If you really want to change the theme you can either alter the source code of DropDownButton or make a custom widget for it. However, simply hardcoding the values for text-color should still do the work.
In order to change the Appbar's text color you will have to manually change the text color as the parent theme suggests that the text color should be white whereas you want it to be black(as per your requirements).
Scaffold(
appBar: AppBar(
title: Text("Weight Overview",color: Colors.black)
)
[...]
)
On OP's request,
You can manually change the Theme of your children at any point of your Widget tree by using the Theme widget and providing it with a theme.
Solution code:
Scaffold(
appBar: AppBar(
title: Theme(
theme: ThemeData(primaryColor: Colors.black),
child: Text("Weight Overview",),
),
),
[...]
)

How can I create a Drawer below status bar in Flutter?

I want to make something like this, but always get this:
Scaffold(
drawer: Drawer(..),
..
)
How do I create a Drawer that is not displayed in the status bar?
For this kind of scenario, Flutter has the SafeArea widget. This widget will make sure that nothing is rendered e.g. beneath the status bar, i.e. a padding is added.
To apply this to your Drawer, you can simply wrap your Drawer with a SafeArea:
Scaffold(
drawer: SafeArea(
child: Drawer(..),
),
..
)
Screenshot of the drawer
You can also specify if you want to remove some of the padding added by SafeArea using the optional parameters top, bottom, left & right, e.g. SafeArea(bottom: false, ..).
Adding the padding: const EdgeInsets.all(0.0), to ListView resolves the issue.

Flutter - Color name string to material color

In Flutter is there a way to generate a material color from it's name, without creating a full map Map<String,MaterialColor>.
In theory, something like this:
String colorName = "deepOrange";
MaterialColor color = Colors(colorName);
According to the comment, the intention is to save and read back from shared_preferences. In that case, it is better to save and retrieve the color by int value, not by string name, to ensure we always get the color.
Save: prefs.setInt("prefered_color", Color.value)
Retrieve: Color c = const Color(prefs.getInt('prefered_color') ?? 0xFF42A5F5);
According to the official doc, there's currently no API to perform the function you described. Although it's easy to implement your methodology, I doubt its usefulness in general cases. Also we have to deal with typos or noSuchColor errors. But using const / enum will offer the advantage of compile time error checking.
I manage to do something like that using the
Colors.primaries list ( found in the colors.dart file):
//colors.dart
/// The material design primary color swatches, excluding grey.
static const List<MaterialColor> primaries = <MaterialColor>[
red,
pink,
purple,
deepPurple,
indigo,
blue,
lightBlue,
cyan,
teal,
green,
lightGreen,
lime,
yellow,
amber,
orange,
deepOrange,
brown,
// The grey swatch is intentionally omitted because when picking a color
// randomly from this list to colorize an application, picking grey suddenly
// makes the app look disabled.
blueGrey,
];
So in order to save the colors:
Color colorToSave = Colors.indigo;
prefs.setInt("colorIndex", Colors.primaries.indexOf(colorToSave));
To retrieve:
MaterialColor colorSaved = Colors.primaries(getInt('colorIndex') ?? 0);
Hope that helps you.
Here is an easy way to do it.
Let's suppose you want to get the material color for Colors.blue
You can use this to get it:-
MaterialStateProperty.all(Colors.blue)
For example, to set the background color of an elevated button, a material color should be provided. You can provide a named color like:-
ElevatedButton(
style: ButtonStyle(
backgroundColor: MaterialStateProperty.all(Colors.blue),
),
onPressed: (){},
child: Text('Press me'),
),
Hope helps.