I cant see my data Firebase DB on Flutter - flutter

I can see my data on Flutter Databese, but it disappears on the loading screen. It appears and disappears at first, what is the reason, how can I solve this problem ?
When I press the product group, the data appears at first, but then it disappears.
import 'package:flutter/material.dart';
import 'package:warehouse_management/models/product.dart';
import 'package:warehouse_management/screens/new_product_page.dart';
import 'package:warehouse_management/screens/search_product_in_group.dart';
import 'package:warehouse_management/utils/color_palette.dart';
import 'package:warehouse_management/widgets/product_card.dart';
class ProductGroupPage extends StatelessWidget {
final String? name;
ProductGroupPage({Key? key, this.name}) : super(key: key);
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
#override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: Padding(
padding: const EdgeInsets.only(
bottom: 10,
right: 10,
),
child: FloatingActionButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) {
return NewProductPage(
group: name,
);
},
),
);
},
splashColor: ColorPalette.bondyBlue,
backgroundColor: ColorPalette.pacificBlue,
child: const Icon(
Icons.add,
color: ColorPalette.white,
),
),
),
body: Container(
color: ColorPalette.pacificBlue,
child: SafeArea(
child: Container(
color: ColorPalette.aquaHaze,
height: double.infinity,
width: double.infinity,
child: Column(
children: [
Container(
padding: const EdgeInsets.only(
top: 10,
left: 10,
right: 15,
),
width: double.infinity,
height: 90,
decoration: const BoxDecoration(
color: ColorPalette.pacificBlue,
borderRadius: BorderRadius.only(
bottomLeft: Radius.circular(16),
bottomRight: Radius.circular(16),
),
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
children: [
IconButton(
icon: const Icon(
Icons.chevron_left_rounded,
size: 35,
),
onPressed: () {
Navigator.of(context).pop();
},
),
Text(
name!.length > 14
? '${name!.substring(0, 12)}..'
: name!,
style: const TextStyle(
fontFamily: "Nunito",
fontSize: 28,
color: ColorPalette.timberGreen,
),
),
],
),
Row(
children: [
IconButton(
splashColor: ColorPalette.timberGreen,
icon: const Icon(
Icons.search,
color: ColorPalette.timberGreen,
),
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) =>
SearchProductInGroupPage(
name: name,
),
),
);
},
),
IconButton(
icon: const Icon(
Icons.delete,
color: ColorPalette.timberGreen,
),
onPressed: () {
//TODO
},
),
],
)
],
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: SizedBox(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: const [
SizedBox(
height: 20,
),
],
),
const Text(
"Ürünler",
style: TextStyle(
color: ColorPalette.timberGreen,
fontSize: 20,
fontFamily: "Nunito",
),
),
const SizedBox(height: 20),
Expanded(
child: StreamBuilder(
stream: _firestore
.collection("products")
.where("group", isEqualTo: name)
.orderBy('name')
.snapshots(),
builder: (
BuildContext context,
AsyncSnapshot<
QuerySnapshot<Map<String, dynamic>>>
snapshot,
) {
if (!snapshot.hasData) {
return const Center(
child: SizedBox(
height: 40,
width: 40,
child: CircularProgressIndicator(
color: Colors.black,
),
),
);
}
return ListView.builder(
itemCount: snapshot.data!.docs.length,
itemBuilder:
(BuildContext context, int index) {
return ProductCard(
product: Product.fromMap(
snapshot.data!.docs[index].data(),
),
docID: snapshot.data!.docs[index].id,
);
},
);
},
),
),
],
),
),
),
),
],
),
),
),
),
);
}
}
You can also find the fields I created for Firebase in the appendix.
When I click on the first field on the previous page, my data appears and then disappears.
Then, after 1 second, my data disappears, it appears on firebase, but not on the screen.

<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="#drawable/launch_background"
/>
The solution is to remove the above code.

Related

A RenderViewport expected a child of type RenderSliver but received a child of type RenderPositionedBox

encountered this error after changing some labels, I don't even remember changing the widgets:
I know that this is a sliver and box issue but I tried changing and removing widgets to fix this problem to no avail
return Scaffold(
backgroundColor: Colors.grey[200],
floatingActionButton: Column(
mainAxisAlignment: MainAxisAlignment.end,
children: [
FloatingActionButton(
onPressed: () {
//PATIENT.
if (currTab == 0) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AddPatient(),
),
);
} else {
//SHIPMENT.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const AddShipment(),
),
);
}
},
heroTag: null,
child: Icon(currTab == 0
? Icons.person_add
: Icons.add_shopping_cart_rounded),
),
const SizedBox(
height: 10,
),
currTab == 0 ? _getFAB() : const SizedBox(),
// FloatingActionButton(
// onPressed: () {},
// heroTag: null,
// child: const Icon(Icons.sort_rounded),
// ),
],
),
resizeToAvoidBottomInset: true,
body: DefaultTabController(
length: 2,
child: NestedScrollView(
body: TabBarView(
controller: _tabController,
physics: const NeverScrollableScrollPhysics(),
children: [
StreamBuilder<List<Patient>>(
stream: readPatients(orderTypePatients),
builder: (context, snapshot) {
if (snapshot.hasData) {
countPatients = snapshot.data!.length;
} else {
countPatients = 0;
}
return CustomScrollView(
slivers: [
if (snapshot.hasData)
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
final patient = snapshot.data;
return makeCardPatient(patient![index]);
} else if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(
color: Colors.green),
);
} else if (snapshot.hasError) {
return const SliverToBoxAdapter(
child: Center(child: Text('has err')));
} else {
return const Center(
child: CircularProgressIndicator(
color: Colors.green),
);
}
},
childCount: snapshot.data?.length,
),
)
else if (snapshot.hasError)
Center(
child: Text(
snapshot.error.toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
),
)
else
const SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: CircularProgressIndicator(
color: Colors.green,
),
),
),
),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text(
'Total:\n$totalSumPatients',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
fontSize: 21,
),
),
],
),
),
),
],
);
},
),
StreamBuilder<List<Shipment>>(
stream: readShipments(orderTypeShipments),
builder: (context, snapshot) {
if (snapshot.hasData) {
countShipments = snapshot.data!.length;
} else {
countShipments = 0;
}
return CustomScrollView(
slivers: [
if (snapshot.hasData)
SliverList(
delegate: SliverChildBuilderDelegate(
(BuildContext context, int index) {
if (snapshot.hasData) {
final shipment = snapshot.data;
return makeCardShipment(shipment![index]);
} else if (!snapshot.hasData) {
return const Center(
child: CircularProgressIndicator(
color: Colors.green),
);
} else if (snapshot.hasError) {
return const SliverToBoxAdapter(
child: Center(child: Text('has err')));
} else {
return const Center(
child: CircularProgressIndicator(
color: Colors.green),
);
}
},
childCount: snapshot.data?.length,
),
)
else if (snapshot.hasError)
SliverToBoxAdapter(
child: Center(
child: Text(
snapshot.error.toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
),
))
else
const SliverToBoxAdapter(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Center(
child: CircularProgressIndicator(
color: Colors.green,
),
),
),
),
SliverToBoxAdapter(
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Center(
child: Text(
'Total:\n$totalSumShipment',
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.w900,
fontSize: 21,
),
),
),
),
),
],
);
},
),
],
),
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) {
return [
SliverAppBar(
stretch: true,
bottom: TabBar(
controller: _tabController,
indicatorSize: TabBarIndicatorSize.tab,
indicatorColor: Colors.green,
indicatorWeight: 5,
labelColor: Colors.green,
labelStyle: const TextStyle(
color: Colors.black,
fontWeight: FontWeight.bold,
),
unselectedLabelColor: Colors.grey,
overlayColor:
MaterialStateProperty.all<Color>(Colors.grey[300]!),
onTap: (value) {
totalSumShipment = 0;
totalSumPatients = 0;
searchController.clear();
FocusManager.instance.primaryFocus?.unfocus();
currTab = value;
setState(() {});
},
tabs: [
Tab(
icon: Stack(
children: [
Icon(
Icons.supervised_user_circle_rounded,
color: currTab == 0 ? Colors.green : Colors.grey,
size: 36,
),
countPatients == 0
? const SizedBox(
height: 1,
)
: Positioned(
right: 0,
child: Container(
padding: const EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(7),
),
constraints: const BoxConstraints(
minWidth: 15,
minHeight: 15,
maxHeight: 15,
maxWidth: 15,
),
child: Text(
countPatients.toInt().toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 10,
),
textAlign: TextAlign.center,
),
),
),
],
),
text: 'Patients',
),
Tab(
icon: Stack(
children: [
Icon(
Icons.shopping_bag_rounded,
color: currTab == 1 ? Colors.green : Colors.grey,
size: 36,
),
countShipments == 0
? const SizedBox(
height: 1,
)
: Positioned(
right: 0,
child: Container(
padding: const EdgeInsets.all(1),
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.circular(7),
),
constraints: const BoxConstraints(
minWidth: 15,
minHeight: 15,
maxHeight: 15,
maxWidth: 15,
),
child: Text(
countShipments.toInt().toString(),
style: const TextStyle(
color: Colors.white,
fontSize: 10,
),
textAlign: TextAlign.center,
),
),
),
],
),
text: 'Orders',
),
],
),
pinned: true,
snap: false,
floating: true,
elevation: 10,
expandedHeight: 300.0,
stretchTriggerOffset: 150.0,
backgroundColor: Colors.grey.shade200,
flexibleSpace: FlexibleSpaceBar(
stretchModes: const [
StretchMode.zoomBackground,
StretchMode.fadeTitle,
StretchMode.blurBackground,
],
titlePadding: const EdgeInsetsDirectional.all(0),
background: SafeArea(
child: Stack(
alignment: AlignmentDirectional.topCenter,
fit: StackFit.loose,
children: [
Positioned(
top: 5,
right: 5,
child: IconButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SettingsPage(),
),
);
},
icon: const Icon(Icons.settings_rounded)),
),
Positioned(
top: 10,
right: 1,
left: 1,
child: Padding(
padding: const EdgeInsets.all(50.0),
child: GestureDetector(
onTap: () {
log(countShipments.toString());
setState(() {});
},
child: Image.asset(
'assets/images/my_Logo.png',
fit: BoxFit.fitWidth,
),
),
),
),
Positioned(
top: 120,
left: 10,
right: 10,
child: Padding(
padding: const EdgeInsets.all(30.0),
child: searchField(),
),
),
],
),
),
centerTitle: true,
collapseMode: CollapseMode.pin,
),
)
];
},
),
),
);
↑ is the scaffold
Card makeCardPatient(Patient patient) {
totalSumPatients = totalSumPatients + patient.balance;
return Card(
elevation: 8.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25.0),
),
margin: const EdgeInsets.symmetric(
horizontal: 10.0,
vertical: 6.0,
),
child: Container(
decoration: BoxDecoration(
color: const Color.fromARGB(228, 64, 96, 72),
borderRadius: BorderRadius.circular(25),
),
child: makeListTilePatient(patient),
),
);
}
↑ is the card wiget that is called in the streambuilder to create the Card widget
ListTile makeListTilePatient(Patient patient) {
return ListTile(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(25),
),
contentPadding: const EdgeInsets.symmetric(
horizontal: 20.0,
vertical: 10.0,
),
leading: Container(
padding: const EdgeInsets.only(right: 12.0),
decoration: const BoxDecoration(
border: Border(
right: BorderSide(
width: 1.0,
color: Colors.white24,
),
),
),
child: CircleAvatar(
radius: 25,
backgroundColor: patient.balance == 0 ? Colors.blue : Colors.red,
child: Text(
patient.balance.toStringAsFixed(2).toUpperCase(),
style: const TextStyle(
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
),
),
title: Text(
patient.name,
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
subtitle: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Expanded(
flex: 1,
child: Text(
DateFormat('yyyy/MM/dd').format(patient.lastvisit) == '1986/09/25'
? ' '
: timeAgo(patient.lastvisit),
style: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black,
),
),
),
Expanded(
flex: 1,
child: Padding(
padding: const EdgeInsets.only(left: 10.0),
child: Text(
DateFormat('yyyy/MM/dd').format(patient.appointment) ==
'1986/09/25'
? ' '
: timeFromNow(patient.appointment),
style: const TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
),
),
),
)
],
),
trailing: const Icon(
Icons.keyboard_arrow_right,
color: Colors.white,
size: 30.0,
),
onTap: () {
totalSumPatients = 0;
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => Details(
patient: patient,
),
),
);
setState(() {});
},
);
}
↑ is the Tile widget to detail whats in the card.
I know that there is something that I'm not seeing but tried searching and editing for a long while and I didnt find what I'm missing.
I'm new to SO can you please help me?
I tried wrapping some widgets with Slivertoboxadapter but didnt work.
extra functions used ,I dont think they are related to the problem but here they are:
Stream<List<Shipment>> readShipments(String orderTypeShipments) {
return FirebaseFirestore.instance
.collection('shipments')
.orderBy(
orderTypeShipments,
descending: sortOrder,
)
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Shipment.fromJson(doc.data()))
.where(
(element) =>
element.userId.toUpperCase() ==
Helper.currUserID.toUpperCase() &&
element.type.toUpperCase().contains(
searchController.text.toUpperCase(),
),
)
.toList());
}
Future<int> getPatientCount() async {
var x = FirebaseFirestore.instance
.collection('patients')
.orderBy(
orderTypePatients.toUpperCase(),
descending: sortOrder,
)
.snapshots()
.map((snapshot) => snapshot.docs
.map((doc) => Patient.fromJson(doc.data()))
.where((element) =>
element.user == Helper.currUserID &&
(element.name
.toUpperCase()
.contains(searchController.text.toUpperCase())))
.toList());
setState(() {});
return x.length;
}
As suggested by #Yeasin Sheikh I overlooked that line, silly me.
thanks yeasin.
I just had to wrap the Center widget with a SliverToBoxAdapter in the snapshot.error:
SliverToBoxAdapter(child:Center(...

Showmodalbuttom sheet 'Invalid constant value'

import 'package:flutter/material.dart';
import 'package:todoey/screen/tasks_list.dart';
class TasksScreen extends StatelessWidget {
Widget buildButtomSheet(BuildContext context) {
return Container();
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.lightBlueAccent,
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lightBlueAccent,
child: const Icon(Icons.add),
onPressed: () {
showModalBottomSheet = showModalBottomSheet(context: context, builder: buildButtomSheet);
},
),
body: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Container(
padding: const EdgeInsets.only(
top: 60.0, left: 30, right: 30, bottom: 30.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
CircleAvatar(
child: Icon(
Icons.list,
size: 30,
color: Colors.lightBlueAccent,
),
backgroundColor: Colors.white,
radius: 30,
),
SizedBox(height: 10),
Text(
'Todoey',
style: TextStyle(
color: Colors.white,
fontSize: 50,
fontWeight: FontWeight.w700,
),
),
Text(
'12 task',
style: TextStyle(
color: Colors.white,
),
),
],
),
),
Expanded(
child: Container(
padding: const EdgeInsets.symmetric(horizontal: 20),
decoration: const BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(30.0),
topRight: Radius.circular(30)),
color: Colors.white,
),
child: const TasksList(),
),
),
],
),
);
}
}
child: const Icon(Icons.add),
onPressed: () {
showModalBottomSheet = showModalBottomSheet(context: context, builder: buildButtomSheet);
I had issues with the code line above, i have tried several changes but it still tells "Invalid function".
Remove const from FloatingActionButton, because onPressed method will be called on run time.
floatingActionButton: FloatingActionButton(
onPressed: () {},
),
More about const
floatingActionButton: FloatingActionButton(
backgroundColor: Colors.lightBlueAccent,
child: const Icon(Icons.add),
onPressed: () async {
final result = await showModalBottomSheet(
context: context, builder: buildButtomSheet);
},
),

How to align rows at bottom of the screen along with other containers at top

I am trying to achieve a UI in which there are 2 buttons in the center and then afterwards there are 2 rows at the bottom of the screen fixed respective to screen sizes I will stay at its place, but I'm unable to do it in my case I'm using Column and all the other containers and rows are in it.
Desired Result on emulator is fine
What I am getting in real device
Here is the code.
class IntroPage extends StatelessWidget {
const IntroPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF29F76),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Stack(
children: <Widget>[
Image.asset(
"assets/intropage.png",
fit: BoxFit.fill,
),
Padding(
padding: const EdgeInsets.only(
top: 550,
),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignUpPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign Up",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black),
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(
top: 650,
),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignInPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign In",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFFFE6B01)),
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.only(
top: 750,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 2,
width: 150,
color: Colors.white,
),
const Text(
" Please Read ",
style: TextStyle(color: Colors.white),
),
Container(
height: 2,
width: 150,
color: Colors.white,
),
],
),
),
Padding(
padding: const EdgeInsets.only(
top: 760,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
TermsandConditions()));
},
child: const Text(
"Terms & Conditions",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PrivacyPolicy()));
},
child: const Text(
"Privacy Policy",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
],
),
),
],
)
],
),
),
);
}
}
Use this method its will do the job. Background image will appear and you can align your widgets using the Alignment property
class IntroPage extends StatelessWidget {
const IntroPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Container(
decoration: const BoxDecoration(
image: DecorationImage(
fit: BoxFit.fill,
image: AssetImage(
'assets/intropage.png',
),
)),
child: Scaffold(
backgroundColor: Colors.transparent,
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(top: 250.0),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignUpPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign Up",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black),
),
),
),
),
),
),
),
Align(
alignment: Alignment.center,
child: Padding(
padding: const EdgeInsets.only(top: 500.0),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignInPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign In",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFFFE6B01)),
),
),
),
),
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Padding(
padding: const EdgeInsets.only(bottom: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 2,
width: 150,
color: Colors.white,
),
const Text(
" Please Read ",
style: TextStyle(color: Colors.white),
),
Container(
height: 2,
width: 150,
color: Colors.white,
),
],
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => TermsandConditions()));
},
child: const Text(
"Terms & Conditions",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PrivacyPolicy()));
},
child: const Text(
"Privacy Policy",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
],
),
),
],
)),
);
}
}
Try to wrap your widgets inside
Positioned(
Align(
alignment: Alignment.bottomCenter,
child: _your_widget_here()
It's not recommanded to use fix value for the layout.
Stack here it's not necessary.
A simple Spacer Widget can the work. But there are multiple way to do it.
import 'package:flutter/material.dart';
class IntroPage extends StatelessWidget {
const IntroPage({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF29F76),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Image.asset(
"assets/intropage.png",
fit: BoxFit.fitWidth,
),
const Spacer(),
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignUpPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign Up",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Colors.black),
),
),
),
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => const SignInPage()));
},
child: Container(
width: 180,
height: 60,
decoration: BoxDecoration(
color: Colors.white,
border: Border.all(
color: Colors.white,
),
borderRadius:
const BorderRadius.all(Radius.circular(40))),
child: const Center(
child: Text(
"Sign In",
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: Color(0xFFFE6B01)),
),
),
),
),
),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
height: 2,
width: 150,
color: Colors.white,
),
const Text(
" Please Read ",
style: TextStyle(color: Colors.white),
),
Container(
height: 2,
width: 150,
color: Colors.white,
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Padding(
padding: const EdgeInsets.only(left: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
TermsandConditions()));
},
child: const Text(
"Terms & Conditions",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
Padding(
padding: const EdgeInsets.only(right: 20, top: 6),
child: GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => PrivacyPolicy()));
},
child: const Text(
"Privacy Policy",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
),
],
),
],
),
),
);
}
}
If you can use standard buttons, use them. I used ElevatedButton with custom styles, so please check it.
In general it is a bad idea to add accumulated top padding to each element. Screens sizes are different and your layout will be failed on another screen size.
Please see my example of how to do this better.
Also I added other small improvements, maybe you will be interested in.
class IntroPage extends StatelessWidget {
const IntroPage({Key? key}) : super(key: key);
Widget _createButton({
required VoidCallback onPressed,
required Widget child,
required Color textColor,
}) {
return ElevatedButton(
onPressed: onPressed,
child: child,
style: ElevatedButton.styleFrom(
elevation: 0.0,
primary: Colors.white,
onPrimary: textColor,
textStyle: TextStyle(
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
padding: EdgeInsets.all(8.0),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(40),
),
fixedSize: Size(180.0, 60.0),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: const Color(0xFFF29F76),
body: SafeArea(
child: Column(
children: <Widget>[
Expanded(
child: Stack(
children: <Widget>[
Align(
alignment: Alignment.topCenter,
child: SizedBox(
height: 400.0, //your image height
child: Image.network(
"https://images.ctfassets.net/hrltx12pl8hq/61DiwECVps74bWazF88Cy9/2cc9411d050b8ca50530cf97b3e51c96/Image_Cover.jpg?fit=fill&w=480&h=270",
fit: BoxFit.cover,
),
),
),
Align(
alignment: Alignment.bottomCenter,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
_createButton(
child: Text("Sign Up"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
//your page
builder: (context) => Container(),
),
);
},
textColor: Colors.black,
),
SizedBox(height: 32.0),
_createButton(
child: Text("Sign In"),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
//your page
builder: (context) => Container(),
),
);
},
textColor: Color(0xFFFE6B01),
),
SizedBox(height: 32.0),
],
),
),
],
),
),
_BottomSection(),
],
),
),
);
}
}
class _BottomSection extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Column(
children: [
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: const <Widget>[
Expanded(
child: Divider(
height: 2,
thickness: 2,
color: Colors.white,
),
),
SizedBox(width: 10),
Text(
"Please Read",
style: TextStyle(color: Colors.white),
),
SizedBox(width: 10),
Expanded(
child: Divider(
height: 2,
thickness: 2,
color: Colors.white,
),
),
],
),
Padding(
padding: const EdgeInsets.only(
top: 6.0,
right: 20.0,
left: 20.0,
),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => Container()));
},
child: const Text(
"Terms & Conditions",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => Container()));
},
child: const Text(
"Privacy Policy",
style: TextStyle(
color: Colors.white,
decoration: TextDecoration.underline),
),
),
],
),
),
],
);
}
}

how to create add minus button on my product in flutter?

May I know how to create a function that displays the value and have the button remove and add to the product list? I already create an icon button for remove and add to the screen but I do not have any clues on how to use the button.
GUI on application:
my aim is to have this kind of button:
my full code for this GUI:
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/material.dart';
import 'CartList.dart';
import 'bottom_navi_iconW.dart';
import 'globals.dart' as globals;
class SarapanPagi extends StatefulWidget {
final List list;
final int index;
final String category;
SarapanPagi({this.index,this.list,this.category});
#override
_SarapanPagiState createState() => _SarapanPagiState();
}
class _SarapanPagiState extends State<SarapanPagi> {
Future<List> getData() async{
var url = 'http://10.0.2.2/foodsystem/breakfastlist.php';
var data = {
'product_type': globals.jenisCategory,
'product_owner': widget.list[widget.index]['restaurant_id'],
};
var response = await http.post(url, body: json.encode(data));
//final response= await http.get("http://10.0.2.2/foodsystem/getdata.php");
return json.decode(response.body);}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
iconTheme: IconThemeData(color: Colors.black),
title: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
//Text("Restaurant's Owner Page"),
Text(widget.list[widget.index]['restaurant_name'], textAlign: TextAlign.center, style: TextStyle(fontWeight: FontWeight.w700), ),
],
),
centerTitle: false,
//automaticallyImplyLeading: false,
),
body:
Padding(
padding: const EdgeInsets.only(bottom: 0, left: 5, right: 5),
child: Column(
children: [
/*FloatingActionButton(
onPressed: (){
return showDialog(
context: context,
builder: (context){
return AlertDialog(
content: Text(
globals.jenisCategory
),
);
},
);
},
),*/
//SizedBox(height: 30,),
Container(
//decoration: BoxDecoration(border: Border.all(color: Colors.black, width: 4), borderRadius: BorderRadius.circular(15)),
height: 627,
child: FutureBuilder<List>(
future: getData(),
builder: (context, snapshot){
if(snapshot.hasError) print(snapshot.error);
return snapshot.hasData ?
ItemList(list: snapshot.data,) :
Center(child: CircularProgressIndicator(),);
},
),
),
],
),
),
bottomNavigationBar:
Container(
height: 70,
color: Colors.red,
child: BottomNavIcon(
onTap: (){
Navigator.of(context).push(new MaterialPageRoute(
builder: (BuildContext context)=> new CartListItem()),);
},
image: "troli.png",
name: "CART",
),
),
);
}
}
class ItemList extends StatelessWidget {
final List list;
ItemList({this.list});
#override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Container(
//color: Colors.red.shade100,
height: 627,
child: ListView.builder(
itemCount: list==null ? 0 : list.length,
itemBuilder: (context, i){
return new Container(
//decoration: BoxDecoration(border: Border.all(color: Colors.blue, width: 4), borderRadius: BorderRadius.circular(15)),
height: 250,
child: new GestureDetector(
onTap: (){},
child: new Card(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Column(
children: [
Padding(
padding: const EdgeInsets.only(bottom : 5.0),
child: Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 23, fontWeight: FontWeight.bold),),
),
Row(
children: [
//Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 30),),
Padding(
padding: const EdgeInsets.all(8.0),
child: Image.asset(
"menulist/${list[i]['image']}",
width: 150,
height: 150,
),
),
Padding(
padding: const EdgeInsets.only(left: 20.0, bottom: 0),
child:
Column(
children: [
//Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 30),),
Text("Price RM : ${list[i]["product_price"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 25),),
Row(
children: [
IconButton(
icon: Icon(Icons.remove),
onPressed: (){},
iconSize: 15,
),
IconButton(
icon: Icon(Icons.add),
onPressed: (){},
iconSize: 15,
),
],
),
RaisedButton(
shape: RoundedRectangleBorder(borderRadius: new BorderRadius.circular(40.0)),
color: Colors.red.shade300,
child: Text("Add to Cart", style: TextStyle(fontWeight: FontWeight.bold),),
onPressed: (){},
)
],
),
),
],
),
],
),
/*ListTile(
title: Text(list[i]["product_name"], textAlign: TextAlign.center, style: TextStyle(fontSize: 30),),
leading:
Image.asset(
"images/${list[i]['image']}",
width: 100,
height: 100,
),
subtitle: Text("Price RM : ${list[i]["product_price"]}", textAlign: TextAlign.center, style: TextStyle(fontSize: 25),),
),*/
],
),
),
),
);
},
),
),
);
}
}
First of all you need to change it to StatefulWidget, since the UI interaction is rendered (changed price). Then something like
onPressed: () => setState(() => list[i]["product_price"] += unitprice),
will do

How to show data in function as a loop?

I am beginner in flutter and following some tutorials. I need to know how can I show data in a function by looping? For now, I am calling function 2 or 3 times to show data on how its possible ill call function just one time and shop my data which is in array?
Here. my data file which name is post_model.dart
class Post {
String authorName;
String authorImageUrl;
String timeAgo;
String imageUrl;
Post({
this.authorName,
this.authorImageUrl,
this.timeAgo,
this.imageUrl,
});
}
final List<Post> posts = [
Post(
authorName: 'Umaiz Khan',
authorImageUrl: 'assets/images/user0.png',
timeAgo: '5 min',
imageUrl: 'assets/images/post0.jpg',
),
Post(
authorName: 'Saad ahmed',
authorImageUrl: 'assets/images/user1.png',
timeAgo: '10 min',
imageUrl: 'assets/images/post1.jpg',
),
Post(
authorName: 'Hiba',
authorImageUrl: 'assets/images/user4.png',
timeAgo: '10 min',
imageUrl: 'assets/images/post2.jpg',
),
];
final List<String> stories = [
'assets/images/user1.png',
'assets/images/user2.png',
'assets/images/user3.png',
'assets/images/user4.png',
'assets/images/user0.png',
'assets/images/user1.png',
'assets/images/user2.png',
'assets/images/user3.png',
];
Here is my code at the end of the line you can see I am calling function and sending index. I need to call the function just one time and it will show all my arrays in data. Thanks in advance
import 'package:flutter/material.dart';
import 'package:curved_navigation_bar/curved_navigation_bar.dart';
import 'package:mytravel/screens/loginPage.dart';
import 'package:mytravel/screens/guidePlacePage.dart';
import 'package:mytravel/models/post_model.dart';
import 'package:mytravel/screens/view_post_screen.dart';
class newsFeedPage extends StatefulWidget {
#override
_newsFeedPageState createState() => _newsFeedPageState();
}
class _newsFeedPageState extends State<newsFeedPage> {
List<Widget> _buildPost() {
List<Widget> items = [];
items.add(
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 5.0),
child: Container(
width: double.infinity,
height: 560.0,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(25.0),
),
child: Column(
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(vertical: 10.0),
child: Column(
children: <Widget>[
ListTile(
leading: Container(
width: 50.0,
height: 50.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
boxShadow: [
BoxShadow(
color: Colors.black45,
offset: Offset(0, 2),
blurRadius: 6.0,
),
],
),
child: CircleAvatar(
child: ClipOval(
child: Image(
height: 50.0,
width: 50.0,
image: AssetImage(posts[].authorImageUrl),
fit: BoxFit.cover,
),
),
),
),
title: Text(
posts[].authorName,
style: TextStyle(
fontWeight: FontWeight.bold,
),
),
subtitle: Text(posts[].timeAgo),
trailing: IconButton(
icon: Icon(Icons.more_horiz),
color: Colors.black,
onPressed: () => print('More'),
),
),
InkWell(
onDoubleTap: () => print('Like post'),
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ViewPostScreen(
post: posts[],
),
),
);
},
child: Container(
margin: EdgeInsets.all(10.0),
width: double.infinity,
height: 400.0,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(25.0),
boxShadow: [
BoxShadow(
color: Colors.black45,
offset: Offset(0, 5),
blurRadius: 8.0,
),
],
image: DecorationImage(
image: AssetImage(posts[].imageUrl),
fit: BoxFit.fitWidth,
),
),
),
),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
children: <Widget>[
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.favorite_border),
iconSize: 30.0,
onPressed: () => print('Like post'),
),
Text(
'2,515',
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w600,
),
),
],
),
SizedBox(width: 20.0),
Row(
children: <Widget>[
IconButton(
icon: Icon(Icons.chat),
iconSize: 30.0,
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (_) => ViewPostScreen(
post: posts[],
),
),
);
},
),
Text(
'350',
style: TextStyle(
fontSize: 14.0,
fontWeight: FontWeight.w600,
),
),
],
),
],
),
IconButton(
icon: Icon(Icons.bookmark_border),
iconSize: 30.0,
onPressed: () => print('Save post'),
),
],
),
),
],
),
),
],
),
),
),
);
for (var i = 0; i < posts.length; i++) {
items.add(_buildPost(i));
}
return items;
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFEDF0F6),
body: ListView(
physics: AlwaysScrollableScrollPhysics(),
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Social Travel',
style: TextStyle(
fontFamily: 'Billabong',
fontSize: 32.0,
),
),
],
),
),
ListView(
physics: AlwaysScrollableScrollPhysics(),
children: _buildPost(),
),
],
),
);
}
}
You can build the children array of your ListView in a new method like this :
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFFEDF0F6),
body: ListView(
physics: AlwaysScrollableScrollPhysics(),
children: _buildListViewItems(),
),
);
}
List<Widget> _buildListViewItems() {
List<Widget> items = [];
items.add(
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Instagram',
style: TextStyle(
fontFamily: 'Billabong',
fontSize: 32.0,
),
),
],
),
),
);
for (var i = 0; i < posts.length; i++) {
items.add(_buildPost(i));
}
return items;
}