I have issues with my Material Widgets (AppBar widget as an example). I tried to find a solution in other SO questions but nothing is working. In production App bar's back button, and some other material files aren't working too, even material clipboard.
[Edit] I found out that the issue was with not supported language (Turkmen/Russian - two main languages of my app) in some non-global (Chinese, Hindu) phones. There were no issues with other iOS / Android devices.
So there is my MaterialApp file:
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:peydaly.market/blocs/markets_provider.dart';
import 'package:peydaly.market/generated/l10n.dart';
import 'package:peydaly.market/screens/loading_screen.dart';
import 'blocs/markets_provider.dart';
class App extends StatelessWidget {
#override
Widget build(context) {
return MarketsProvider(
child: MaterialApp(
localizationsDelegates: [
S.delegate,
GlobalMaterialLocalizations.delegate,
GlobalWidgetsLocalizations.delegate,
GlobalCupertinoLocalizations.delegate,
],
supportedLocales: S.delegate.supportedLocales,
debugShowCheckedModeBanner: false,
darkTheme: buildDarkMode(),
theme: buildLightMode(),
title: 'Peýdaly.Market',
home: LoadingScreen(),
),
);
}
ThemeData buildLightMode() {
return ThemeData.light().copyWith(
buttonTheme: ButtonThemeData(
minWidth: 10,
),
primaryColor: Colors.grey[100],
backgroundColor: Colors.white,
iconTheme: IconThemeData(
color: Colors.grey[900],
),
appBarTheme: AppBarTheme(
centerTitle: true,
brightness: Brightness.light,
iconTheme: IconThemeData(
color: Colors.grey[900],
),
actionsIconTheme: IconThemeData(
color: Colors.grey[900],
),
// brightness: Brightness.light,
color: Colors.white,
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.grey[900],
fontWeight: FontWeight.w600,
),
),
elevation: 0,
),
);
}
ThemeData buildDarkMode() {
return ThemeData.dark().copyWith(
buttonTheme: ButtonThemeData(
minWidth: 10,
),
backgroundColor: Colors.grey[900],
primaryColor: Colors.grey[850],
iconTheme: IconThemeData(
color: Colors.white,
),
appBarTheme: AppBarTheme(
centerTitle: true,
brightness: Brightness.dark,
iconTheme: IconThemeData(
color: Colors.white,
),
actionsIconTheme: IconThemeData(
color: Colors.white,
),
// brightness: Brightness.dark,
color: Colors.grey[900],
textTheme: TextTheme(
headline6: TextStyle(
color: Colors.grey[100],
fontWeight: FontWeight.w600,
),
),
elevation: 0,
),
);
}
}
My pubspec.yaml file:
name: peydaly.market
description: Subscription Online Groceries delivery service app for mobile devices.
version: 0.2+23
environment:
sdk: ">=2.7.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
flutter_localizations:
sdk: flutter
animations: ^1.1.2
cached_network_image: ^2.2.0+1
carousel_slider: ^2.3.1
crypto: ^2.1.5
cupertino_icons: ^1.0.0
cupertino_rounded_corners: ^1.0.3
date_format: ^1.0.9
flare_flutter: ^2.0.6
flutter_polyline_points: ^0.2.4
futuristic: ^0.1.3
google_maps_flutter: ^1.0.6
http: ^0.12.1
intl: 0.16.1
location: ^3.0.2
modal_bottom_sheet: ^0.2.2
path_provider: ^1.6.14
rive: ^0.6.3
rxdart: ^0.24.1
shimmer: ^1.1.1
splashscreen: ^1.3.4
sqflite: ^1.3.1+1
superellipse_shape: ^0.1.5
dev_dependencies:
flutter_test:
sdk: flutter
pedantic: ^1.4.0
flutter:
uses-material-design: true
generate: true
assets:
- assets/social/
- assets/playlists/
- assets/avatar/
- assets/sql/
flutter_localizations:
sdk: flutter
flutter_intl:
enabled: true
class_name: S
main_locale: tm
flutter_icons:
android: "launcher_icon"
ios: true
image_path: "assets/icon/icon.png"
And my debug console error.
═══════ Exception caught by widgets library ═══════════════════════════════════
The following assertion was thrown building AppBar(dirty, dependencies: [_LocalizationsScope-[GlobalKey#2fd79]], state: _AppBarState#848b3):
No MaterialLocalizations found.
AppBar widgets require MaterialLocalizations to be provided by a Localizations widget ancestor.
The material library uses Localizations to generate messages, labels, and abbreviations.
To introduce a MaterialLocalizations, either use a MaterialApp at the root of your application to include them automatically or add a Localization widget with a MaterialLocalizations delegate.
The specific widget that could not find a MaterialLocalizations ancestor was: AppBar
dirty
dependencies: [_LocalizationsScope-[GlobalKey#2fd79]]
state: _AppBarState#848b3
The ancestors of this widget were
Scaffold
dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], Directionality, MediaQuery, _EffectiveTickerMode]
state: ScaffoldState#0f235(tickers: tracking 2 tickers)
CitySelectionScreen
dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], MarketsProvider]
state: _CitySelectionScreenState#fb711
MaterialApp
state: _MaterialAppState#44c02
MarketsProvider
App
...
The relevant error-causing widget was
AppBar
lib/screens/city_selection_screen.dart:53
When the exception was thrown, this was the stack
#0 debugCheckHasMaterialLocalizations.<anonymous closure>
package:flutter/…/material/debug.dart:74
#1 debugCheckHasMaterialLocalizations
package:flutter/…/material/debug.dart:94
#2 _AppBarState.build
package:flutter/…/material/app_bar.dart:509
#3 StatefulElement.build
package:flutter/…/widgets/framework.dart:4744
#4 ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4627
...
════════════════════════════════════════════════════════════════════════════════
════════ Exception caught by widgets library ═══════════════════════════════════
No MaterialLocalizations found.
The ancestors of this widget were
Scaffold
dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], Directionality, MediaQuery, _EffectiveTickerMode]
state: ScaffoldState#0f235(tickers: tracking 2 tickers)
CitySelectionScreen
dependencies: [_InheritedTheme, _LocalizationsScope-[GlobalKey#2fd79], MarketsProvider]
state: _CitySelectionScreenState#fb711
MaterialApp
state: _MaterialAppState#44c02
MarketsProvider
App
...
The relevant error-causing widget was
AppBar
lib/screens/city_selection_screen.dart:53
════════════════════════════════════════════════════════════════════════════════
So the issue was with App localization issues of Material UI elements on unsupported devices, so to add support to it, I added these lines to my App file:
supportedLocales: [
const Locale('ru'),
const Locale('tm'),
...S.delegate.supportedLocales,
],
Related
this is my pubspec file this is happening every time I want to use the assets image. i use every combination for in asset
name: profile1
description: A new Flutter project.
publish_to: 'none'
version: 1.0.0+1
environment:
sdk: ">=2.16.2 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^1.0.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^1.0.0
flutter:
uses-material-design: true
assets:
- assets/*
[enter image description here][1]
[1]: https://i.stack.imgur.com/EJ75N.png
the error is in my pubspec file I cant use my asset images
Error on line 61, column 4: Expected a key while parsing a block mapping.
╷
61 │ assets:
│ ^
╵
Please correct the pubspec.yaml file at
C:\Users\Akshay\AndroidStudioProjects\profile1\pubspec.yaml
Process finished with exit code 1
dart code go to the image part
import 'package:flutter/material.dart';
void main() => runApp(MaterialApp(
home: profile(),
));
class profile extends StatelessWidget {
const profile({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.black,
title: Text('profile'),
),
body: Padding(
padding: EdgeInsets.fromLTRB(19, 15, 0, 0),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Center(
using image in image asset Image but the error is in the pubspec file. i use various methods in dart that is not working
child: CircleAvatar(
backgroundImage: new AssetImage("assets/img1.jpeg"),
// backgroundColor: Colors.cyanAccent,
radius: 70,
),
),
),
),
backgroundColor: Color.fromARGB(255, 26, 26, 26),
);
}
}
The pubspec.yaml is indentation sensitive.
assets must be without any indentation at the start of the line. The individual asset entries are one tab level deeper in.
Pubspec.yaml is not set properly.
You should make the asset part as follows :
This is just an example. If you want, you can also export the whole folder as just lib/img/ . The important thing here is the alignment of the codes.
the original Material Icons in FLutter Icon(Icons.access_alarms) stop working every time I try to define a new custom font for my app.
What I do is to import the font in the pubspec.yamlfile correctly, include the "uses-material-design:true" and define the font in main ThemeData using the fontFamily property.
flutter:
uses-material-design: true
assets:
- images/
fonts:
- family: Core Sans C
fonts:
- asset: Fonts/coresansc-45regular-webfont.ttf
style: normal
weight: 500
screenshot of the pubspec.yaml including the font definition
ThemeData kThemeLB = ThemeData(
fontFamily: 'Core Sans C',
backgroundColor: kBgColor1,
primaryColor: kMainColor,
// primarySwatch: kMainColor,
textTheme: const TextTheme(
bodyText1: TextStyle(
color: kSecondaryColor,
),
),
scaffoldBackgroundColor: kBgColor1);
And my main.dart
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: kThemeLB,
home: LoadLogin(),
My fonts DIR
Fonts dir
I can use FontAwesome Icons correctly, but still I'd like to know what the issue is.
I've been looking for a solution for a long time now but I can't seem to figure out what's the problem here. Any help is much appreciated thank!
Try move your files inside your assets dir, for example:
- assets
-- fonts
--- **"your fonts inside fonts dir"**
after that modify your pubspec file, like this:
fonts:
- family: coreSansC
- fonts:
- asset: assets/fonts/coresansc-45regular-webfont.ttf
finally modify your ThemeData, like this:
ThemeData kThemeLB = ThemeData(
fontFamily: 'coreSansC',
backgroundColor: kBgColor1,
primaryColor: kMainColor,
// primarySwatch: kMainColor,
textTheme: const TextTheme(
bodyText1: TextStyle(
color: kSecondaryColor,
),
),
I recommend you use camelCase names.
I'm new in flutter, just started since last week. I'm learning from the online lesson, then i want to load a image in my flutter app. But there was an error which is :
════════ Exception caught by image resource service ════════════════════════════════════════════════
The following assertion was thrown resolving an image codec:
Unable to load asset: assets/image2.jpg
When the exception was thrown, this was the stack:
#0 PlatformAssetBundle.load (package:flutter/src/services/asset_bundle.dart:225:7)
#1 AssetBundleImageProvider._loadAsync (package:flutter/src/painting/image_provider.dart:670:14)
Image provider: AssetImage(bundle: null, name: "assets/image2.jpg")
Image key: AssetBundleImageKey(bundle: PlatformAssetBundle#9f84b(), name: "assets/image2.jpg", scale: 1.0)
════════════════════════════════════════════════════════════════════════════════════════════════════
Below is the main.dart code :
import 'package:flutter/material.dart';
void main() =>
runApp(MaterialApp(
home: Home(),
));
class Home extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar : AppBar(
title : Text(
"Welcome to HLH",
style: TextStyle(
fontSize: 20.0,
color: Colors.black,
fontFamily: "Goldman",
),
),
centerTitle: false,
backgroundColor: Colors.amber,
),
body: Center(
child: Image(
image : AssetImage("assets/image2.jpg"),
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Text("Login"),
),
);
}
}
Can anyone exists me how to solve it? I'm wondering is it the image problem? Thanks^^
This can be a error of not declaring the assets in your pubspec.yaml file.
flutter:
assets:
- images/
Add this to your pubspec.yaml file and then flutter pub get on the app root folder in terminal
I think this problem was caused due to the image path!
Make sure that the path of the image correctly in pubspec.yaml (must be the same with the image path directory).
like this:-
i saved my image in this path "assets/images/cat.png"
Pubspec.yaml file :
flutter:
assets:
- assets/images/
I tried to use a custom font for my AppBar, but it didn't change. I tried with two different fonts, RobotoMono and DancingScript, but nothing, the app did't change the font. I tried to unistall the app from the virtual phone too, too create another virtual device, but nothing. That's my main.dart :
import 'package:flutter/material.dart';
import 'background_image_task-9.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Blumax',
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'Dancing',
primarySwatch: myColour
),
home: BackgroundImage(
),
);
}
}
const MaterialColor myColour = const MaterialColor(
0xFF0009FF,
const <int, Color>{
50: const Color(0xFF0009FF),
100: const Color(0xFF0009FF),
200: const Color(0xFF0009FF),
300: const Color(0xFF0009FF),
400: const Color(0xFF0009FF),
500: const Color(0xFF0009FF),
600: const Color(0xFF0009FF),
700: const Color(0xFF0009FF),
800: const Color(0xFF0009FF),
900: const Color(0xFF0009FF),
},
);
This is where i use the custom font, background_image_task-9.dart :
import 'package:flutter/material.dart';
class BackgroundImage extends StatelessWidget{
#override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text('Blumax', style: TextStyle(
fontWeight: FontWeight.w500,
fontFamily: 'RobotoMono',
fontSize: 40
),),
centerTitle: true,
),
body: Container(
decoration: BoxDecoration(
image: DecorationImage(image: AssetImage("assets/blumax.jpg"), fit: BoxFit.cover),
),
),
);
}
}
And that's my pubspec.yaml :
name: iphone_prj
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
english_words: ^3.1.0
cupertino_icons: ^0.1.2
dev_dependencies:
flutter_test:
sdk: flutter
flutter:
assets:
- assets/
uses-material-design: true
fonts:
- family: RobotoMono
fonts:
- asset: assets/fonts/RobotoMono-Bold.ttf
- family: DancingScript
fonts:
- asset: assets/fonts/DancingScript-Bold.ttf
weight: 300
Just add your font-family name properly in your main ThemeData as per pubspec.yaml file
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Blumax',
debugShowCheckedModeBanner: false,
theme: ThemeData(
fontFamily: 'DancingScript',
primarySwatch: myColour
),
home: BackgroundImage(
),
);
}
}
The problem in your case is your font-family name is DancingScript and your providing it in the ThemeData as Dancing. So it will not effect to your app fonts.
Also, in your BackgroundImage class you have added RobotoMono font. But, the "fontWeight: FontWeight.w500" you have added is not matching as per your pubspec.yaml as you have added there RobotoMono-Bold fonts.
So, by matching your font names and font style will effect your app fonts as per your requirements.
There can be many reasons that avoid changing font in flutter :
1- Notice that the pubsec.yaml file is Space sensitive , It means that you need to use 2 or 4 spaces for declaring blocks. That's why you have to use indentation before declaring fonts. you can see the correct example in the snippet below:
flutter:
fonts:
- family: Raleway
fonts:
- asset: fonts/Raleway-Regular.ttf
- asset: fonts/Raleway-Italic.ttf
style: italic
- family: RobotoMono
fonts:
- asset: fonts/RobotoMono-Regular.ttf
- asset: fonts/RobotoMono-Bold.ttf
weight: 700
2- As the document mentioned the Folder Structure should be like :
awesome_app/
fonts/
Raleway-Regular.ttf
Raleway-Italic.ttf
RobotoMono-Regular.ttf
RobotoMono-Bold.ttf
3- Add your font-family name correctly in your main ThemeData as per pubspec.yaml file:
MaterialApp(
title: 'Custom Fonts',
// Set Raleway as the default app font.
theme: ThemeData(fontFamily: 'Raleway'),
home: MyHomePage(),);
your using asset prefix while declaring fonts in pubspec.yaml
Here is the solution https://stackoverflow.com/a/59113335/5557479
I was trying to create the Floating Action button but I am missing icon.
My code is:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Scaffold(
appBar: AppBar(
backgroundColor: Colors.blue,
centerTitle: true,
title: Text(
"Lessons of Flutter",
style: TextStyle(
color: Colors.white,
),
),
),
body: Center(
child: const Text('Press the button below!')
),
floatingActionButton: FloatingActionButton(
onPressed: () {
// Add your onPressed code here!
},
child: Icon(Icons.mouse),
backgroundColor: Colors.green,
),
),
);
}
}
it is a screen from the virtual device.( You can see icon looks weird.)
To use this class, make sure you set uses-material-design: true in your project's pubspec.yaml file in the flutter section. This ensures that the MaterialIcons font is included in your application. This font is used to display the icons. For example:
Refer this link: https://api.flutter.dev/flutter/material/Icons-class.html
The Icon is not rendering because of the missing font from the material design library. You have to enable the material design library in your pubspec.yml file as given below,
flutter:
uses-material-design: true
Just make uses-material-design to true and the error will be gone. This ensures that the MaterialIcons font is included in your application. This font is used to display the icons Here is the official docs of Icon class