How do I fix it const issue on appBar ?
While you are adding title:Text(..) AppBar is not const anymore. Therefore, the parent MaterialApp cant be const.
You can do
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(
title: const Text("this is te"),
),
),
);
}
Your code will have diagnostic-messages on error:
const_with_non_const
// correct
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(),
),
);
}
// fail
import 'package:flutter/material.dart';
void main() {
runApp(
const MaterialApp(
home: Scaffold(
appBar: AppBar(),
),
),
);
}
While Text('AppBar Demo') is a const, it is preferred to add const before it.
// not preferred
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('AppBar Demo'),
),
),
),
);
}
// preferred
import 'package:flutter/material.dart';
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('AppBar Demo'),
),
),
),
);
}
Related
void main() {
runApp(
const MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
this is my code and there is showing "the constructor called is not a constructor"
Error message-
lib/main.dart:8:17: Error: Cannot invoke a non-'const' constructor where a const expression is expected.
Try using a constructor or factory that is 'const'.
appBar: AppBar(
^^^^^^
tried adding 'const' before scaffold
I am sorry for the silly question. I am just a beginner. can't find the solution anywhere else.
To be a bit more precise: AppBar doesn't have a const constructor, so you can't use the const keyword. However, Text does have a const constructor, so you can (should) use const for that one.
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('I am Rich'),
),
),
),
);
}
Just remove const
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
Remove the 'const' before MaterialApp. It should work fine.
the correct code should be
void main() {
runApp(
MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text('I am Rich'),
),
),
),
);
}
Please help me. I can't do basic navigation operations
I can't go to my shared page. I don't know why. When I want to route to another page with Navigator.push accrue to an error and I capture my error and add that at bottom of my question.
this is my main page
import 'package:flutter/material.dart';
import 'package:like/shared.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: homeScreen(context),
);
}
Scaffold homeScreen(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("shared"),
),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Shared()),
);
},
child: Text("go")),
),
);
}
}
this is my shared page
import 'package:flutter/material.dart';
import 'package:like/main.dart';
void main() => runApp(Shared());
class Shared extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Material App',
home: Scaffold(
appBar: AppBar(
title: Text('Material App Bar'),
),
body: Center(
child: Container(
child: Text('Hello World'),
),
),
),
);
}
}
Both of them do not work.
For more information about the error, you can see the photo of the error
Navigator.push(
context,
MaterialPageRoute(builder: (context) => Shared()),
);
Shared() is not a widget
remove this line from the top of your page:
import 'package:like/shared.dart';
Shared() is a package
import 'package:flutter/material.dart';
void main() {
runApp(RecipeApp());
}
class RecipeApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final ThemeData theme = ThemeData();
return MaterialApp(
title: 'Recipe Calculator',
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(
primary: Colors.grey,
secondary: Colors.black,
),
),
home: const MyHomePage(title: 'Recipe Calculator'),
);
}
}
This is the current code and above is the current output as per the code, the color remains blue and white instead of grey and black
running on my emulator using your code it works, try restarting your app completely.
import 'package:flutter/material.dart';
void main() {
runApp(RecipeApp());
}
class RecipeApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
final ThemeData theme = ThemeData();
return MaterialApp(
title: 'Recipe Calculator',
theme: theme.copyWith(
colorScheme: theme.colorScheme.copyWith(
primary: Colors.grey,
secondary: Colors.black,
),
),
home: const MyHomePage(title: 'Recipe Calculator'),
);
}
}
class MyHomePage extends StatelessWidget {
final title;
const MyHomePage({this.title});
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(title),
),
);
}
}
Try below code hope its helpful to you ,I think you can used Scaffold Widget refer AppBar class here and refer Scaffold class here
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.grey,//change color on your need
title: Text(
'BottomNavigationBar Sample',
),
),
body:Container(),//or your widget
);
Your result screen->
I am pretty new in Dart and I have tried the following:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter App',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Column(children: [
Card(
child: Text('CHART!!'),
),
Card(child: Text('LIST OF TX'),)
],),
);
}
}
output is:
Why the children of the column does not be placed next to each other?
Change the Column to Row if you want them to be next to each others horizontally.
To place the children next to each other use the Row widget which is very similar to Column. Please see the code below.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter App',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Flutter App'),
),
body: Row(children: [
Card(
child: Text('CHART!!'),
),
Card(child: Text('LIST OF TX'),)
],),
);
}
}
when returning MaterialApp flutter shows error as follow "error: 1 required argument(s) expected, but 0 found. (not_enough_required_arguments at [demo] lib/main.dart:9)"Here is the screenshot
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
import 'package:solution_02_category_widget/category.dart';
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Category(
name: _categoryName,
color: _categoryColor,
iconLocation: _categoryIcon,
),
),
),
);
}
}
You should wrap the title argument under AppBar Widget. You can in turn wrap the appBar argument inside the Scaffold. The scaffold is like an empty page that gives your app the white background. Below is a sample code implementation:
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
// debugShowMaterialGrid: true,
theme: ThemeData(
brightness: Brightness .light,
accentColor: Colors.deepPurple,
primarySwatch: Colors.deepOrange),
home: Scaffold(
appBar: AppBar(
title: Text('Some text'),
),
),
);
}
}
Your code is working fine without error if i remove the Category() widget. The below code is working fine:
import 'package:flutter/material.dart';
const _categoryName = "Cake";
const _categoryIcon = Icons.cake;
const _categoryColor = Colors.green;
void main() {
runApp(UnitConverterApp());
}
class UnitConverterApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Unit Converter',
home: Scaffold(
backgroundColor: Colors.green[100],
body: Center(
child: Container(),
),
),
);
}
}
Maybe the problem is in the Category() widget so if you post it i can check it for you.