trying to run flutter program and keep getting this error "the method 'HomeScreen' isn't defined for the type 'MyApp' - flutter

trying to run flutter program and keep getting this error "the method 'HomeScreen' isn't defined for the type 'MyApp' i need and answer to this problem quickly
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Food Delivery UI',
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: Colors.grey[50],
primaryColor: Colors.deepOrangeAccent,
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}

I think you miss import file of HomeScreen at top of the main.dart.
HomeScreen class wasn't define in main.dart file as per your code.
Or maybe class name is something else. Check it again.

Chances are Home Screen is in another .dart file. You'll need to import this dart file into your main.dart for it to work. Super easy fix just add this part at the top
import 'package:flutter/material.dart';
import 'package:<projectname>/lib/<subfolder if you have one>/HomeScreen.dart'
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Food Delivery UI',
debugShowCheckedModeBanner: false,
theme: ThemeData(
scaffoldBackgroundColor: Colors.grey[50],
primaryColor: Colors.deepOrangeAccent,
primarySwatch: Colors.blue,
),
home: HomeScreen(),
);
}
}

Related

Flutter title doesn't change

I'm trying to change the title but despite the fact I changed the title it still shows the previous title on the tab of the browser.
void main() {
runApp(Myapp());
}
class Myapp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'New title ', : but still shows the old title on the browser
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
scaffoldBackgroundColor: Colors.black,
),
home: NavScreen(),
);
}
}
If you are changing the title during the run time, you just need to wrap it with the stateful Widget.
If your App title is static and does not change after a build, try clearing the flutter cache and rebuild your application again.

Debug mode banner is not removed

I am working on a Flutter app and want to hide the debug mode banner.
This is my code for main.dart:
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
FlutterError.onError = FirebaseCrashlytics.instance.recordFlutterError;
//FirebaseCrashlytics.instance.crash();
runApp(new MaterialApp(
debugShowCheckedModeBanner: false,
home: new MyApp(),
));
}
The debug mode banner is not removed, it is shown as always.
What do I need to change to get it removed?
Debug banner does not remove from main method, you just follow the example and you bind your MyApp() with the MaterialApp()
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: NextPage(),
);
}
}

Flutter mobile device image screen to large

I am currently using android studio flutter to test my prototype I manage to run my template successfully onto the mobile device however template is to large and I dont know how to make the template smaller so that it would fit onto the screen
when you select this link you can be able to see what it looks like:
mobile device image link
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32,47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Center(
child: Image.network("https://i.stack.imgur.com/deB5P.png", fit: Boxfit.fill),
)
title: 'Flutter Demo',
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
),
),
);
}
}
You can use box constraints by giving width and height properties. If you want your app to be responsive you can use MediaQuery. And for image you can use fit property like Image.asset("image", fit: BoxFit.cover), to know more about constants of BoxFit refer this.
import 'package:flutter/material.dart';
final Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData.dark().copyWith(scaffoldBackgroundColor: darkBlue),
debugShowCheckedModeBanner: false,
home: YourImage(),
);
}
}
class YourImage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Container(
child: Center(
child: Image.asset("assets/images/logo.png", fit: BoxFit.fill),
),
);
}
}

Change background color used in Recents apps screen with Flutter app

How can I change the color shown in the app switcher of Android. Right now the color comes up with a gray background, but I want it to be red.
This is the app code.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
color: Colors.red,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: Material(
child: Text('Check color in app switcher.'),
),
);
}
}
The color parameter of MaterialApp has no effect.
If you are using the MaterialApp you would use the color attribute to change the color of the app bar in the switcher.
You could also use the SystemChrome.setApplicationSwitcherDescription method which you would find in package:flutter/services.dart

Recommended Dark/Light or custom Theme implementation in Flutter Cupertino

I’m currently implementing an iOS style app in flutter using the CupertinoApp class and Cupertino widgets. Now I would like to implement a different Theme for the application (like feedly did), so that the user can switch between both during runtime.
Unlike a MaterialApp, CupertinoApp has no property to set the theme.
MaterialApp with theme property:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
CupertinoApp without theme property:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
// theme: ThemeData(
// primarySwatch: Colors.blue,
// ),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
It looks like sb. is working on a CupertinoTheme which actually seems to be a Theme for a MaterialApp and not for a CupertinoApp.
What's the recommended way to define themes and change them during runtime in a Cupertino style app (e.g. Dark and Light Theme)?
First of all CupertinoThemeData is an alternative for ThemeData:
import 'package:flutter/cupertino.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return CupertinoApp(
title: 'Flutter Demo',
theme: CupertinoThemeData(),
home: MyHomePage(),
);
}
}
CupertinoThemeData has a 'raw' property which contains these properties:
Brightness, primaryColor, primaryContrastingColor, textTheme and so on.
Would you want to choose either dark or light? that's too easy!!
CupertinoThemeData.raw(Brightness.light)
CupertinoThemeData.raw(Brightness.dark)
but if you want to change it during runtime you should write a function:
CupertinoThemeData generalTheme(Brightness brightness) {
CupertinoThemeData _basicCupertinoTheme = CupertinoThemeData.raw(
brightness,
...
}
As you see, you can send an argument to function but don't forget to use setState method.
For more info check out CupertinoThemeData.raw constructor
you can use the following package to control dark and light themes, also the theme is saved directly into shared preferences so when you reopen the application, the latest theme you chose will be loaded,use the package as provided in the read me section.
https://pub.dartlang.org/packages/dynamic_theme