I think I've hared that there is a Widget the gets a Textstyle and a list of children. The children can be Text-Widgets or Icon-Widgets and get the defining styles (and with that the color of the Icon) from that parent.
But I can't find that anywhere.
Do I remember that correctly, and if, what Widget was that?
Or am I just wrong about that?
Edit:
I thought it was this, but it don't seam to work the way I thought:
return RichText(
strutStyle: StrutStyle.fromTextStyle(TextStyle(color: Colors.white, fontWeight: FontWeight.w500)),
text: TextSpan(
children: [
TextSpan(text: "Add Object"),
WidgetSpan(child: Icon(Icons.add))
]
),
);
You can make use of the DefaultTextStyle widget which receives a TextStyle entity and will apply it to descendant Text widgets (as long as the descendant Text widgets do not have explicit styles applied: https://api.flutter.dev/flutter/widgets/DefaultTextStyle-class.html
When talking about the icons as well, there is no dedicated widget for that since you would usually define that as part of your overall theme as ThemeData (usually provided in MaterialApp)
Related
In my flutter code when I am creating an UI lot of places using Text widget. So I converted this Text widget into a function and calling everywhere? Text widget also including some styling. So calling the function or calling the Text widget is better (execution speed)?
Example code:
Text('Time left to Entrance exam',style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.w400,
color: Colors.black
),);
or
Text buildText(String text,double fontSize, Color color) {
return Text(text,
style: TextStyle(
fontSize: fontSize,
color: color
),);
}
TextWidget function is more useful than multiple texts. It's absolutely a good practice and if any changes need you can able to change centrally and it's time-saving with clean code. You do not get the execution speed issue. And more important things, in both widget and function you just call a single Text widget. That's why there is no performance issue. You go for the second one for good practice.
Is there a straight forward way to change the font family to a widget and all its children?
my thought was to have an inherited widget and store the font family and call it to get that when creating every text widgets and set its style, but I prefer a simpler way.
You can wrap the widget whose descendants you want to change (e.g. MyWidget()) in a Theme Widget. For example:
Container(
child: Theme(
data: Theme.of(context).copyWith(
textTheme: TextTheme(<your changes here>),
),
child: MyWidget()
)
)
I am Getting this null safety error while working on my flutter app.
The argument type 'TextStyle?' can't be assigned to the parameter type 'TextStyle'.
Here is the code snippet that is throwing this error:
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button))
VS Code suggest this solution to put not operator at the end of the argument.
ButtonStyle(textStyle: MaterialStateProperty.all<TextStyle>(Theme.of(context).textTheme.button!))
Question: Is this a good practice to use ! in my code and what other solutions are possible in this case?
You can also get this error if you imported wrong library for the material TextStyle.
This may happen when you used:
import 'dart:ui';
Instead of:
import 'package:flutter/material.dart';
Material state properties represent values that depend on a widget's material "state". The state is encoded as a set of MaterialState values, like MaterialState.focused, MaterialState.hovered, MaterialState.pressed. For example the InkWell.overlayColor defines the color that fills the ink well when it's pressed (the "splash color"), focused, or hovered. The InkWell uses the overlay color's resolve method to compute the color for the ink well's current state.
You could use only Theme of context to use theme textStyle like this:
Text("copied text theme",
textAlign: TextAlign.center,
style: Theme.of(context).textTheme.button)
Initialize Your Style in the Buildcontext, Example :
var _styleType = Theme.of(context)
.textTheme
.body1
.copyWith(color: Colors.white);
Then Apply It to the Widget :
Container(
margin: EdgeInsets.symmetric(vertical: 10),
child: new Text("\u00a9 TasnuvaOshin",
overflow: TextOverflow.ellipsis,
style: _styleType),
),
MaterialStateTextStyle.resolveWith((states) => TextStyle(fontSize: 12),
You can use .resolveWith Method to create a MaterialStateTextStyle from a MaterialPropertyResolver callback function.
I am a newbie for Flutter who comes from Web Development side. Just for fun and practice for Flutter layout, I wanted to make a "drum kit" application. But the problem is, I can not set Drum Kit parts as it should be.
You know in web development, there is position absolute option for the elements on the webpage. Is there anything like that for Flutter?
I'd appreciate any answers. Thanks. :)
Let me show what I expected : Here it is
This is what I got as a result: Result
Stack(
children: <Widget>[
Positioned(
top: 100,
left: 0,
right: 0,
child: Text("Search",
style: TextStyle(
color: Color(0xff757575),
fontWeight: FontWeight.w700,
fontFamily: "Roboto",
fontStyle: FontStyle.normal,
fontSize: 56.0),
textAlign: TextAlign.center),
),
]
)
you can try this You can fix position in the screen
The Stack widget and Positioned widget helps you achieve this:
According to the official documentation:
A Stack is a widget that positions its children relative to the edges of its box.
This class is useful if you want to overlap several children in a simple way, for example having some text and an image, overlaid with a gradient and a button attached to the bottom.
A Positioned is a widget that controls where a child of a Stack is positioned.A Positioned widget must be a descendant of a Stack.
Read more about the Stack widget here: Stack Widget
Read more about the Positioned widget here: Positioned Widget
Taking your example, you will need to create a Stack widget and the children property of the Stack widget should be your drum parts wrapped in a Positioned widget. The Positioned widget should only be used in the Stack widget.
You can use Stack class, which will have a Positioned class
How does it help
Stack helps you have relative-layout
Positioned helps you adjust the item like absolute in web
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",),
),
),
[...]
)