How to change datepicker color in flutter? - flutter

builder: (context, child) {
return Theme(
data: Theme.of(context).copyWith(
colorScheme: const ColorScheme.light(
primary: Colors.white,
onPrimary: Colors.black,
),
textButtonTheme: TextButtonThemeData(
style: TextButton.styleFrom(
foregroundColor: Colors.black,
),
),
),
child: child!,
);
},
This is my code. And I want to make the header white. But the button is not visible when the header is white. What should I do?

The today color is colorScheme.primary which is white, and we can't see.
final Color todayColor = colorScheme.primary;
.....
} else if (isToday) {
// The current day gets a different text color and a circle stroke
// border.
dayColor = todayColor;
decoration = BoxDecoration(
border: Border.all(color: todayColor),
shape: BoxShape.circle,
);
}
You can check the calendar_date_picker.dart
For now, I can think of creating a local file and customize the color, or changing picker color.

Related

how to disable the original text background color when selecting text in SelectableText.rich

Is there some way to disable the original text background color when selecting text in SelectableText.rich.
Otherwise the original background color(amber for example) is still there, which is different from the selected text background color (blue), and thus making it look like it is not selected.
Wrap it with theme and give it color i hope it helps
Theme(
data: ThemeData(textSelectionColor: Colors.green),
child: SelectableText.rich(
),
),
You need to set your select color first in ThemeData like this:
MaterialApp(
theme: ThemeData(
// use textSelectionTheme and set your needed color instead
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colors.amber,
),
),
),
if need to use just this SelectableText.rich with specific color just wrap your SelectableText.rich with theme:
Theme(
data: ThemeData(
textSelectionTheme: const TextSelectionThemeData(
selectionColor: Colors.amber,
// selectionColor: Colors.transparent, // it can make select color transparent that you need or make that backgroundColor: Color.transparent
),
),
child: const SelectableText.rich(
TextSpan(
children: [
TextSpan(
text: 'test ',
style:
TextStyle(color: Colors.black, backgroundColor: Colors.amber
// this backgroundColor make the white backcolor into amber
),
),
],
),
toolbarOptions: ToolbarOptions(cut: true), // tools like copy, paste
enableInteractiveSelection: false, // you can enable select color
cursorColor: Colors.red, // this is an option if you want change
),
),

Is it possible to change Icon according to lightTheme or darkTheme in ThemeData? in flutter

I want the icon to change and the boxshadow color to change when GestureDetecotr is pressed. hopefully it will be What should I do?
In the current emulator, press the middle button to change the appearance.
blackMode
lightMode
home.dart part
import 'package:flutter/material.dart';
import 'package:neumorphism/theme/theme_service.dart';
class Home extends StatelessWidget {
const Home({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
bool _tapped = false;
var _lightIcon = Icons.wb_sunny;
var _blackIcon = Icons.dark_mode;
return Scaffold(
appBar: AppBar(
title: Text('Neumorphism'),
),
body: Center(
child: GestureDetector(
onTap: () {
ThemeService().changeThemeMode();
},
child: Container(
width: 200,
height: 200,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.all(
Radius.circular(40),
),
boxShadow: [
BoxShadow(
color: Colors.grey.shade500,
offset: Offset(5.0, 5.0),
blurRadius: 15.0,
spreadRadius: 1.0),
BoxShadow(
color: Colors.white,
offset: Offset(-5.0, -5.0),
blurRadius: 15.0,
spreadRadius: 1.0),
]),
child: Center(child: Icon(_lightIcon)))),
),
);
}
}
themes.dart
import 'package:flutter/material.dart';
class Themes {
final lightTheme = ThemeData.light().copyWith(
primaryColor: Colors.grey[300],
appBarTheme: AppBarTheme(
brightness: Brightness.light,
),
iconTheme: IconThemeData(color: Colors.white, size: 80),
);
final darkTheme = ThemeData.dark().copyWith(
primaryColor: Colors.grey[800],
appBarTheme: AppBarTheme(
brightness: Brightness.dark,
),
iconTheme: IconThemeData(
color: Colors.black87,
size: 80,
),
);
}
theme_service.dart
theme_service.dart
Hello its easy to do this.
Your main problem is that you need to change the icons when the button is pressed. Right?
Then for this you have to use a Stateful Widget and then wrap the widget into a Value Listenable Builder. After that in onpressed you need to update the notifier.
Let's take a example....
Suppose you have to change the background color of the container when theme changed then do like this..
First create a Instance of value Notifier.
ValueNotifier<bool> _themeNotifier = ValueNotifier<bool>(false);
And then update the notifier in onpressed like this:-
onPressed:(){
_themeNotifier.value = false ; //update the value
}
Note that i have used bool as a datatype of value notifier but you can choose whatever suits your need.
Then Wrap your widget in a ValueListnablebuilder
ValueListenableBuilder(
valueListenable: _themeNotifier,
builder: (context, value, child){
return Container(
decoration:BoxDecoration(color:_themeNotifier.value ? Color.white : Color.black),
child: Container(),
)
},
),
Similar to this in you case you need to change the icon and boxshadow
according to the value of the notifier.
If you need any further explanation or help then you can comment.
and as always
Happy Coding...
you also need to change the brightness of context along with theme mode
Check your theme's brightness to change icon and shadows.
To change the icon, you can use this package. Install the package in your project, import it in your file and then simply replace the child of your container with the below code:
AdvancedIcon(
icon: Icons.wb_sunny,
secondaryIcon: Icons.dark_mode,
state: Theme.of(context).brightness==Brightness.dark ? AdvancedIconState.secondary : AdvancedIconState.primary,
)
Now to change shadow of the container, just replace the boxShadow of your container's decoration with below code:
// replace the shadow with your values.
Theme.of(context).brightness == Brightness.dark ? [BoxShadow(color: Colors.white)] : [BoxShadow(color: Colors.black)]

RaisedButton vs ElevatedButton, FlatButton vs TextButton and OutlineButton vs OutlinedButton

I'm seeing the warning using old buttons:
'RaisedButton' is deprecated and shouldn't be used.
'FlatButton' is deprecated and shouldn't be used.
'OutlineButton' is deprecated and shouldn't be used.
So, what's the difference between:
RaisedButton and ElevatedButton
FlatButton vs TextButton
OutlineButton vs OutlinedButton
Here I found the docs for Migrating to the New Material Buttons and their Themes
The following image says itself what are the difference between all.
Visually, the new buttons look a little different, because they match
the current Material Design spec and because their colors are
configured in terms of the overall Theme’s ColorScheme. There are
other small differences in padding, rounded corner radii, and the
hover/focus/pressed feedback.
You can check Changes Overview for more about changes.
First are obsolete classes.
Quotes from the Flutter documentation:
FlatButton, RaisedButton, and OutlineButton have been replaced by TextButton, ElevatedButton, and OutlinedButton respectively.
The original classes will be deprecated soon, please migrate code that uses them.
These are obsolete classes, so eventually your old code needs to go away. (and, this document will help you do exactly that.) However, that can be a lot of work, so to get things moving, I created some code to migrate the FlatButton and RaisedButton to the new TextButton and ElevatedButton 'in place'. They are analogous to each other, but they approach styling in different ways, which this code handled.
Here's a link to the the gist if you want to run it in dartpad.dev (I couldn't get it to link directly):
https://gist.github.com/wterrill/7942b4bf5d74a8b2ace952ebf213fe5d
Here's the code itself:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final bool disableButton = true; // <-- Change this to see buttons disabled
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
FlatButton(
child: Text("FlatButton"),
onPressed: disableButton
? null
: () {
print("FlatButton normal");
},
color: Colors.green,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColor: Colors.grey,
disabledTextColor: Colors.pink),
SizedBox(height: 25),
FlatButtonX(
childx: Text("TextButton"),
onPressedx: disableButton
? null
: () {
print("FlatButtonX (TextButton)");
},
colorx: Colors.green,
textColorx: Colors.black,
shapex: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColorx: Colors.grey,
disabledTextColorx: Colors.pink),
SizedBox(height: 100),
RaisedButton(
child: Text('RaisedButton'),
color: Colors.green,
textColor: Colors.black,
onPressed: disableButton
? null
: () {
print("RaisedButton normal");
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColor: Colors.grey,
disabledTextColor: Colors.pink,
),
SizedBox(height: 25),
RaisedButtonX(
childx: Text('ElevatedButton'),
colorx: Colors.green,
textColorx: Colors.black,
onPressedx:disableButton
? null
: () {
print("RaisedButtonX (ElevatedButton)");
},
shapex: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(50),
),
side: BorderSide(color: Colors.purple, width: 3.0)),
disabledColorx: Colors.grey,
disabledTextColorx: Colors.pink,
)
],
),
),
),
);
}
}
Widget FlatButtonX(
{Color colorx,
#required Widget childx,
RoundedRectangleBorder shapex,
#required Function onPressedx,
Key keyx,
Color disabledColorx,
Color disabledTextColorx,
Color textColorx}) {
if (disabledTextColorx == null && textColorx == null) {
disabledTextColorx = colorx;
}
if (textColorx == null) {
textColorx = colorx;
}
return TextButton(
key: keyx,
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
// text color
(Set<MaterialState> states) => states.contains(MaterialState.disabled)
? disabledTextColorx
: textColorx,
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
// background color this is color:
(Set<MaterialState> states) =>
states.contains(MaterialState.disabled) ? disabledColorx : colorx,
),
shape: MaterialStateProperty.all(shapex),
),
onPressed: onPressedx as void Function(),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0), child: childx));
}
Widget RaisedButtonX(
{Color colorx,
#required Widget childx,
RoundedRectangleBorder shapex,
#required Function onPressedx,
Key keyx,
Color disabledColorx,
Color disabledTextColorx,
Color textColorx}) {
if (disabledTextColorx == null && textColorx == null) {
disabledTextColorx = colorx;
}
if (textColorx == null) {
textColorx = colorx;
}
return ElevatedButton(
key: keyx,
style: ButtonStyle(
foregroundColor: MaterialStateProperty.resolveWith<Color>(
// text color
(Set<MaterialState> states) => states.contains(MaterialState.disabled)
? disabledTextColorx
: textColorx,
),
backgroundColor: MaterialStateProperty.resolveWith<Color>(
// background color this is color:
(Set<MaterialState> states) =>
states.contains(MaterialState.disabled) ? disabledColorx : colorx,
),
shape: MaterialStateProperty.all(shapex),
),
onPressed: onPressedx as void Function(),
child: childx);
}
One of the advantages of ElevatedButton over RaisedButton is that it will actually pick up your main theme color by default.
So you don't even need to add that custom background color. You would only need to bring your own styling in ElevatedButton, if you want to deviate from your main theme or style other aspects of the button.
My understanding is that they are really equivalent. According to the Flutter 1.22 announcement, the main motivation was around styling. Prior to Flutter 1.22, there was only ONE ButtonTheme for 3 types of buttons, whereas now each type of button has its own theme.
The FlatButton, RaisedButton and OutlineButton widgets have been replaced by TextButton, ElevatedButton, and OutlinedButton respectively.
Link explaining changes:
https://flutter.dev/docs/release/breaking-changes/buttons
How to use the buttons:
https://flutter.dev/docs/development/ui/widgets/material
you can't set borderSide nor shape either on OutlinedButton even though you could on OutlineButton
Alternative buttons such as TextButton, ElevatedButton, and OutlinedButton are not quite easy like before. We can still use those legacy buttons using the legacy_buttons package
https://pub.dev/packages/legacy_buttons/

Weird Black Background in Flutter's OutlineButton Widget

I'm using Flutter's OutlineButton Widget, and I can't figure out how to remove that weird black background highlight when the button is clicked / pressed.
CLICK FOR VIDEO OF ISSUE
This is the button:
OutlineButton(
highlightElevation: 1.0,
onPressed: () => onRequestAllowLocation(context),
child: Text(
"ALLOW LOCATION",
style: TextStyle(fontWeight: FontWeight.w600, fontSize: 16),
),
borderSide: BorderSide(color: MyApp.accentColor, width: 2.0),
textColor: MyApp.accentColor,
shape: new RoundedRectangleBorder(borderRadius: new BorderRadius.circular(16.0))),
)
And here's the app's theme:
ThemeData(
fontFamily: 'Dosis',
brightness: Brightness.dark,
primarySwatch: Colors.blue,
accentColor: accentColor,
highlightColor: highlightColor,
buttonColor: accentColor,
indicatorColor: accentColor,
backgroundColor: primaryColor,
scaffoldBackgroundColor: primaryColor,
primaryColor: primaryColor,
)
P.S. None of the const colors I provide above are black.
It's the shadow. Stop setting highlightElevation and it will go away. From OutlineButton class docs:
The button's highlightElevation, which defines the size of the drop shadow when the button is pressed, is 0.0 (no shadow) by default. If highlightElevation is given a value greater than 0.0 then the button becomes a cross between RaisedButton and FlatButton: a bordered button whose elevation increases and whose background becomes opaque when the button is pressed.

Change the Highlight Color of selected text

When the user select a text from inside a TextField, the default highlight color is blue. How to change it to green for example ?
2021 answer
Wrap with Theme and use copyWith to preserve other theme data.
Theme(data: Theme.of(context).copyWith(
textSelectionTheme: TextSelectionThemeData(
selectionColor: Colors.green)),
child: TextFormField()
)
Wrap your text widget with theme and assign the color to the textSelectionColor property inside ThemeData
refer below code for same:- I have changed the text selection color to green
Theme(
data: ThemeData(textSelectionColor: Colors.green),
child: TextField(
controller: _inputController,
decoration: InputDecoration(hintText: "Input"),
),
),
change the value of textSelectionColor of your ThemeData and it will give you the result you are looking for.
please use this code.
Widget build(BuildContex contex){
return MaterialApp{
debugShowCheckedModeBanner: false,
theme: ThemeData(
brightness: Brightness.light,
primarySwatch: Colors.green,
iconTheme: IconThemeData(
color: kGreenColor,
),
hoverColor: kPrimaryColor,
indicatorColor: kPrimaryColor,
),
}
}