I'm building my first web project.
So the project is structured like so:
Web: Is a web site, one of the pages (RetailerAccess) is an actual app that should be compatible with all devices.
In order to do so I use platform check in main() and return the proper screen.
home: kIsWeb
? Stack(
children: [
Container(
// color: Colors.white,
child: AnimatedBackground(),
),
LayoutTemplate()
],
)
: RetailerAccess());
It works as expected.. When running on browser shows the web page and you can navigate to RetailerAccess while if running on iPad starts directly on RetailerAccess.
So far so good.
The weird part is that text widgets are drawn with a double green underline where running on device while when running on browser are drawn normally.
Are you aware of any bug that makes it behave like this or is there something I should set in a specific way?
I tried running it in Release mode on an old iPad 3 running iOS 9.3.5 but no changes..
Thank you very much for your help.
Update:
I realised I don't have a Scaffold in RetailerAccess wrapped it with a Material widget and it solved it.
Shouldn't a theme been passed to RetailerAccess anyways?
You are getting the error because you don't have a scaffold widget.
Try the code below:
It works perfectly:
home: kIsWeb
? Scaffold(
Stack(
children: [
Container(
// color: Colors.white,
child: AnimatedBackground(),
),
LayoutTemplate()
],
)
),
: Scaffold(
RetailerAccess()
);
I hope this helps.
Related
Problem description
I am not able to set white color with some opacity in Flutter on the Web.
Either
Colors.white24
or
Color(0x3DFFFFFF)
or even (suggested by #biruk-is)
Colors.white.withOpacity(0.24)
fail to show in Web app. It works as expected on Android and Windows, though...
How to reproduce?
Simply create a new Flutter App, with the standard template and modify the Scaffold as
return Scaffold(
backgroundColor: Colors.white24,
body: Center(
child: Text(
'Test text',
),
),
);
by doing this you get a colored background in Android or Windows, but on the web (both Chrome and Edge) show a white background...
Question:
Am i doing something wrong or did i misunderstood anything? Please kindly let me know (i am new to flutter (and web!) developement).
try using this:
const Color.fromRGBO(0, 0, 0, 0.3)
it gives me grey with 30% opacity.
Posible solution
If by using
Colors.white.withOpacity(0.24)
you do get the color white, you might still be able to get your desired color by calling
Colors.black.withOpacity(0.74)
If that is your case read the "What causes this issue?" section below.
What causes this issue?
The problem is a background color set up in the tree. If that background color is set to black, then setting Colors.black.withOpacity will always return black, or if the background color is white then setting Colors.white.withOpacity will always return white.
In my case, running
import 'package:flutter/material.dart';
void main() {
runApp(Container());
}
returns a black screen on Android, Windows and iOS, but a white screen in Chrome and Edge.
I also found out that there is already an issue in flutter that reports this problem.
use this:
Colors.white.withOpacity(0.5)
I am trying to do a responsive webapp and everything was working fine debugging it on the desktop firefox and I did the entire layout with the switch device option in the debug tools, but when I uploaded to an HTTP server and access it through my phone I get this screen
On chrome I have no problems and everything displays just right, on firefox on the other hand I have that weird blank space... At chrome that space was occupied by the "add to the homescreen" prompt, maybe that has something to do?
Any ideas would be greatly apreciated. I'm leaving the code just in case it helps...
void main() => runApp(MyApp2());
class MyApp2 extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Container(
color: Colors.red,
// child: Text('Hello World'),
),
),
);
}
}
I guess its useful to say that on chrome I get the expected behaviour of getting the full screen red except for the AppBar
Add to Homescreen button is feature of PWA so you should dive into index.html probably. Can you debug your Firefox Mobile and play with css a little bit. You can check official Firefox Remote Debugging Docs.
I have a Flutter issue I can't resolve. I tried all common things like flutter clean, restarting pc, wiping emulator data and few more things still getting stuck on white screen basically.
Launching lib\main.dart on Android SDK built for x86 in debug mode...
✓ Built build\app\outputs\flutter-apk\app-debug.apk.
Installing build\app\outputs\flutter-apk\app.apk...
Connecting to VM Service at ws://127.0.0.1:55863/xq7cW6jF1O8=/ws // this statement stays as its is
void main() => MaterialApp(
color: Colors.black,
home: Scaffold(
backgroundColor: Colors.black,
),
);
Basically connection to VM is not happening.
Edit
My dartDeveloperTool says unable to connect to vm service, but it opens in chrome and doesn't show any widget just clean dartDebugger tool.
Call to OpenGL ES API with no current context (logged once per thread).
Of course, it won't work.
Because you need to wrap it in runApp method. Like this:
void main() {
runApp(
MaterialApp(
color: Colors.black,
home: Scaffold(
backgroundColor: Colors.black,
),
),
);
}
But it's kinda bad practice to put your MaterialApp inside your main() function. Try to move it into a StatelessWidget or StatefulWidget.
Here's the example:
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: null, // Change null with your own widgets
),
);
}
}
Don't underestimate the importance of runApp() - If you don't use it - your app (which is one big widget) will result in an indefinite white screen because it has no constraints...
According to the documentation it:
/// Inflate the given widget and attach it to the screen. The widget is given constraints during layout that force it to fill the entire screen.
In my case, run app on debug mode working normal but I build file apk for release mode, app only show white screen. I finally found the solution. The problem was with my grade version. I downgrade version of grade in android/build.grade to 3.5.0, it works normally again.
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
}
I just faced a similar issue today and removing the Dart/Flutter extensions and installing them back solved it for me.
I also had the same issue while trying to run my app, I ran flutter clean, flutter doctor everything was green until I notice I was not connected to the internet.
If you are running the app for the first time, just restart your IDE and run your app with internet connection.
I had faced the same issue. In my case culprit was corporate proxy.
I have fixed it by setting Environment variables (User variables section) as I was using Windows machine.
HTTP_PROXY to http://proxy-ip:port
HTTPS_PROXY to http://proxy-ip:port
NO_PROXY to localhost,127.0.0.1
I faced the same issue of white screen with my friend project, the mistake was missing runApp() in Main.dart file, given below
void main() {
WidgetsFlutterBinding.ensureInitialized();
MultiProvider(
providers: [
ChangeNotifierProvider<CircuitProvider>(
create: (ctx) => CircuitProvider()),
],
child: const MyApp(),
),
}
So then it was replaced like,
void main() {
WidgetsFlutterBinding.ensureInitialized();
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider<CircuitProvider>(
create: (ctx) => CircuitProvider()),
],
child: const MyApp(),
),
);
}
I faced with this problem in working app so in my case I've restarted phone and it was ok. Also check memory on your device maybe you have no free space
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/basic.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
backgroundColor: Colors.blueGrey,
appBar: AppBar(
backgroundColor: Colors.blueGrey[900],
title: Text("I AM RICH"),
),
body: Center(
child:Image(
image: NetworkImage('https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'),
),
),
),
debugShowCheckedModeBanner: false,
)
);
}
In debug mode service extension and multiple permissions are enabled by default(in flutter)
as you are in release mode you have to add internet permission in androidmanifest.xml manually.( Just like you add it in native development)
navigate to android-> app-> src-> main-> AndroidManifest.xml and add this line outside of application scope.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
This happened to me today and i figured that my emulator was not connected to the internet.if you are facing this issue make sure your device you are using is connected to the internet.
Even I was facing the same problem until I checked that my device was not connected to the Internet and later turned it On the NetworkImage loaded the image from the URL string passed
You should use the network constructor. Please change this:
Image(
image: NetworkImage('https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500'),
)
to this:
Image.network('https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500')
You can use in Container in your body like this and wrap your Container with Center Widget
Container(
height: 90,
width: 90,
decoration: BoxDecoration(
image: DecorationImage(
image: NetworkImage("https://images.pexels.com/photos/462118/pexels-photo-462118.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500"),
//whatever image you can put here
fit: BoxFit.cover,
),
),
),
Check if your emulator is able to access internet first. Try loading any url in Google Chrome inside emulator. If not, add the dns address 8.8.8.8 to your network settings.
Try adding below line in main/Manifest file.
android:usesCleartextTraffic="true"
You can get more detail by implementing errorBuilder listener in Image.network widget.
I think your code is fine, to resolve this shutdown android studio & Emulator and restart it again.
It works for me, and it keeps happening to me, I guess its because it couldn't load the photo
CircleAvatar(
backgroundImage: NetworkImage('https://www.pinclipart.com/picdir/big/218-2189254_free-online-avatars-kid-characters-family-vector-for.png'));
Also check if your phone connected to the internet.
n debug mode service extension and multiple permissions are enabled by default(in flutter)
as you are in release mode you have to add internet permission in androidmanifest.xml manually.( Just like you add it in native development)
navigate to android-> app-> src-> main-> AndroidManifest.xml and add this line outside of application scope.
I have two files, main.dart and sections.dart.
I have defined a few widgets in section.dart and used them in main.dart file, when I run flutter run command changes are displayed on screen, but when I press R to hot reload, nothing changes on screen.
main.dart:
import 'package:flutter/material.dart';
import 'sections.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Share IDEASS',
home: Scaffold(
appBar: AppBar(
title: Text('IDEASs'),
),
body: ListView(
children: [
labelsSection,
],
),
),
);
}
}
Sections.dart:
import 'package:flutter/material.dart';
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
title: 'Share IDEAS',
);
}
}
Column labelSectionMethod(color,IconData icon, String label,String numbers){
return Column(
children: <Widget>[
Icon(icon, color:color),
Container(
child: Text(label,
style: TextStyle(color: color
),
),
),
Container(
child:Text(numbers,
style:TextStyle(color:color
),
),
),
],
);
}
Widget labelsSection=Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
labelSectionMethod(Colors.red,Icons.supervised_user_circle,"IDEASS","35"),
labelSectionMethod(Colors.red,Icons.favorite,"LIKE","22"),
labelSectionMethod(Colors.red,Icons.data_usage,"STREAK","12"),
],
),
);
Maybe because I am repeating:
return MaterialApp(
title: 'Share IDEAS',
);
in sections.dart, or something to do with main function.
UPDATE:
i moved content of sections.dart file in main.dart file still hot reload is not working.
After moving a file to another directory, Android Studio changed my imports to absolute paths.
import 'file:///C:/Users/svenv/AndroidStudioProjects/sample_screen_collection/lib/welcome/reading_list_card.dart';
i had to change it to this
import 'package:samplescreencollection/components/reading_list_card.dart';
This usually happens when you have change a lot of code, added states, changed a widget from stateless to state full, etc. Preforming a hot restart (ctrl +F5) will fix this issue.
If this seems to not work still, try removing the app from your phone/simulator/emulator (closing the app, and deleting it) and running debug/flutter run again. This should fix your issue
There have been issue reported to Flutter regarding this,https://github.com/flutter/flutter/issues/17155#issue-319238844
Basically it's like Flutter is not properly identifying the changes in order to generate updated build files (my personal understanding):
Solution
Simple solution is Clean the project and rebuild it.
I'm using Android studio Android Studio 3.6, Following steps illustrate how to clean the flutter project
Navigate to Tools in navigationbar
Select Flutter > Flutter Clean
Finally Run the Project again by clicking the green playicon
Sometimes flutter gets confused with large amounts of changes, and you need to perform a restart, especially when interacting with the entire App or main function.
You can pressCtrl+C to kill the flutter run process, and try launching it again. Additionally, if you run Flutter through IntelliJ or Android Studio, the flutter plugin includes a "Hot Restart" feature which accomplishes the same thing.
My situation was - Flutter would neither hot reload nor stop on a break-point nor even provide fresh builds to Android emulator, whereas iOS was fine.
I tried suggestions from above but nothing really worked, spent a day on these things.I upgraded from Android Studio 3.x to 4.1 and nothing changed.
What worked for me was:
Create a totally new Flutter sample project.
Validate it works - new versions are deployed, debugging and hot reloads work
Transfer my project into this new project. This pretty much involved replace of the lib and .git folders and manual updates of pubspec.yaml, AndroidManifest.xml and Info.plist (manual file updates took most of the effort, but still much less time than my several day attempts to make things work again)
Verify everything works.