How to make widgets not overlap in Stack - flutter-layout

I have ListView.seperated and a TextFormField in a Stack, how can I make those widgets not overlap?
Stack(
children: [
Builder(builder: (context) {
if (messages == null) {
return const Center(child: CircularProgressIndicator());
}
return RefreshIndicator(
onRefresh: () async {
ref
.read(messagesProvider.notifier)
.clearMessagesForChat(chat['id']);
},
child: ListView.separated(
controller: chatScrollController,
itemCount: messages.length,
separatorBuilder: (context, index) {
return const Divider(
height: 1,
);
},
itemBuilder: (BuildContext context, int index) {
final message = messages[index];
return ListTile(
contentPadding:
const EdgeInsets.symmetric(horizontal: 16, vertical: 6),
leading: GestureDetector(
onTap: () => showModalBottomSheet(
context: context,
builder: (context) {
return UserProfile(message['author']);
}),
child:
getProfilePicture(context, message['author'], 30)),
title: Row(
children: [
Text(
message['author']['username'],
style: const TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(
width: 16,
),
Text(
DateFormat.yMMMd().format(
DateTime.parse(message['created_at']).toLocal()),
style:
const TextStyle(color: Colors.grey, fontSize: 12),
)
],
),
subtitle: Text(
message['content'],
style: const TextStyle(fontSize: 16),
),
onTap: () {},
);
},
),
);
}),
Align(
alignment: Alignment.bottomLeft,
child: Container(
padding: const EdgeInsets.only(left: 10, bottom: 10, top: 10),
height: 60,
width: double.infinity,
color: Colors.white,
child: Row(
children: <Widget>[
const SizedBox(
width: 15,
),
Expanded(
child: TextField(
controller: messageController,
keyboardType: TextInputType.multiline,
minLines: 1,
maxLines: null,
onSubmitted: (_) => sendMessage(chat['id']),
decoration: const InputDecoration(
hintText: 'Write message...',
hintStyle: TextStyle(color: Colors.black54),
border: InputBorder.none),
),
),
const SizedBox(
width: 15,
),
FloatingActionButton(
onPressed: () => sendMessage(chat['id']),
backgroundColor: Colors.blue,
elevation: 0,
child: const Icon(
Icons.send,
color: Colors.white,
size: 18,
),
),
],
),
),
),
],
),
Thanks

Related

Function is not a constructor

Insurance Model
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'dart:convert';
class Insurance {
final String? id;
final String lifeInsurance;
final String healthInsurance;
final String microPension;
const Insurance({
this.id,
required this.lifeInsurance,
required this.healthInsurance,
required this.microPension,
});
Map<String, dynamic> toMap() {
return <String, dynamic>{
'id': id,
'lifeInsurance': lifeInsurance,
'healthInsurance': healthInsurance,
'microPension': microPension,
};
}
factory Insurance.fromMap(Map<String, dynamic> map) {
return Insurance(
id: map['_id'] != null ? map['id'] as String : null,
lifeInsurance: map['lifeInsurance'] as String,
healthInsurance: map['healthInsurance'] as String,
microPension: map['microPension'] as String,
);
}
String toJson() => json.encode(toMap());
factory Insurance.fromJson(String source) =>
Insurance.fromMap(json.decode(source));
}
Insurance Services
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:provider/provider.dart';
import 'package:verdent/global_variable.dart';
import 'package:verdent/models/insurance.dart';
import 'package:verdent/providers/user_provider.dart';
class InsuranceServices {
void addInsurance({
required BuildContext context,
required String lifeInsurance,
required String healthInsurance,
required String microPension,
}) async {
final userProvider = Provider.of<UserProvider>(context, listen: false);
try {
Insurance insurance = Insurance(
healthInsurance: healthInsurance,
lifeInsurance: lifeInsurance,
microPension: microPension,
);
http.Response res = await http.post(
Uri.parse('$uri/api/add-insurance'),
headers: {
'Content-Type': 'application/json; charset=UTF-8',
'x-auth-token': userProvider.user.token,
},
body: insurance.toJson(),
);
httpErrorHandling(
response: res,
context: context,
onSuccess: () {
showSnackBar(context, 'Your Insurance Detail Added Successfully!');
Navigator.pop(context);
},
);
} catch (e) {
showSnackBar(context, e.toString());
}
}
}
farmer_insurance
import 'package:flutter/material.dart';
import 'package:verdent/Forms/services/insurance_service.dart';
import '../../Dashboard/Home/Widgets/drawer.dart';
class Farmer_Insurance extends StatefulWidget {
const Farmer_Insurance({Key? key}) : super(key: key);
#override
State<Farmer_Insurance> createState() => _Farmer_InsuranceState();
}
class _Farmer_InsuranceState extends State<Farmer_Insurance> {
// static const String routeName = '/insurance';
final _addInsuranceFormKey = GlobalKey<FormState>();
final InsuranceServices insuranceServices = InsuranceServices();
List<String> yno = ['Yes', 'No'];
String selectedvalue8 = 'Yes';
String selectedvalue9 = 'Yes';
String selectedvalue10 = 'Yes';
void addInsurance() {
insuranceServices.addInsurance(
context: context,
healthInsurance: selectedvalue8,
lifeInsurance: selectedvalue9,
microPension: selectedvalue10,
);
}
#override
Widget build(BuildContext context) {
final size = MediaQuery.of(context).size;
return Scaffold(
drawer: NavigationDrawerWidget(),
backgroundColor: const Color.fromRGBO(235, 238, 239, 1.0),
appBar: AppBar(
backgroundColor: Colors.white,
elevation: 0,
leading: Builder(
builder: (context) => InkWell(
splashColor: Colors.black,
onTap: () {
Scaffold.of(context).openDrawer();
},
child: const Icon(
Icons.notes,
color: Colors.green,
size: 30,
))),
title: const Text(
"Insurance Details ",
style: TextStyle(color: Colors.black, fontSize: 25),
),
actions: <Widget>[
Padding(
padding: const EdgeInsets.only(right: 20),
child: InkWell(
splashColor: Colors.black,
onTap: () {},
child: Ink(
decoration: const BoxDecoration(shape: BoxShape.circle),
child: const Icon(
Icons.notifications_outlined,
color: Colors.black,
size: 30,
))),
),
],
),
body: SingleChildScrollView(
child: Form(
key: _addInsuranceFormKey,
child: Column(
children: [
Center(
child: Column(
children: [
const Align(
alignment: Alignment.topLeft,
child: Padding(
padding:
EdgeInsets.only(left: 30, top: 10.0, bottom: 10),
child: Text(
"Insurance Details ",
style: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.bold),
),
),
),
Container(
decoration: const BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(12.0)),
color: Colors.white,
),
height: size.height / 2,
width: size.width / 1.12,
padding: const EdgeInsets.only(
left: 20, right: 20, top: 20, bottom: 30),
child: SingleChildScrollView(
child: Column(children: [
SizedBox(
height: size.height / 70,
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
const Padding(
padding: EdgeInsets.all(18.0),
child: Text(
"Life Insurance",
style: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.bold),
),
),
SizedBox(
width: 150,
height: 70,
child: Padding(
padding: const EdgeInsets.all(6),
child: DropdownButtonFormField<String>(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: const Color.fromARGB(
255, 227, 242, 253),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
border: OutlineInputBorder(
borderSide: const BorderSide(
color: Color.fromARGB(
255, 207, 234, 255),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
filled: true,
fillColor: Colors.amber,
),
hint: const Text("Select Options: "),
value: selectedvalue8,
icon: const Icon(Icons.arrow_drop_down),
iconSize: 24,
isExpanded: true,
style: const TextStyle(
color: Colors.black,
fontSize: 18,
),
items: yno
.map((String item) =>
DropdownMenuItem(
value: item,
child: Text(item,
style: const TextStyle(
fontSize: 18))))
.toList(),
onChanged: (String? item) {
setState(() => selectedvalue8 = item!);
},
),
),
),
],
),
),
SizedBox(
height: size.height / 70,
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
const Padding(
padding: EdgeInsets.all(18.0),
child: Text(
"Health Insurance",
style: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.bold),
),
),
SizedBox(
width: 150,
height: 70,
child: Padding(
padding: const EdgeInsets.all(6),
child: DropdownButtonFormField<String>(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: const Color.fromARGB(
255, 227, 242, 253),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
border: OutlineInputBorder(
borderSide: const BorderSide(
color: Color.fromARGB(
255, 207, 234, 255),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
filled: true,
fillColor: Colors.amber,
),
hint: const Text("Select Options: "),
value: selectedvalue9,
icon: const Icon(Icons.arrow_drop_down),
iconSize: 24,
isExpanded: true,
style: const TextStyle(
color: Colors.black, fontSize: 18),
items: yno
.map((String item) =>
DropdownMenuItem(
value: item,
child: Text(item,
style: const TextStyle(
fontSize: 18))))
.toList(),
onChanged: (String? item) {
setState(() => selectedvalue9 = item!);
},
),
),
),
],
),
),
SizedBox(
height: size.height / 70,
),
SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
const Padding(
padding: EdgeInsets.all(18.0),
child: Text(
"Micro Pension",
style: TextStyle(
color: Colors.black,
fontSize: 17,
fontWeight: FontWeight.bold),
),
),
SizedBox(
width: 150,
height: 70,
child: Padding(
padding: const EdgeInsets.all(6),
child: DropdownButtonFormField<String>(
decoration: InputDecoration(
enabledBorder: OutlineInputBorder(
borderSide: const BorderSide(
color: const Color.fromARGB(
255, 227, 242, 253),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
border: OutlineInputBorder(
borderSide: const BorderSide(
color: Color.fromARGB(
255, 207, 234, 255),
width: 2),
borderRadius:
BorderRadius.circular(20),
),
filled: true,
fillColor: Colors.amber,
),
hint: const Text("Select Options: "),
value: selectedvalue10,
icon: const Icon(Icons.arrow_drop_down),
iconSize: 24,
isExpanded: true,
style: const TextStyle(
color: Colors.black, fontSize: 18),
items: yno
.map((String item) =>
DropdownMenuItem(
value: item,
child: Text(item,
style: const TextStyle(
fontSize: 18))))
.toList(),
onChanged: (String? item) {
setState(() => selectedvalue10 = item!);
},
),
),
),
],
),
),
SizedBox(
height: size.height / 70,
),
MaterialButton(
shape: const RoundedRectangleBorder(
borderRadius:
BorderRadius.all(Radius.circular(12.0))),
color: Colors.green,
highlightColor: Colors.white,
splashColor: Colors.amber,
onPressed: () {
if (_addInsuranceFormKey.currentState!
.validate()) {
addInsurance();
}
},
child: const Padding(
padding: EdgeInsets.symmetric(
vertical: 15.0, horizontal: 50.0),
child: Text(
'SUBMIT',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 15),
)),
),
]),
),
),
],
),
),
],
),
),
),
);
}
}
Insurance.js
const express = require("express");
const insuranceRouter = express.Router();
const auth = require("../middlewares/auth");
const { Insurance } = require("../models/insurance_detail");
//Get all your products
insuranceRouter.post("/api/add-insurance", auth ,async (req, res) => {
try {
const { lifeInsurance, healthInsurance, microPension } = req.body;
let insurance = new Insurance({
lifeInsurance,
healthInsurance,
microPension
});
insurance = await insurance.save();
res.json(insurance);
} catch (e) {
res.status(500).json({ error: e.message });
}
});
module.exports = insuranceRouter;
Showing this error when i try to upload my data on my database "Insurance is not a Constructor".I don't know why this error is showing I tried to add const in my Isurance model but same error is showing again and again please tell me how to solve this issue

Bold text in Flutter bulleted list

I have a page with a bulleted list. I'd like 3 of the list-items to contain some bold text. Not paragraphs that are all bold text, but paragraphs that contain 1 or 2 words in bold. For example,
I would like just this text to be bold.
In this second bulleted paragraph, I want these words to be bold.
In this third bulleted paragraph, I want these words in bold.
I'd really appreciate help. Here's my code:
import 'package:cordova/ui/contact/contact.dart';
import 'package:cordova/ui/home.dart';
import 'package:cordova/ui/information/license_page.dart';
import 'package:cordova/ui/widget/contact_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_windowmanager/flutter_windowmanager.dart';
import 'package:hexcolor/hexcolor.dart';
class NotesScreen extends StatefulWidget {
NotesScreen({Key? key}) : super(key: key);
#override
_NotesScreenState createState() => _NotesScreenState();
}
class _NotesScreenState extends State<NotesScreen> {
disableScreen() async {
await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
}
Text bold(String abc) {
return Text(abc,
style: TextStyle(
fontSize: 35,
fontFamily: 'RedHatDisplay',
fontWeight: FontWeight.bold));
}
#override
void initState() {
// TODO: implement initState
super.initState();
disableScreen();
}
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
decoration: const BoxDecoration(color: Colors.white),
height: 50,
alignment: Alignment.bottomCenter,
child: IconButton(
onPressed: () {
Navigator.pushAndRemoveUntil(
context,
MaterialPageRoute(builder: (context) => const HomeScreen()),
(route) => false);
},
icon: const Icon(
Icons.home,
color: Colors.black,
size: 25,
)),
),
body: SafeArea(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: HexColor('#ffffff'),
child: SingleChildScrollView(
child: Column(children: [
Container(
decoration: BoxDecoration(color: HexColor('#008AD8')),
//height: MediaQuery.of(context).size.height * 0.7,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton.icon(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(
Icons.arrow_back_ios_new,
color: Colors.white,
),
label: const Text(
"Back",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: 'RedHatDisplay',
fontWeight: FontWeight.bold),
)),
IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ContactScreen()));
},
icon: const Icon(
Icons.mail,
color: Colors.white,
size: 25,
)),
],
),
const SizedBox(
height: 30,
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 4.0),
child: Text(
'Information',
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontFamily: 'RedHatDisplay',
fontWeight: FontWeight.w600),
),
),
const SizedBox(
height: 10,
),
const SizedBox(
height: 30,
)
],
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: Column(
children: [
const UnorderedListItemBold(
"I would like just ", "this", "text to be bold."),
const UnorderedListItemBold(
"In this second bulleted paragraph, I want ",
"these words",
"to be bold."),
const UnorderedListItemBold(
"In this third bulleted paragraph, ",
"I want these words",
"in bold."),
UnorderedList(const [
"This paragraph has NO bold text.",
"Neither does this one.",
"And this paragraph is also plain text."
]),
],
))),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: Column(
children: [
UnorderedList(const [
"More text, plain and simple.",
"And more text in another paragraph, plain and simple.",
"Another paragraph of plain text.",
"A final short paragraph in this section."
]),
],
))),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Final paragraph of this page, just plain text.",
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
]),
),
)));
}
}
class UnorderedList extends StatelessWidget {
UnorderedList(this.texts);
final List<String> texts;
#override
Widget build(BuildContext context) {
var widgetList = <Widget>[];
for (var text in texts) {
// Add list item
widgetList.add(UnorderedListItem(text));
// Add space between items
widgetList.add(SizedBox(height: 5.0));
}
return Column(children: widgetList);
}
}
class UnorderedListItem extends StatelessWidget {
const UnorderedListItem(this.text);
final String text;
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("• ",
style: TextStyle(
fontSize: 35,
fontFamily: 'RedHatDisplay',
)),
Expanded(
child: Text(text,
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
)),
),
],
);
}
}
class UnorderedListItemBold extends StatelessWidget {
const UnorderedListItemBold(this.text, this.bold, this.remaining);
final String text;
final String bold;
final String remaining;
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("• ",
style: TextStyle(
fontSize: 35,
fontFamily: 'RedHatDisplay',
)),
Expanded(
child: RichText(
text: const TextSpan(
style: TextStyle(
fontSize: 20, fontFamily: 'RedHatDisplay', color: Colors.black),
children: <TextSpan>[
TextSpan(text: 'I would like just '),
TextSpan(
text: 'this ', style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: 'text to be bold.'),
],
child: <TextSpan>[
TextSpan(text: 'In this second bulleted paragraph, I want '),
TextSpan(
text: 'these words ',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: 'to be bold.'),
],
children: <TextSpan>[
TextSpan(text: 'In this third bulleted paragraph, '),
TextSpan(
text: 'I want these words ',
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(text: 'in bold.'),
],
),
)),
],
);
}
}
Thank you for any help you can give.
What I was trying to achieve can be seen in the attached image - specifically in the 3 sentences "Lorem ipsum hoorah!", where the "ipsum" is in bold.
Here's a solution:
class NotesScreen extends StatefulWidget {
NotesScreen({Key? key}) : super(key: key);
#override
_NotesScreenState createState() => _NotesScreenState();
}
class _NotesScreenState extends State<NotesScreen> {
// disableScreen() async {
// await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
// }
// Text bold(String abc) {
// return Text(abc,
// style: TextStyle(
// fontSize: 35,
// fontFamily: 'RedHatDisplay',
// fontWeight: FontWeight.bold));
// }
#override
void initState() {
// TODO: implement initState
super.initState();
// disableScreen();
}
#override
Widget build(BuildContext context) {
return Scaffold(
bottomNavigationBar: Container(
decoration: const BoxDecoration(color: Colors.white),
height: 50,
alignment: Alignment.bottomCenter,
child: IconButton(
onPressed: () {
// Navigator.pushAndRemoveUntil(
// context,
// MaterialPageRoute(builder: (context) => const HomeScreen()),
// (route) => false);
},
icon: const Icon(
Icons.home,
color: Colors.black,
size: 25,
)),
),
body: SafeArea(
child: Container(
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height,
color: Color(0xFFffffff),
child: SingleChildScrollView(
child: Column(children: [
Container(
decoration: BoxDecoration(color: Color(0xFFE30613)),
//height: MediaQuery.of(context).size.height * 0.7,
child: Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
TextButton.icon(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(
Icons.arrow_back_ios_new,
color: Colors.white,
),
label: const Text(
"Back",
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontFamily: 'RedHatDisplay',
fontWeight: FontWeight.bold),
)),
IconButton(
onPressed: () {
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => ContactScreen()));
},
icon: const Icon(
Icons.mail,
color: Colors.white,
size: 25,
)),
],
),
const SizedBox(
height: 30,
),
const Padding(
padding: EdgeInsets.symmetric(horizontal: 4.0),
child: Text(
'Information',
style: TextStyle(
color: Colors.white,
fontSize: 30,
fontFamily: 'RedHatDisplay',
fontWeight: FontWeight.w600),
),
),
const SizedBox(
height: 10,
),
const SizedBox(
height: 30,
)
],
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Lorem ipsum sweetness and light.",
textAlign: TextAlign.justify,
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
Padding(
padding: EdgeInsets.all(8),
child: Container(
alignment: Alignment.centerLeft,
child: const Text(
'Lorem',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'RedHatDisplay',
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Lorem ipsum.",
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
Padding(
padding: EdgeInsets.all(8),
child: Container(
alignment: Alignment.centerLeft,
child: const Text(
'Lorem',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'RedHatDisplay',
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: Column(
children: [
const UnorderedListItemBold(
text: "Lorem",
bold: "ipsum",
remaining: "summa cum laude."),
const UnorderedListItemBold(
text: "Lorem",
bold:"ipsum2",
remaining: "summa cum laude."),
const UnorderedListItemBold(
text: "Lorem",
bold:"ipsum3",
remaining: "summa cum laude."),
UnorderedList(const [
"Lorem ipsum",
"Lorem ipsum",
"Lorem ipsum"
]),
],
))),
Padding(
padding: EdgeInsets.all(8),
child: Container(
alignment: Alignment.centerLeft,
child: const Text(
'Lorem',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'RedHatDisplay',
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Lorem ipsum",
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: Column(
children: [
UnorderedList(const [
"Lorem ipsum",
"and more lorem ipsum",
"and more lorem ipsum",
"and more lorem ipsum"
]),
],
))),
Padding(
padding: EdgeInsets.all(8),
child: Container(
alignment: Alignment.centerLeft,
child: const Text(
'Lorem',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'RedHatDisplay',
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Lorem ipsum",
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
Padding(
padding: EdgeInsets.all(8),
child: Container(
alignment: Alignment.centerLeft,
child: const Text(
'Lorem',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
fontFamily: 'RedHatDisplay',
),
),
),
),
Padding(
padding: EdgeInsets.all(8),
child: SizedBox(
width: MediaQuery.of(context).size.width * 0.95,
child: const Text(
"Lorem ipsum",
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
),
)),
),
]),
),
)));
}
}
class UnorderedList extends StatelessWidget {
UnorderedList(this.texts);
final List<String> texts;
#override
Widget build(BuildContext context) {
var widgetList = <Widget>[];
for (var text in texts) {
// Add list item
widgetList.add(UnorderedListItem(text));
// Add space between items
widgetList.add(SizedBox(height: 5.0));
}
return Column(children: widgetList);
}
}
class UnorderedListItem extends StatelessWidget {
const UnorderedListItem(this.text);
final String text;
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("• ",
style: TextStyle(
fontSize: 35,
fontFamily: 'RedHatDisplay',
)),
Expanded(
child: Text(text,
style: TextStyle(
fontSize: 20,
fontFamily: 'RedHatDisplay',
)),
),
],
);
}
}
class UnorderedListItemBold extends StatelessWidget {
final String text;
final String bold;
final String remaining;
const UnorderedListItemBold({ Key? key, required this.text, required this.bold, required this.remaining }) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("• ",
style: TextStyle(
fontSize: 35,
fontFamily: 'RedHatDisplay',
)),
Expanded(
child: RichText(
text: TextSpan(
style: TextStyle(
fontSize: 20, fontFamily: 'RedHatDisplay', color: Colors.black),
children: <TextSpan>[
TextSpan(text: text),
TextSpan(
text: bold,
style: TextStyle(fontWeight: FontWeight.bold)),
TextSpan(
text:
remaining),
],
),
)),
],
);
}
}
I hope this will be of use to someone else in the future. :)

Does my style of coding make the Emulator slow?

I am working with Android Studio it runs really smooth. After some time of update. The app was really slow at getting data from the backend (testing backend with PostMan still fast) also execute other tasks. I wonder if our style of coding slow down the app. Have you ever experienced this ? Does the bad coding style has a huge effect on Emulator performance. If yes, could you please suggest a good way of coding that allows the app in Android Studio run faster.
Thank you so much.
Edit: Here is my code
import 'dart:convert';
import 'package:auto_size_text/auto_size_text.dart';
import 'package:flutter/cupertino.dart' hide Image;
import 'package:flutter/material.dart' hide Image;
import 'package:flutter/widgets.dart';
import 'package:goweefrontend/SizeConfig.dart';
import 'package:goweefrontend/View/Detail/DetailJourney.dart';
import 'package:goweefrontend/View/Map/TestMap.dart';
import 'package:goweefrontend/Model/LoggedUser.dart';
import 'package:goweefrontend/View/CustomBottomNavBar/CustomBottomNavBar.dart';
import 'package:goweefrontend/View/UserPage/CustomCalendar.dart';
import 'package:goweefrontend/View/UserPage/JoinRequest.dart';
import 'package:goweefrontend/View/UserPage/UserPageService.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:goweefrontend/View/LoginFunction/LoginComponent/LoginScreen.dart';
import '../../Constants.dart';
import 'package:http/http.dart' as http;
import '../../Model/Journey.dart';
class UserPage extends StatefulWidget {
static String routeName = "/userPage";
#override
_UserPageState createState() => _UserPageState();
}
class _UserPageState extends State<UserPage> {
CalendarFormat format = CalendarFormat.twoWeeks;
DateTime focusedDay = DateTime.now();
DateTime selectedDay = DateTime.now();
bool upcomingIsVisible = false;
bool draftIsVisible = false;
bool passedIsVisible = false;
TextEditingController _lastNameController = TextEditingController();
TextEditingController _firstNameController = TextEditingController();
TextEditingController _ageController = TextEditingController();
TextEditingController _phoneNumberController = TextEditingController();
String _firstName;
String _lastName;
int _age;
String _phoneNumber;
String _imageURL;
List<Journey> journeyList = [];
Future _future;
Future loadUser() async {
String jsonString = await storage.read(key: "jwt");
final jsonResponse = json.decode(jsonString);
loggedUser = new LoggedUser.fromJson(jsonResponse);
getProfile();
}
//http APIs
editProfile(
String lastName, String firstName, String age, String phoneNumber) async {
await UserPageService().editProfile(lastName, firstName, age, phoneNumber);
}
getProfile() async {
var res = await http.get(
Uri.parse("$baseUrl/users/${loggedUser.user.userId}"),
headers: {
'Content_Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ${loggedUser.token}',
'Connection': 'Keep-Alive'
},
);
var data = jsonDecode(res.body);
loggedUser.user.firstName = data["firstName"];
loggedUser.user.lastName = data["lastName"];
loggedUser.user.age = data["age"];
loggedUser.user.phoneNumber = data["phoneNumber"];
loggedUser.user.userAvatar.imageLink = data["userAvatar"]["imageLink"];
setState(() {
_firstName = data["firstName"];
_lastName = data["lastName"];
_age = data["age"];
_phoneNumber = data["phoneNumber"];
_imageURL = data["userAvatar"]["imageLink"];
});
}
getJourneyByUserId() async {
setState(() {
journeyList = [];
});
var res = await http.get(
Uri.parse("$baseUrl/journeys/userid=${loggedUser.user.userId}"),
headers: {
'Content_Type': 'application/json; charset=UTF-8',
'Authorization': 'Bearer ${loggedUser.token}',
'Connection': 'Keep-Alive'
},
);
var data = jsonDecode(res.body);
List idList = [];
for (var i in data) {
idList.add(i["journeyId"]);
}
for (var i in idList) {
var res = await http.get(
Uri.parse("$baseUrl/journeys/$i"),
);
var data = jsonDecode(res.body);
Journey userJourney = new Journey.fromJson(data);
setState(() {
journeyList.add(userJourney);
});
}
return journeyList;
}
logOut() async {
await UserPageService().logOut();
setState(() {
rangeList.clear();
journeyList.clear();
});
}
Future functionForBuilder() async {
return await getJourneyByUserId();
}
//function to print Journey Card
Widget printCard(Journey journey, IconData flexIcon, page, visible) {
return Visibility(
visible: visible,
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(0, 5, 0, 5),
child: Container(
height: getHeight(0.1),
padding: EdgeInsets.fromLTRB(5, 10, 0, 10),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
offset: Offset(0, 8),
blurRadius: 5,
color: Colors.grey[300]),
]),
child: Row(
children: [
Padding(
//show journey image
padding: EdgeInsets.all(3),
child: CircleAvatar(
backgroundImage: NetworkImage(journey
.images[0].imageLink !=
null
? journey.images[0].imageLink
: "assets/images/Travel-WordPress-Themes.jpg.webp"),
radius: 20,
),
),
SizedBox(width: getWidth(0.02)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Container(
width: getWidth(0.32),
child: AutoSizeText(
"${journey.departure} - ${journey.destination}",
style: TextStyle(color: Colors.black),
overflow: TextOverflow.ellipsis,
),
),
],
),
],
),
SizedBox(width: getWidth(0.05)),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
children: [
Container(
width: getWidth(0.27),
child: AutoSizeText(
"${journey.startDate.day}/${journey.startDate.month} - "
"${journey.endDate.day}/${journey.endDate.month}/${journey.endDate.year}",
style:
TextStyle(color: Colors.grey, fontSize: 13),
maxLines: 1,
),
),
],
),
],
),
SizedBox(width: getWidth(0.05)),
Column(
children: [
Container(
width: getWidth(0.1),
child: TextButton(
onPressed: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => page));
},
child: FittedBox(
child: Icon(
flexIcon,
color: Colors.black,
),
),
),
)
],
)
],
),
),
)
],
),
);
}
//function to show dialog User Profile Card
showDialogFunc1(context) {
return showDialog(
context: context,
builder: (context) {
return Stack(alignment: Alignment.bottomCenter, children: [
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 270),
height: getHeight(0.4),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(30.0)),
child: Padding(
padding: EdgeInsets.fromLTRB(20, 30, 0, 10),
child: Row(children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
printInfo("NAME", "$_lastName $_firstName"),
printInfo("AGE", "$_age"),
printInfo("PHONE", "$_phoneNumber"),
Center(
child: Container(
height: getHeight(0.05),
decoration: kBoxDecorationView,
child: TextButton(
onPressed: () {
showDialogFunc2(context);
},
child: Text(
"Edit Profile",
style: TextStyle(color: Colors.white),
),
),
),
)
]),
flex: 5,
),
Expanded(
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(10, 0, 40, 0),
child: Column(
children: [
CircleAvatar(
radius: 35,
backgroundImage: NetworkImage(
loggedUser.user.userAvatar.imageLink),
),
SizedBox(height: 15),
FittedBox(
child: Text(
"${loggedUser.user.email}",
style: TextStyle(
fontSize: 15,
color: Colors.black,
fontWeight: FontWeight.bold,
letterSpacing: 1.0,
decoration: TextDecoration.none),
),
)
],
),
)
],
),
flex: 5,
)
]))),
]);
});
}
//print User Information
printInfo(info1, info2) {
return FittedBox(
child: Row(
children: [
Text(
info1 + " : " + info2,
style: TextStyle(
decoration: TextDecoration.none,
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
],
),
);
}
Widget loopPrintUpComeTrip() {
int counter = 0;
List<Widget> list = [];
for (var i in journeyList) {
if (i.status == "Upcoming") {
counter += 1;
if (counter == 1) {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), true));
} else {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), upcomingIsVisible));
}
}
}
if (counter == 0) {
list.add(Text("No Upcoming Trip to show"));
}
return Column(children: list);
}
Widget loopPrintInProgressTrip() {
int counter = 0;
List<Widget> list = [];
for (var i in journeyList) {
if (i.status == "In progress") {
counter += 1;
if (counter == 1) {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), true));
} else {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), draftIsVisible));
}
}
}
if (counter == 0) {
list.add(Text("No In Progress Trip to show"));
}
return Column(children: list);
}
Widget loopPrintFinishedTrip() {
int counter = 0;
List<Widget> list = [];
for (var i in journeyList) {
if (i.status == "Finished") {
counter += 1;
if (counter == 1) {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), true));
} else {
list.add(printCard(i, Icons.arrow_forward_ios,
DetailsJourneyScreen(journey: i), passedIsVisible));
}
}
}
if (counter == 0) {
list.add(Text(
"No Finished Trip to show",
style: TextStyle(color: Colors.grey),
));
}
return Column(children: list);
}
//print edit form
printFormField(info, controller) {
return Container(
height: getHeight(0.06),
child: TextButton(
onPressed: () {},
child: TextField(
controller: controller,
style: TextStyle(fontSize: 15),
decoration: InputDecoration(
hintText: "New $info",
contentPadding: EdgeInsets.fromLTRB(15, 5, 0, 5),
isDense: true,
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(15.0),
borderSide: BorderSide(color: kTextColor),
gapPadding: 0,
),
),
),
),
);
}
//show dialog edit profile
showDialogFunc2(context) {
return showDialog(
context: context,
builder: (context) {
return Stack(alignment: Alignment.bottomCenter, children: [
Container(
margin: EdgeInsets.fromLTRB(10, 0, 10, 270),
height: getHeight(0.4),
decoration: BoxDecoration(
color: Colors.grey[200],
borderRadius: BorderRadius.circular(30.0)),
child: Padding(
padding: EdgeInsets.fromLTRB(20, 30, 0, 10),
child: Row(children: [
Expanded(
child: Column(
//crossAxisAlignment: CrossAxisAlignment.start,
//mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
printFormField("Last Name", _lastNameController),
printFormField(
"First Name", _firstNameController),
printFormField("Age", _ageController),
printFormField(
"Phone Number", _phoneNumberController),
SizedBox(height: getHeight(0.01)),
Center(
child: Container(
height: getHeight(0.05),
decoration: kBoxDecorationView,
child: TextButton(
onPressed: () async {
await editProfile(
_lastNameController.text,
_firstNameController.text,
_ageController.text,
_phoneNumberController.text);
_lastNameController.clear();
_firstNameController.clear();
_ageController.clear();
_phoneNumberController.clear();
Navigator.pop(context);
Navigator.pop(context);
setState(() {
getProfile();
});
},
child: Text(
"Save",
style: TextStyle(color: Colors.white),
),
),
))
]),
flex: 6,
),
Expanded(
child: Column(
children: [
Padding(
padding: EdgeInsets.fromLTRB(10, 0, 40, 0),
child: Column(
children: [
CircleAvatar(
radius: 35,
backgroundImage: NetworkImage(
loggedUser.user.userAvatar.imageLink),
),
SizedBox(height: 10),
Text(
"Upload Image",
style: TextStyle(
fontSize: 10,
color: Colors.black,
fontWeight: FontWeight.bold,
letterSpacing: 1.0,
decoration: TextDecoration.none),
)
],
),
)
],
),
flex: 4,
)
]))),
]);
});
}
#override
void initState() {
super.initState();
loadUser();
_future = getJourneyByUserId();
}
//screen
#override
Widget build(BuildContext context) {
return Scaffold(
resizeToAvoidBottomInset: false,
backgroundColor: kColorPalette4,
appBar: AppBar(
title: Text(
"User Profile",
style: TextStyle(color: kColorPalette4),
),
centerTitle: true,
backgroundColor: kBackgroundColor,
elevation: 0.0,
titleSpacing: 1.0,
),
body: Container(
child: FutureBuilder(
future: _future,
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (!snapshot.hasData) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircularProgressIndicator(),
Text("Loading...")
]),
);
} else {
return SingleChildScrollView(
child: Padding(
padding: EdgeInsets.fromLTRB(10, 5, 10, 0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Center(
//user avatar
child: TextButton(
onPressed: () {
showDialogFunc1(context);
getProfile();
},
child: CircleAvatar(
backgroundImage: NetworkImage(_imageURL != null
? _imageURL
: "https://www.pngitem.com/pimgs/m/256-2560208_person-icon-black-png-transparent-png.png"),
radius: 50,
),
)),
SizedBox(height: 5),
CustomCalendar(),
Text("Trip Requests",
style: TextStyle(
color: kBackgroundColor,
letterSpacing: 1.0,
fontWeight: FontWeight.bold,
fontSize: 15)),
SizedBox(height: 10),
JoinRequest(),
SizedBox(height: 10),
Row(
children: [
Container(
width: getWidth(0.35),
child: AutoSizeText(
"Upcoming Trips",
style: TextStyle(
color: kColorUpComing,
letterSpacing: 1.0,
fontWeight: FontWeight.bold,
fontSize: 15),
maxLines: 1,
),
),
Padding(
padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
child: Container(
height: getHeight(0.05),
decoration: kBoxDecorationView,
child: TextButton(
child: Text(
"View All",
style: TextStyle(color: kColorPalette4),
),
onPressed: () {
setState(() {
upcomingIsVisible = !upcomingIsVisible;
});
},
),
),
)
],
),
//upcoming trip card
SizedBox(height: 5),
loopPrintUpComeTrip(),
//Calendar Field
SizedBox(height: 5),
Row(
children: [
Container(
width: getWidth(0.35),
child: AutoSizeText(
"Finished Trips",
style: TextStyle(
color: kColorFinished,
letterSpacing: 1.0,
fontWeight: FontWeight.bold,
fontSize: 15),
maxLines: 1,
),
),
Padding(
padding: EdgeInsets.fromLTRB(15, 0, 0, 0),
child: Container(
height: getHeight(0.05),
decoration: kBoxDecorationView,
child: TextButton(
child: Text(
"View All",
style: TextStyle(color: kColorPalette4),
),
onPressed: () {
setState(() {
passedIsVisible = !passedIsVisible;
});
},
),
),
)
],
),
SizedBox(height: 5),
loopPrintFinishedTrip(),
SizedBox(height: 5),
Row(
children: [
Container(
width: getWidth(0.35),
child: AutoSizeText(
"In Progress Trips",
style: TextStyle(
color: kColorInProgress,
letterSpacing: 1.0,
fontWeight: FontWeight.bold,
fontSize: 15),
maxLines: 1,
),
),
Padding(
padding: EdgeInsets.fromLTRB(10, 0, 0, 0),
child: Container(
height: getHeight(0.05),
decoration: kBoxDecorationView,
child: TextButton(
child: Text(
"View All",
style: TextStyle(color: kColorPalette4),
),
onPressed: () => setState(
() => draftIsVisible = !draftIsVisible),
),
),
)
],
),
SizedBox(height: 5),
loopPrintInProgressTrip(),
SizedBox(height: 10),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
height: getHeight(0.055),
decoration: kBoxDecorationView,
alignment: Alignment.center,
child: TextButton(
onPressed: () async {
await logOut();
Navigator.pushReplacement(
context,
MaterialPageRoute(
builder: (context) =>
LoginScreen()));
},
child: FittedBox(
child: Row(
children: [
Icon(
Icons.logout,
color: kColorPalette4,
size: 20,
),
AutoSizeText(
"Log Out",
style: TextStyle(
color: kColorPalette4,
),
),
],
),
)),
)
],
),
SizedBox(
height: getHeight(0.01),
)
],
),
),
);
}
},
),
));
}
}
Usually when i'm dealing with retrieving and sending data to the database I create a new thread to do the job of transferring data, if you are transferring with the main thread it's not recommended to do that the program could crash.
There is a way to see the performance of the app:
Profile your app performance
Another reason to be lagging could be:
Style of coding won't slow your program down. However, an inefficient
algorithm will. – Learning Mathematics 8 hours ago
another reason could be the version of the Android you are using to operate in the app some View object has specific limitations depending of the version of the Android.

Displaying contents of ListView in Flutter and displaying image uploaded from gallery

I'm fairly new to software dev using Flutter/Dart and I'm currently working on a widget that uses listview to display the information entered by a user. This information is currently stored in a List and for some reason, each time I add new info to the list, only the info at the first index is printed. I'm not actually getting any error message so it's pretty hard to trace it. I've tried debugging it using print statements but it's really not helping me.
The second problem is that when the user chooses a picture from their gallery, it isn't immediately displayed.
How can I fix these things?
import 'dart:io';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:mind_matters/shared/loading.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:image_picker/image_picker.dart';
import 'package:path/path.dart';
class EmergencyContacts extends StatefulWidget {
#override
_EmergencyContactsState createState() => _EmergencyContactsState();
}
class _EmergencyContactsState extends State<EmergencyContacts> {
bool loading = false;
static bool buttonPressed = true;
static String emergencyName = "";
static String emailAddress = "";
static String phoneNumber = "";
static File _contactPic;
static int index = -1;
Future getPic() async {
var image = await ImagePicker.pickImage(source: ImageSource.gallery);
setState(() {
_contactPic = image;
print('Image path is: $_contactPic');
});
}
Future uploadPic(BuildContext context) async {
String fileName = basename(_contactPic.path);
StorageReference firebaseStorageRef =
FirebaseStorage.instance.ref().child(fileName);
StorageUploadTask uploadTask = firebaseStorageRef.putFile(_contactPic);
StorageTaskSnapshot takeSnapshot = await uploadTask.onComplete;
}
List<EmergencyContact> emergencyContacts = [];
void createInstanceOfContact() {
if (emergencyName.isNotEmpty) {
emergencyContacts.add(EmergencyContact(
contactPic: _contactPic,
emergencyName: emergencyName,
phoneNumber: phoneNumber,
emailAddress: emailAddress));
} else if (emergencyName.isEmpty) {
emergencyContacts.add(EmergencyContact(
contactPic: null,
emergencyName: "null",
phoneNumber: "null",
emailAddress: "null"));
}
}
void incrementIndex() {
if (buttonPressed){
index++;
}
}
#override
Widget build(BuildContext context) {
return loading
? Loading()
: Scaffold(
appBar: AppBar(
backgroundColor: const Color(0xffff696A),
elevation: 0,
leading: new IconButton(
icon: new Icon(Icons.arrow_back, color: Colors.black45),
onPressed: () => Navigator.of(context).pop(),
),
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
Stack(
alignment: Alignment.center,
children: <Widget>[
Text(" Add emergency contacts",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: Colors.black54,
fontSize: 20,
fontWeight: FontWeight.bold,
)),
],
),
],
),
automaticallyImplyLeading: false,
centerTitle: true,
),
body: Container(
decoration: new BoxDecoration(
gradient: new LinearGradient(
colors: [const Color(0xffff696A), const Color(0xffca436b)],
begin: FractionalOffset.topLeft,
end: FractionalOffset.bottomRight,
stops: [0.0, 1.0],
tileMode: TileMode.clamp),
),
child: SingleChildScrollView(
child: Container(
height: 100000,
child: Padding(
padding: EdgeInsets.fromLTRB(20, 0, 20, 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
SizedBox(height: 20),
Container(
child: Padding(
padding: EdgeInsets.all(10),
child: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"For your safety, please enter at least 1 phone number "
"and email. "
"It will be used to contact someone if we or "
"someone you're talking to feel you need "
"immediate help.",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 16,
)),
SizedBox(height: 20),
new ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemBuilder: (context, index) =>
new CardRow(
emergencyContacts[index]),
itemCount: emergencyContacts.length,
),
Text("the index here is $index"),
SizedBox(height: 10),
Align(
alignment: Alignment.bottomCenter,
child: Container(
width: 194,
child: FloatingActionButton(
onPressed: () {
setState(() {
_contactPic = null;
emergencyName = "";
emailAddress = "";
phoneNumber = "";
});
showAlertDialog(context);
},
child: Icon(
Icons.add,
),
foregroundColor: Colors.white,
backgroundColor:
const Color(0xffff696A),
mini: true,
elevation: 10,
),
),
),
SizedBox(height: 10),
Container(
width: 106,
child: RaisedButton(
shape: new RoundedRectangleBorder(
borderRadius:
new BorderRadius.circular(
18.0),
side: BorderSide(
color: const Color(0xffff696A),
)),
elevation: 10,
//:todo don't forget on pressed action
onPressed: () async {
},
color: const Color(0xffff696A),
textColor: Colors.white,
child: Row(children: <Widget>[
Text("next step".toUpperCase(),
textAlign: TextAlign.center,
style: TextStyle(fontSize: 14)),
]),
),
),
],
),
),
),
),
])),
),
),
));
}
showAlertDialog(BuildContext context) {
// show the dialog
showDialog(
context: context,
builder: (BuildContext context) {
return Dialog(
child: Container(
height: 422,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Add Contact",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: const Color(0xffff696A),
fontSize: 20,
fontWeight: FontWeight.bold,
)),
SizedBox(height: 10),
CircleAvatar(
radius: 50,
backgroundColor: const Color(0xffff696A),
child: ClipOval(
child: SizedBox(
width: 92,
height: 92,
child: (_contactPic != null)
? Image.file(_contactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
),
),
Padding(
padding: EdgeInsets.only(left: 26),
child: IconButton(
icon: Icon(
Icons.photo_camera,
size: 30,
),
onPressed: () {
getPic();
},
),
),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emergencyName = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person,
),
hintText: "Enter their name/nickname",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emailAddress = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.email,
),
hintText: "Enter their email address",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => phoneNumber = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone,
),
hintText: "Enter their phone number",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
Align(
alignment: Alignment.center,
child: Row(
children: <Widget>[
FlatButton(
child: Text("Cancel",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () {
Navigator.of(context).pop();
},
),
SizedBox(width: 80),
FlatButton(
child: Text("Add Contact",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () async {
setState(() {
buttonPressed = true;
});
if (buttonPressed){
createInstanceOfContact();
incrementIndex();
}
print("the index is $index");
print(emergencyContacts[index].contactPic.toString());
print(emergencyContacts[index].emergencyName);
print(emergencyContacts[index].phoneNumber);
print(emergencyContacts[index].emailAddress);
Navigator.of(context).pop();
print(_contactPic.toString());
print(emergencyName);
print(emailAddress);
print(phoneNumber);
},
),
],
),
)
],
),
),
),
),
);
});
}
}
class CardRow extends StatelessWidget {
static EmergencyContact emergencyContact;
static File emergencyContactPic = _EmergencyContactsState._contactPic;
static String emergencyName = _EmergencyContactsState.emergencyName;
static String emergencyNumber = _EmergencyContactsState.phoneNumber;
static String emergencyEmail = _EmergencyContactsState.emailAddress;
CardRow(emergencyContact);
#override
Widget build(BuildContext context) {
return new Container(
height: 130,
margin: const EdgeInsets.symmetric(vertical: 16, horizontal: 24),
child: new Stack(
children: <Widget>[
contactCard,
contactCardContent,
contactThumbnail,
deleteContact,
],
));
}
final contactThumbnail = new Container(
//margin: new EdgeInsets.only(top: 3, left: 270),
margin: new EdgeInsets.only(bottom: 5),
alignment: FractionalOffset.centerLeft,
child: CircleAvatar(
radius: 46,
backgroundColor: new Color(0xFF733b67),
child: ClipOval(
child: SizedBox(
width: 82,
height: 82,
child: (emergencyContactPic != null)
? Image.file(emergencyContactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
)),
);
final contactCard = new Container(
height: 124,
margin: new EdgeInsets.only(left: 46),
decoration: new BoxDecoration(
color: new Color(0xFF733b67),
shape: BoxShape.rectangle,
borderRadius: new BorderRadius.circular(8),
boxShadow: <BoxShadow>[
new BoxShadow(
color: Colors.black12,
blurRadius: 10,
offset: new Offset(0, 10),
)
]),
);
final deleteContact = new Container(
margin: new EdgeInsets.only(top: 3, left: 270),
child: IconButton(
icon: new Icon (Icons.delete_sweep,
size: 30,
),
color: const Color(0xffff696A),
onPressed: (){
//todo delete card
},
),
);
final contactCardContent = new Container(
margin: new EdgeInsets.fromLTRB(90, 16, 16, 16),
constraints: new BoxConstraints.expand(),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(height: 4.0),
new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.person,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyName,
style: TextStyle(
color: Colors.white,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new SizedBox(height: 10.0),
Row(children: <Widget>[
Icon(
Icons.phone,
size: 20,
),
new SizedBox(width: 10.0),
Text(emergencyNumber,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new Container(
margin: new EdgeInsets.symmetric(vertical: 8.0),
height: 2.0,
width: 18.0,
color: const Color(0xffff696A)),
new Row(
children: <Widget>[
Icon(
Icons.mail,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyEmail,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
],
),
],
),
);
}
class EmergencyContact {
File contactPic;
String emergencyName;
String phoneNumber;
String emailAddress;
EmergencyContact(
{this.contactPic,
this.emergencyName,
this.phoneNumber,
this.emailAddress});
}
The reason the listView.builder is not functioning correctly is that you have declared the variables in the CardRow Widget and are using those static values instead of the arguments that you have passed. You are passing the arguments, you just aren't using them.
The only change I did here:
final EmergencyContact emergencyContact;
//static File emergencyContactPic = _EmergencyContactsState._contactPic;
// static String emergencyName = _EmergencyContactsState.emergencyName;
// static String emergencyNumber = _EmergencyContactsState.phoneNumber;
// static String emergencyEmail = _EmergencyContactsState.emailAddress;
CardRow(this.emergencyContact);
Here is where I extracted your widgets as methods(just the ones passed in the argument), the reason I did this was that this can't be accessed in the initializers that you had done. I would recommend you to start using final more, and extracting your Widgets into their own Stateful/Stateless Widgets, or as methods, as I have done below. It's good practice.
child: new Stack(
children: <Widget>[
contactCard,
contactCardContent(),
contactThumbnail(),
deleteContact,
],
));
Container contactThumbnail() {
return Container(
//margin: new EdgeInsets.only(top: 3, left: 270),
margin: new EdgeInsets.only(bottom: 5),
alignment: FractionalOffset.centerLeft,
child: CircleAvatar(
radius: 46,
backgroundColor: new Color(0xFF733b67),
child: ClipOval(
child: SizedBox(
width: 82,
height: 82,
child: (emergencyContact.contactPic != null)
? Image.file(emergencyContact.contactPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
)),
);
}
Container contactCardContent() {
return new Container(
margin: new EdgeInsets.fromLTRB(90, 16, 16, 16),
constraints: new BoxConstraints.expand(),
child: new Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
new Container(height: 4.0),
new Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Icon(
Icons.person,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyContact.emergencyName,
style: TextStyle(
color: Colors.white,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new SizedBox(height: 10.0),
Row(children: <Widget>[
Icon(
Icons.phone,
size: 20,
),
new SizedBox(width: 10.0),
Text(emergencyContact.phoneNumber,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
]),
new Container(
margin: new EdgeInsets.symmetric(vertical: 8.0),
height: 2.0,
width: 18.0,
color: const Color(0xffff696A)),
new Row(
children: <Widget>[
Icon(
Icons.mail,
size: 20,
),
new SizedBox(width: 10.0),
new Text(emergencyContact.emailAddress,
style: TextStyle(
color: Colors.grey,
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
],
),
],
),
);
}
Second Problem
For your second problem, the reason you don't see the image being displayed immediately is that the showDialog is not being rebuilt. To explicate more on this, the setState that is being used in the showAlertDialog function belongs to the parent Widget, so, that means that it is only going to be rebuilding what is outside of that particular function. The showAlertDialog function only gets rebuilt when you first click on it. In order to fix this, we will either have to put the showDialog in a Stateful widget or use a StatefulBuilder (https://api.flutter.dev/flutter/widgets/StatefulBuilder-class.html)
The StatefulBuilder comes with its own setState construction, meaning it can now get rebuilt when using setState, resulting in displaying the image immediately. BUT now we have another issue, I see you have the boolean buttonPressed in there, that whole setState(boolean) belongs to the parent widget, not the StatefulBuilder setState, so now this means that the list will not be displayed after "add contacts" is pressed until it gets rebuilt. We need the setState from the parent, not from the StatefulBuilder. There are many ways to fix this, but I personally think using a callback is one of the best. First, you will need to create a function, like rebuildWidget and simply change the state right there. Secondly, pass that function down to showAlertDialog as an argument. Thirdly, go ahead and create a final Function and then assign it. Last, simply call it on the onPressed.
void rebuildWidget() {
setState(() {
buttonPressed = true;
});
}
showAlertDialog(
context,
rebuildWidget
);
showAlertDialog(BuildContext context, myState) {
final Function() updateParent = myState;
showDialog(
context: context,
builder: (context) {
String contentText = "Content of Dialog";
File myPic;
return StatefulBuilder(
builder: (context, setState) {
return Dialog(
// title: Text("Title of Dialog"),
child: Container(
height: 422,
child: SingleChildScrollView(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
//mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Add Contact",
textAlign: TextAlign.center,
style: TextStyle(
fontFamily: 'Poppins',
color: const Color(0xffff696A),
fontSize: 20,
fontWeight: FontWeight.bold,
)),
SizedBox(height: 10),
CircleAvatar(
radius: 50,
backgroundColor: const Color(0xffff696A),
child: ClipOval(
child: SizedBox(
width: 92,
height: 92,
child: myPic != null
? Image.file(myPic, fit: BoxFit.fill)
: Image.network(
'IMAGE',
fit: BoxFit.fill,
)),
),
),
Padding(
padding: EdgeInsets.only(left: 26),
child: IconButton(
icon: Icon(
Icons.photo_camera,
size: 30,
),
onPressed: () async {
await getPic();
setState(() { // To rebuild this method
myPic = _contactPic;
});
},
),
),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emergencyName = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.person,
),
hintText: "Enter their name/nickname",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => emailAddress = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.email,
),
hintText: "Enter their email address",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
SizedBox(height: 10),
Container(
width: 320,
child: Form(
//key: _formKey2,
child: TextFormField(
obscureText: false,
onChanged: (val) {
setState(() => phoneNumber = val);
},
decoration: InputDecoration(
prefixIcon: Icon(
Icons.phone,
),
hintText: "Enter their phone number",
hintStyle: TextStyle(
fontFamily: "Poppins",
color: Colors.black,
height: 1.2,
fontSize: 14,
)),
),
),
),
Align(
alignment: Alignment.center,
child: Row(
children: <Widget>[
FlatButton(
child: Text("Cancel",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () {
Navigator.of(context).pop();
},
),
SizedBox(width: 80),
FlatButton(
child: Text("Add Contact",
style: TextStyle(
color: const Color(0xffff696A),
fontFamily: "Poppins",
height: 1.2,
fontSize: 14,
)),
onPressed: () async {
updateParent();
if (buttonPressed) {
createInstanceOfContact();
incrementIndex();
}
print("the index is $index");
print(emergencyContacts[index]
.contactPic
.toString());
print(emergencyContacts[index].emergencyName);
print(emergencyContacts[index].phoneNumber);
print(emergencyContacts[index].emailAddress);
Navigator.of(context).pop();
print(_contactPic.toString());
print(emergencyName);
print(emailAddress);
print(phoneNumber);
},
),
],
),
)
],
),
),
),
),
);
},
);
},
);
}
}

How can i display different widgets in a list : Flutter

I'm trying to recreate this ui template in which a I have list under that list is a list of items and after all the items is a container displaying a summary. I'm trying to achieve this by wrapping the listview.builder and a container inside a list view however it doesn't show anything. This is the code.
Container(
height: 550.0,
child: ListView(
children: <Widget>[
Expanded(
child: ListView.builder(
physics: ClampingScrollPhysics(),
scrollDirection: Axis.vertical,
itemCount: itemList.length,
itemBuilder: (BuildContext context, int index) {
return SingleItem(
itemImage: itemList[index]['image'],
itemPrice: itemList[index]['price'],
itemName: itemList[index]['name'],
itemCode: itemList[index]['code'],
);
},
),
),
Expanded(
child: Container(
color: Colors.brown,
),
)
],
)
),
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 Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
children: [
ListView.builder(
shrinkWrap: true,
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return Container(
padding: EdgeInsets.all(20.0),
margin: EdgeInsets.all(10.0),
decoration: BoxDecoration(
border: Border.all(width: 1, color: Colors.grey)),
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
Container(
width: 50,
height: 50,
color: Colors.blueAccent,
),
SizedBox(
width: 30,
),
Column(
children: [Text("Title"), Text('Tag'), Text('Price')],
)
],
),
);
},
),
Container(
width: double.infinity,
child: Card(
color: Colors.redAccent,
elevation: 1.0,
child: (Column(children: [
Text(
'Summary',
style: TextStyle(fontSize: 22, fontWeight: FontWeight.bold),
),
SizedBox(
height: 10.0,
),
Text("""
This is the summary of your products
""")
])),
),
)
],
)),
);
}
}
Try this out:
https://codepen.io/theredcap/pen/qBbjLWp
You can look at this to give you an idea
return Column(
children: [
Expanded(
child: ListView.builder(
itemCount: 6,
itemBuilder: (context, index) {
return Container(
margin: EdgeInsets.all(16),
height: 170,
decoration: BoxDecoration(
color: Colors.grey[300],
borderRadius: BorderRadius.circular(20),
boxShadow: [
BoxShadow(
offset: Offset(0, 15),
blurRadius: 27,
color: Colors.black12)
]),
child: Row(
children: [
Image.asset(
"assets/images/Item_1.png", // your image
fit: BoxFit.cover,
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(5.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Align(
alignment: Alignment.topRight,
child: GestureDetector(
onTap: () {},
child: Image.asset(
"assets/images/Item_1.png", // your icon/image
width: 50,
height: 30,
)),
),
Text(
"Circle Miracle Plate Studs Earrings",
style: TextStyle(
fontSize: 13, fontWeight: FontWeight.bold),
),
Text(
"SKU: 23fe42f44",
style: TextStyle(fontSize: 13),
),
SizedBox(height: 5),
Text(
"\$ 7,500",
style: TextStyle(fontSize: 13),
),
Row(children: [
Text(
"Qty: ",
style: TextStyle(fontWeight: FontWeight.bold),
),
DropdownButton<String>(
value: dropdownValue,
icon: Icon(Icons.arrow_downward),
iconSize: 20,
style: TextStyle(color: Colors.deepPurple),
onChanged: (String newValue) {
setState(() {
dropdownValue = newValue;
});
},
items: <String>['One', 'Two', 'Free', 'Four']
.map<DropdownMenuItem<String>>(
(String value) {
return DropdownMenuItem<String>(
value: value,
child: Text(value),
);
}).toList(),
)
]),
Text("Ship By: 30 June2020")
],
),
),
),
],
),
);
}),
),
Container(
height: 100,
decoration: BoxDecoration(color: Colors.white),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text("Summary",
style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
SizedBox(
height: 12,
),
Table(children: [
TableRow(children: [
Text("Subtotal"),
Text("102000"),
]),
TableRow(children: [
Text("Shipping Charge"),
Text("Free"),
]),
TableRow(children: [
Text("Shipping Insurance"),
Text("Free"),
]),
TableRow(children: [
Text("Grand Total"),
Text("102000"),
]),
]),
],
),
)
],
)
;

Resources