Error: Method invocation is not a constant expression - android

Error: Method invocation is not a constant expression.
color: Colors.black.withOpacity(0.6),
i get this error when i try run this:
body: ListView.builder(
itemCount: 15,
itemBuilder: (context, i) => const ListTile(
title: Text("Bitcoin",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 24,
)),
subtitle: Text("\$20000",
style: TextStyle(
color: Colors.grey.withOpacity(0.6), //error in this line
fontWeight: FontWeight.w700,
fontSize: 14,
)),
),
),
Please helpto solve this

You need to remove the const before ListTile, because Colors.grey.withOpacity(0.6) isn't a constant value.
body: ListView.builder(
itemCount: 15,
itemBuilder: (context, i) => ListTile(
title: const Text("Bitcoin",
style: TextStyle(
fontWeight: FontWeight.w500,
fontSize: 24,
)),
subtitle: Text("\$20000",
style: TextStyle(
color: Colors.grey.withOpacity(0.6), //error in this line
fontWeight: FontWeight.w700,
fontSize: 14,
)),
),
),

Related

Do Update method needs to be in a form or can just simply inside a button as well?

So its just a general question that I would like to know because I'm still new in flutter development. So I just want to know does update method have to be inside a form or it can just simply put inside a button for example like save for profile pages. Heres my profile page. if anyone knew how to implement update inside this page it would be helpful
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter_user_profile/model/parking.dart';
class ParkingPage extends StatefulWidget {
#override
State<ParkingPage> createState() => _ParkingPageState();
}
class _ParkingPageState extends State<ParkingPage> {
User? user = FirebaseAuth.instance.currentUser;
Parking loginuser = Parking();
#override
void initState(){
super.initState();
FirebaseFirestore.instance
.collection('parkingTech')
.doc(user!.uid)
.get()
.then((value){
this.loginuser = Parking.fromMap(value.data());
setState(() {});
});
}
// #override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF121212),
appBar: AppBar(
backgroundColor: Color(0xFF121212),
elevation: 0.0,
title: Text('Parking Tech',
style:TextStyle(
color: Color(0xFFFFFFFF),
fontWeight: FontWeight.bold,
),
),
centerTitle:true,
actions: [
// Icon(Icons.menu,
// color:Colors.amber,
// size:40,
// )
]
),
body: Padding(
padding: const EdgeInsets.all(30.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children:[
Row(
mainAxisAlignment: MainAxisAlignment.center ,
children:[
CircleAvatar(
backgroundColor: Colors.amber,
radius: 100.0,
child: Center(
child: Text('P',
style:TextStyle(
fontSize: 80,
color: Color(0xFF121212),
fontWeight: FontWeight.bold,
),
),
)
),
],
),
SizedBox(height: 15),
Text("Parking time : ${loginuser.name} ",
style:TextStyle(
fontSize: 20,
color: Colors.grey,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 10),
Text('00 : 56 : 05',
style:TextStyle(
fontSize: 50,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 15),
Text('P15 AED',
style:TextStyle(
fontSize: 30,
color: Colors.blueGrey,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 15),
Text('On Parking ',
style:TextStyle(
fontSize: 15,
color: Colors.amber,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 15),
Text('End Parking',
style:TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 12),
Text('Extend Parking',
style:TextStyle(
fontSize: 20,
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 12),
],
),
),
);
}
}

Row widget contents are not getting displayed

I am trying to build an app which retrieves all the table data and display it on the screen.I have made it using a list builder widget .I wanted to add a header to each of the rows retrieved .I tried adding a row widget which contains the header.But it is not getting displayed .
Here is what the out List looks like
I wanted add did dname and age Header by making a row ..but its not visible
Here is what my code looks like
// build list view & its tile
ListView _buildPosts(BuildContext context, List<Post> posts) {
print('Hi');
Row(//This row is not getting displayed
children: [
Container(
child: const Text('Did',
style: TextStyle(
color: Colors.white,
),
),
),
Container(
child: const Text('Dname',style: TextStyle(
color: Colors.white,
),),
),
Container(
child: const Text('Age',style: TextStyle(
color: Colors.white,
),),
),
],
);
return ListView.builder(
itemCount: posts.length,
padding: const EdgeInsets.all(20),
itemBuilder: (context, index) {
return Card(
shadowColor: Colors.white,
color:const Color(0xFF303030),
elevation: 1,
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(posts[index].Did,style: const TextStyle(fontWeight: FontWeight.bold,
color:Colors.white,),),
Text(posts[index].Dname,style: const TextStyle(fontWeight: FontWeight.bold,
color:Colors.white,),),
Text(posts[index].Age,style: const TextStyle(fontWeight: FontWeight.bold,color:Colors.white,),
),
],
),
),
);
},
);
}
}
class HttpService {
Future<List<Post>> getPosts() async {
Response res = await get(
Uri.parse('http://localhost/localconnect/driver_change.php'));
print(res.body);
if (res.statusCode == 200) {
List<dynamic> body = jsonDecode(res.body);
List<Post> posts = body.map(
(dynamic item) => Post.fromJson(item),
).toList();
return posts;
} else {
throw "Unable to retrieve posts.";
}
}
}
The problem is that the row isn't used anywhere. So instead of using this so called _buildPosts you can simply return a listView.builder. Checkout the code below:
I have added 2 sets of solutions below:
ListView.builder(
itemCount: posts.length,
itemBuilder: (context, index) {
return Card(
shadowColor: Colors.white,
color: const Color(0xFF303030),
elevation: 1,
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Column(
children: [
Container(
child: const Text(
'Did',
style: TextStyle(
color: Colors.white,
),
),
),
Text(
posts[index].Did,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
Column(
children: [
Container(
child: const Text(
'Dname',
style: TextStyle(
color: Colors.white,
),
),
),
Text(
posts[index].Dname,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
Column(
children: [
Container(
child: const Text(
'Age',
style: TextStyle(
color: Colors.white,
),
),
),
Text(
posts[index].Age,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
],
),
),
);
},
),
This is the most simplest approach but as per your answer there is another complex approach:
ListView.builder(
itemCount: posts.length * 2,
itemBuilder: (context, index) {
if (index.isOdd) {
return Card(
shadowColor: Colors.white,
color: const Color(0xFF303030),
elevation: 1,
child: IntrinsicHeight(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
posts[index*2].Did,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Text(
posts[index*2].Dname,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
Text(
posts[index*2].Age,
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.white,
),
),
],
),
),
);
} else {
return Row(
//This row is not getting displayed
children: [
Container(
child: const Text(
'Did',
style: TextStyle(
color: Colors.white,
),
),
),
Container(
child: const Text(
'Dname',
style: TextStyle(
color: Colors.white,
),
),
),
Container(
child: const Text(
'Age',
style: TextStyle(
color: Colors.white,
),
),
),
],
);
}
},
),
It can't be displayed because it isn't used anywhere! Your _buildPosts() only returns Listview.builder().
Wrap your List view.builder() inside a Column, then move your Row() inside that Column() above Listview.builder().
The best and optimal solution for your case is use DataTable .It makes table data visualization so simple and it is very easy to use
#override
Widget build(BuildContext context) {
return DataTable(
columns: const <DataColumn>[
DataColumn(
label: Text(
'Did',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Dname',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
DataColumn(
label: Text(
'Age',
style: TextStyle(fontStyle: FontStyle.italic),
),
),
],
rows: const <DataRow>[
DataRow(
cells: <DataCell>[
DataCell(Text('1')),
DataCell(Text('Raj')),
DataCell(Text('34')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('Janine')),
DataCell(Text('43')),
DataCell(Text('Professor')),
],
),
DataRow(
cells: <DataCell>[
DataCell(Text('William')),
DataCell(Text('27')),
DataCell(Text('Associate Professor')),
],
),
],
);
}
you can render a list into row like this
rows: _list.map((cita) => DataRow(
cells: [
DataCell(Text(_list.telefono ?? '',style: const
TextStyle(
fontSize: 18,color: Colors.black
))),
DataCell(Text(_list.nombre ?? '',style: const
TextStyle(
fontSize: 18,color: Colors.black
))),
DataCell(Text(_list.nombre ?? '',style: const
TextStyle(
fontSize: 18,color: Colors.black
))),
]
)
I know my answer is far away from your asking question but this is the best approach to handle data table in flutter

launch a button(url) and then open browser in flutter

I want when the Sign Up is clicked, the browser be open and go to my website but i dont know how to do that.
here is my code, when i tap on Sign Up it doesn't work:
return Container(
child: GestureDetector(
onTap: () => launch("https://my.drclubs.ir/"),
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: "Don\`t have an Account?",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w500),
),
TextSpan(
text: "Sign Up",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold),
),
],
),
),
),
);
}
You can use a package called url_launcher here.
Example:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
const _url = 'https://flutter.dev'; /// Put your custom url here.
void main() => runApp(
const MaterialApp(
home: Material(
child: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Show Flutter homepage'),
),
),
),
),
);
void _launchURL() async =>
await canLaunch(_url) ? await launch(_url) : throw 'Could not launch $_url';
You can try this:
return Container(
child: GestureDetector(
onTap: _launchURL,
child: RichText(
text: TextSpan(
children: [
TextSpan(
text: "Don\`t have an Account?",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w500),
),
TextSpan(
text: "Sign Up",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.bold),
),
],
),
),
),
);

How can I place a divider between those list tiles

I'm new to coding and I've just started my first project, I'm just reading flutter's documentation and trying to do stuff on my own, but this one's been kinda tough, how can i place a divider between those list tiles?
I've tried some stuff like ListView.separated and similar alternatives, couldn't make it work on my code, probably cause the structure of it is all wrong somehow, any tips?
Widget build(BuildContext context) {
return Container(
child: Drawer(
child: ListView(
children: <Widget>[
UserAccountsDrawerHeader(
currentAccountPicture: CircleAvatar(
backgroundColor: Colors.blueGrey,
child: Text(
"LPI",
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.w700,
),
),
),
accountName: Text(
'Lucas Pereira Issler',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w700,
),
),
accountEmail: Text(
'lucas.issler#ftc.edu.br',
style: TextStyle(
fontSize: 14,
fontWeight: FontWeight.w400,
),
),
),
ListTile(
leading: Icon(
Icons.pets,
size: 40,
color: Colors.blueAccent,
),
title: Text('Adoção',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w800,
color: Colors.blueAccent,
)),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
leading: Icon(
Icons.remove_red_eye,
size: 40,
color: Colors.blueAccent,
),
title: Text('Desaparecidos',
style: TextStyle(
fontSize: 22,
fontWeight: FontWeight.w800,
color: Colors.blueAccent,
)),
onTap: () {
Navigator.pop(context);
},
),
You need to make a separate ListView.separated() for the ListTile. Checkout this DartPad, hope it helps.

TextField gets hidden when the keyboard pops in

import 'package:e_expense/home.dart';
import 'package:flutter/material.dart';
class SignUpPage extends StatefulWidget {
#override
_SignUpState createState() => _SignUpState();
}
class _SignUpState extends State<SignUpPage> {
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: false,
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.green,
),
centerTitle: true,
title: Text(
"Welcome to E-Expense",
style: TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat',
color: Colors.green),
),
elevation: 0.0,
backgroundColor: Color(0x00000000).withOpacity(0),
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Flexible(
fit: FlexFit.loose,
child: Container(
child: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15.0, 50.0, 0.0, 0.0),
child: Text(
"Signup",
style: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
),
),
),
Container(
padding: EdgeInsets.fromLTRB(270.0, 50.0, 0.0, 0.0),
child: Text(
".",
style: TextStyle(
color: Colors.green,
fontSize: 80.0,
fontWeight: FontWeight.bold,
),
),
)
],
),
),
),
Container(
padding: EdgeInsets.only(top: 35.0, left: 20.0, right: 20.0),
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(
labelText: 'USER NAME',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'EMAIL',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'REPEAT PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 40.0),
ButtonBar(
alignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Text(
'CANCEL',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.normal,
color: Colors.black,
),
)),
RaisedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => HomePage(),
fullscreenDialog: false));
},
color: Colors.green,
child: Text(
'SIGNUP',
style: TextStyle(
fontFamily: 'Montserrat',
fontSize: 15.0),
),
textColor: Colors.white,
)
],
)
],
),
),
],
));
}
}
Hello all this is my first flutter app. I have been trying to create a sign up page with few textfields. Everything looks good, but whenever I click on any textfield softkeyboard shows and hide the text filed(please see the attached images)
A few changes:
Set resizeToAvoidBottomPadding true
Remove the Flexible Widget
Wrap your first Column inside SingleChildScrollView
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomPadding: true,
appBar: AppBar(
iconTheme: IconThemeData(
color: Colors.green,
),
centerTitle: true,
title: Text(
"Welcome to E-Expense",
style: TextStyle(
fontWeight: FontWeight.bold,
fontFamily: 'Montserrat',
color: Colors.green),
),
elevation: 0.0,
backgroundColor: Color(0x00000000).withOpacity(0),
),
body: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
child: Stack(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(15.0, 50.0, 0.0, 0.0),
child: Text(
"Signup",
style: TextStyle(
fontSize: 80.0,
fontWeight: FontWeight.bold,
),
),
),
Container(
padding: EdgeInsets.fromLTRB(270.0, 50.0, 0.0, 0.0),
child: Text(
".",
style: TextStyle(
color: Colors.green,
fontSize: 80.0,
fontWeight: FontWeight.bold,
),
),
)
],
),
),
Container(
padding: EdgeInsets.only(top: 35.0, left: 20.0, right: 20.0),
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextField(
decoration: InputDecoration(
labelText: 'USER NAME',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'EMAIL',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 10.0),
TextField(
decoration: InputDecoration(
labelText: 'REPEAT PASSWORD',
labelStyle: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.bold,
color: Colors.grey,
),
focusedBorder: UnderlineInputBorder(
borderSide: BorderSide(
color: Colors.green,
),
),
),
),
SizedBox(height: 40.0),
ButtonBar(
alignment: MainAxisAlignment.end,
children: <Widget>[
FlatButton(
onPressed: () {},
child: Text(
'CANCEL',
style: TextStyle(
fontFamily: 'Montserrat',
fontWeight: FontWeight.normal,
color: Colors.black,
),
)),
RaisedButton(
onPressed: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => null,
fullscreenDialog: false));
},
color: Colors.green,
child: Text(
'SIGNUP',
style: TextStyle(
fontFamily: 'Montserrat', fontSize: 15.0),
),
textColor: Colors.white,
)
],
)
],
),
),
),
],
),
));
}
Try wrapping the widget that contains those TextFields with a SingleChildScrollView.
Please use
android:windowSoftInputMode="adjustPan"
in forAndroidManifest.xml for activity SignUpPage

Categories

Resources