Flutter: How do you change the AppBar Title font? - flutter

Here's my first flutter app... Just learning and messing around a bit. I'm trying to figure out how to change the text of the title within the AppBar. Note that the text in the body changes, but the text in the app bar does not. Am I totally wrong here? I used this method based on other public questions on stack overflow on this topic. No luck on my end.
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('My First App', style: TextStyle(fontFamily: 'Audowide'),),
centerTitle: true,
backgroundColor: Colors.cyan[700],
brightness: Brightness.dark,
),
body: Center(
child: Text("Body Text!",
style: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
fontFamily: 'Audiowide',
letterSpacing: 2.0,
color: Colors.grey[500],
),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {}, // for later use
backgroundColor: Colors.cyan[700],
child: Text("Press"),
),
),
));

Step 1: Fix typo in your code change Audowide to Audiowide
Step 2: Download https://fonts.google.com/specimen/Audiowide
Step 3: Put file in directory assets/fonts
Step 4: In pubspec.yaml
fonts:
- family: Audiowide
fonts:
- asset: assets/fonts/Audiowide-Regular.ttf
working demo

You need to download the font, and put your reference in pubspec.yaml.
Look at how the documentation reports:
https://flutter.dev/docs/cookbook/design/fonts

You can try one of the 2 things:
Adding fonts manually to the project and defining them in pubspec.yaml, before using them in the project.
Another interesting package is google_fonts which is simpler and provides options for a lot many fonts that you might wanna try. The entire procedure to add it to your project and subsequent use is here

You can add the Google Font in your pubspec.yaml file.
dependencies:
google_fonts: ^1.1.1
Import the dependency in the Pubspec.yaml file
import 'package:google_fonts/google_fonts.dart';

Related

Using variable font in Flutter only show FontWeight.w400 or FontWeight.w600 for every FontWeight

I'm facing a problem trying to use any Variable Font downloaded from fonts.google.com
Here is the result I get:
Expected result (obtained using multiple fixed font files):
Here is the code used:
Page:
Scaffold(
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: FontWeight.values
.map(
(weight) => Text(
'This text has weight $weight',
style: TextStyle(fontWeight: weight),
),
)
.toList(),
),
),
);
App:
MaterialApp(
home: ...,
theme: ThemeData(
fontFamily: 'Sono',
),
)
pubspec:
flutter:
uses-material-design: true
...
fonts:
- family: Sono
fonts:
- asset: assets/fonts/Sono-VariableFont.ttf
Tried a lot of things, like wrapping my app in a DefaultTextStyle or changing the fontVariations parameter. No effect on the variable fonts.
Use FontVariation property of TextStyle widget.
FontWeight.values
.map(
(weight) => Text(
'This text has weight $weight',
style: TextStyle(
fontVariations: [
FontVariation(
'wght', ((weight.index + 1) * 100).toDouble())
],
),
),
)
.toList(),
Output

I want to add a page favorites button with flutter that is saved even in local context

I'm making a learning app. But here, I would like to bookmark only the problem I want out of hundreds of problems, so that the mark appears on the problem selection page next time.
The question is whether this can only be implemented in flutter without firebase or any other database.
Is it possible to implement it locally?
child: Scaffold(
appBar: AppBar(
backgroundColor: Colors.white,
title: Text('1-1',
style: TextStyle(
color: Colors.blueAccent,
fontSize: 20,
fontWeight: FontWeight.bold,
),),
centerTitle: true,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
color: Colors.black,
iconSize: 25,
icon: Icon(Icons.arrow_back),
),
actions: <Widget>[
IconButton(
icon: Icon(Icons.bookmark_outline),
iconSize: 25,
color: Colors.black,
onPressed: () {
//here
}
),
],
),
Yes, in Flutter apps you have some options to store data in local storage permanently. For example, you can use one of these options:
Shared preferences
add this package to your pubspec.yaml file:
dependencies:
shared_preferences: ^2.0.8
and run flutter pub get. Now, You are able to use it based on this example.
SQLite
add this package to your pubspec.yaml file:
dependencies:
sqflite: ^2.0.0+4
and run flutter pub get. Now, You are able to use it based on this example.
Note: Usually, it is better to store a small amount of data using shared preferences and large data based on a database table. personally, I prefer shared preferences for your application.

Unable to display images on Flutter app when using Scaffold Class

I am writing code for a simple flutter app.
The code is very simple,
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.teal,
shadowColor: Colors.black38,
title: Text(
'Some Ramdom Text',
style: TextStyle(color: Colors.black),
),
),
body: Center(
child: Image.asset('images/poor.png'),
),
),
),
);
}
However when I run it, I get the below error:
The following assertion was thrown resolving an image codec:
Unable to load asset: images/poor.png
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:224:7)
<asynchronous suspension>
What could I be missing? I tried refactoring the project.
I checked the pubspec.yaml, it looks good.
assets:
- images/
The Error states itself It was not able to find the assets or load it do a pub get in pubsec.yaml after saving it.
It will fix your problem
I am using Android Studio, and when I open pubspec.yaml, I see the option Pub Get on the right top corner on the screen.
This enables the project to install the referenced packages.
After this I was able to view the image.

How to set color of all text in a specific container in flutter?

I don't want to change the text color of the whole app. Just all the text inside a container. Can I wrap it with some other widget or something for this ?
To apply certain TextStyle properties only to a subtree of your app. You can use DefaultTextStyle
DefaultTextStyle(
child: Container(child: /* your subtree */),
style: TextStyle(color: Colors.red),
),
as a comment pointed out, this replaces all defaults, not just the color. This can be mitigated by using the merge constructor:
DefaultTextStyle.merge(
child: Container(child: /* your subtree */),
style: TextStyle(color: Colors.red),
),
flutter's answer is good in my opinion. But the power of ThemeData is more than you think. Here is the official documentation about Themes for part of an application.
You could provide a Theme to wrap your container to provide a new theme. Here is two way to slove it:
1. Creating unique ThemeData
/*Not recommended, this could make a totally different If you just want a little part changed.*/
Theme(
// Create a unique theme with "ThemeData"
data: ThemeData(
textTheme: /* Your Text Theme*/,
),
child: Container(
onPressed: () {},
child: Text("Your Text Here"),
),
);
2. Extending the parent theme
Theme(
// Find and extend the parent theme using "copyWith". See the next
// section for more info on `Theme.of`.
data: Theme.of(context).copyWith(textTheme: /* Provide your theme here! */),
child: Container(
child: Text("your text here"),
),
);
You could also use existed theme with a little changed:
Theme.of(context).textTheme.copyWith(
body1: Theme.of(context).textTheme.body1.copyWith(color: Colors.red),
)
Use DefaultTextStyle.merge to keep your theme and just change the color.
DefaultTextStyle.merge(
style: TextStyle(color: Colors.grey[400]),
child: Column(...),
)
If you are using the MaterialApp widget you could use the theme property of it and set different Text themes and call them anywhere in your app. For example the following code defines 3 different text themes:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: "Time Tracker",
theme: ThemeData(
textTheme: TextTheme(
headline: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold,color: Colors.blue),
title: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic,color: Colors.red),
body1: TextStyle(fontSize: 14.0, fontFamily: 'Hind',color: Colors.yellow),
),
),
home: LandingPage(),
);
}
}
You can then call a particular theme(headline) anywhere in your app like this:
Text('Home Page',style: Theme.of(context).textTheme.headline,)
Which gives you the headline TextTheme
I have functions for all my styles
TextStyle largeTextStyle() => TextStyle(fontSize: 150);
then I just do
Text("blah", style:largeTextStyle())

How to handle font size on different type of resolution

I am developing on a Pixel 2 simulator and of course, the font size that I use are very good for that phone.
When going on a iPhone 5S, I was expecting to see the font size to be reduced and be proportional to that screen resolution. Maybe Flutter does something, but is it not easy to see at the first place.
I found 'flutter_screenutil' plugin, but not working as expected. Probably because I don't put the right values for the ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context); The values are the 'screen size of the device in the design' of a iphone 6 (based on the example).
If I use the plugin with font size 100 on my Blackberry Motion(1080x1794), the result is what I expect. Using the same code with an iphone 6(750x1334), I should use font size 70. Not very useful.
Anybody has a nice way to handle the font size for different resolution without changing the font size value put in the code?
You can try using Google fonts. As Flutter works with custom fonts and you can apply a custom font across an entire app or to individual widgets. I was not able to see any issues while running ‘Pacifico’ family with font size 20. I also tested the following test code on Pixel 3 and Iphone 11, and do not see any issues.
Steps are simple as described in the flutter documentation:
Import the font files.
Declare the font in the pubspec. In may case I downloaded the
Pacifico from https://fonts.google.com/specimen/Pacifico?selection.family=Pacifico
Use a font in a specific widget.
My pubspec.yaml file:
flutter:
fonts:
- family: Pacifico
fonts:
- asset: fonts/Pacifico-Regular.ttf
My main.dart file :
` Widget build(BuildContext context) {
return MaterialApp(
title: 'Font test',
home: Scaffold(
backgroundColor: Colors.white,
body: SafeArea(
child: Column(
children: <Widget>[
Container(
height: 100.0,
width: 100.0,
color: Colors.red,
child: Text(
'container one',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 20.0,
),
),
),
Container(
height: 100.0,
width: 100.0,
color: Colors.blue,
child: Text(
'container two',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 20.0,
),
),
),
Container(
height: 100.0,
width: 100.0,
color: Colors.purple,
child: Text(
'container three',
style: TextStyle(
fontFamily: 'Pacifico',
fontSize: 20.0,
),
),
),
],
),
),
)
);
}
If you are having any issues with the fonts let me know, Also, as you see it’s the basic sample app. I would appreciate if you can help me out with the code sample to reproduce the issue. I do not have my Mac machine to help you with the screen shot. I will update this thread with the screen shot, once I have my Maclaptop