Flutter will not color in containers - flutter

Im am using flutter and I have a custom widget that I've made. I use a list.builder to create the list of widgets. Now I want to color the container in which the custom widget lives in but I cant. I color coded each of the container I could see but for some reason I cant. I took it a step further and colored all the indiviual parts of the custom widget to no avail.
Custom Widget:
return Scaffold(
body: Center(
child: AnimatedContainer(
width: 400,
height: 400,
duration: Duration(seconds: 0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
border: Border.all(color: Colors.black),
color: Colors.pink,
),
child: Stack(
children: [
Container(
margin: EdgeInsets.fromLTRB(15, 15, 15, 0),
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 3),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
border: Border.all(color: Colors.black),
color: Colors.indigoAccent,
),
child: Text(
title,
overflow: TextOverflow.ellipsis,
),
),
Container(
color: Colors.limeAccent,
child: FutureBuilder(
future: RetrieveProfile_getImage(),
initialData: [],
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
child: Text('Please wait its loading...'));
} else {
if (snapshot.hasError) {
return Center(
child: Text('Error: ${snapshot.error}'));
} else {
return new Container(
margin: EdgeInsets.fromLTRB(0, 50, 0, 0),
// padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
color: Colors.lightGreenAccent,
height: 275,
child: Center(
child: Image(
fit: BoxFit.contain,
image: MemoryImage(_base64),
),
),
);
}
}
}),
),
Container(
margin: EdgeInsets.fromLTRB(15, 330, 15, 0),
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 3),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
border: Border.all(color: Colors.black),
color: Colors.orange,
),
child: Text(
owner,
overflow: TextOverflow.ellipsis,
),
),
Container(
margin: EdgeInsets.fromLTRB(15, 360, 15, 0),
padding: EdgeInsets.symmetric(horizontal: 10, vertical: 3),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(5.0)),
border: Border.all(color: Colors.black),
color: Colors.purpleAccent,
),
child: Text(
location,
overflow: TextOverflow.ellipsis,
),
),
Container(
color: Colors.lightBlue,
margin: EdgeInsets.fromLTRB(340, 345, 15, 35),
child: FutureBuilder(
future: getLikes(),
initialData: [],
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(child: Text('Please wait its loading...'));
} else {
if (snapshot.hasError) {
return Center(child: Text('Error: ${snapshot.error}'));
} else {
return LikeButton(
isLiked: _liked,
animationDuration: Duration(milliseconds: 500),
onTap: changeLike,
likeBuilder: (bool isLiked) {
return Icon(
Icons.favorite,
color: isLiked ? Colors.red : Colors.grey,
);
},
countPostion: CountPostion.left,
likeCount: likeCount,
countBuilder: (likeCount, bool isLiked, String text) {
var color = isLiked ? Colors.red : Colors.grey;
Widget result;
if (likeCount == 0) {
result = Text(
'',
style: TextStyle(color: color),
);
} else {
result = Text(
likeCount.toString(),
style: TextStyle(color: color),
);
}
return result;
},
);
}
}
},
),
),
],
),
),
),
);
ListBuilder:
ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: likedEvent.length,
itemBuilder: (context, index) {
EventResource e =
likedEvent[index];
print("ID: " + e.photoModel.id);
return Material(
color: Colors.deepPurple,
child: Container(
color: Colors.amber,
margin: EdgeInsets.all(12),
child: Container(
width: 430,
color: Colors.green,
child: Container(
color: Colors.blue,
child: Event(
user: id,
owner: e.owner,
location: e.address,
title: e.name,
ImageID: e.photoModel.id,
)
),
),
));
},
);
Flutter Render:

Set a backgroundColor inside scaffold

Related

How to create a screen with a condition in flutter dart

I need a little help with flutter. I want to make a screen with a condition. The condition is if we have data show that data from firebase but if we don't have data show a button that says add data. The codes below show data from firebase but when we have no data that shows nothing.
Here are my codes that show a black screen.
#override
Widget build(BuildContext context) {
return GetBuilder<MapLogic>(
builder: (mapLogic) => Scaffold(
// backgroundColor: Colors.red,
body: StreamBuilder<QuerySnapshot>(
stream:
FirebaseFirestore.instance.collection('collection').snapshots(),
builder: (context, snapshot) {
if (snapshot.hasData) {
if (snapshot.data!.docs.isEmpty) {
return Padding(
padding: const EdgeInsets.only(top: 30),
child: InkWell(
onTap: () async {
generalController.focusOut(context);
Get.to(const AddNewVehicle());
},
child: Container(
height: 55,
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Styles.primaryColor,
borderRadius: BorderRadius.circular(30),
boxShadow: [
BoxShadow(
color: Styles.primaryColor.withOpacity(0.19),
blurRadius: 40,
spreadRadius: 0,
offset: const Offset(
0, 22), // changes position of shadow
),
],
),
child: const Center(
child: Text(
"Add A Vehicle",
// style: state.buttonTextStyle
),
),
),
),
);
} else {
return FadedSlideAnimation(
child: Wrap(
children:
List.generate(snapshot.data!.docs.length, (index) {
if (snapshot.data!.docs[index].get('driver_id') ==
mapLogic.currentDriverData?.get('id')) {
return Padding(
padding: const EdgeInsets.fromLTRB(15, 30, 15, 0),
child: InkWell(
onTap: () {},
child: Container(
width: MediaQuery.of(context).size.width,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(19),
boxShadow: [
BoxShadow(
color: customThemeColor.withOpacity(0.19),
blurRadius: 40,
spreadRadius: 0,
offset: const Offset(
0, 22), // changes position of shadow
),
],
),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Row(
children: [
///---image
Hero(
tag:
'${snapshot.data!.docs[index].get('image')}',
child: Material(
child: Container(
height: 80,
width: 80,
decoration: const BoxDecoration(
color: Colors.grey,
shape: BoxShape.circle),
child: ClipRRect(
borderRadius:
BorderRadius.circular(40),
child: Image.network(
'${snapshot.data!.docs[index].get('image')}',
fit: BoxFit.cover,
),
),
),
),
),
///---price
Expanded(
child: Align(
alignment:
Alignment.center,
child: Container(
width: 70,
decoration: BoxDecoration(
borderRadius:
BorderRadius
.circular(
19),
color:
customThemeColor),
child: Padding(
padding:
const EdgeInsets
.fromLTRB(
0, 2, 0, 2),
child: Center(
child: Text(
'\$${snapshot.data!.docs[index].get('dis_price')}',
style: state
.priceTextStyle!
.copyWith(
color: Colors
.white)),
),
),
),
),
),
],
),
],
),
),
))
],
),
),
),
),
);
} else {
return const SizedBox();
}
}),
),
beginOffset: const Offset(0, 0.3),
endOffset: const Offset(0, 0),
slideCurve: Curves.linearToEaseOut,
);
}
} else {
return Text(
'Record not found',
textAlign: TextAlign.center,
style: GoogleFonts.poppins(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w600),
);
}
}),
),
);
}
You can follow this pattern but I will suggest following docs' one
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done &&
!snapshot.hasData) {
return Text("could not find any data");
}
if (snapshot.hasData) {...}
if (snapshot.hasError) return Text("error");
return CircularProgressIndicator();
}

TextField don't resize my alertdialog - Flutter

I leave the entire code fragment, the main idea is to have a textfield that, by using the keyboard, shows me a series of elements, in the description variable, I have that element to select, this works well, the only issue is, how to keep my alertdialog with all the correct notes.
return await Get.generalDialog(
barrierLabel: "Barrier",
barrierDismissible: false,
transitionBuilder: (context, a1, a2, widget) {
return StatefulBuilder(builder: (context, setState) {
return Transform.scale(
scale: a1.value,
child: Opacity(
opacity: a1.value,
child: Dialog(
insetAnimationCurve: Curves.bounceOut,
insetPadding: EdgeInsets.only(right: 20, left: 20),
//backgroundColor: colorFour(),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12.0),
),
child: SingleChildScrollView(
child: Container(
height: 340,
child: Column(
children: <Widget>[
Container(
alignment: Alignment.topCenter,
height: 50,
decoration: BoxDecoration(
color: Colors.red,
borderRadius: BorderRadius.only(
topLeft: Radius.circular(12),
topRight: Radius.circular(12),
),
),
padding: EdgeInsets.all(5),
child: Center(
child: Text(
title,
style: TextStyle(
color: Colors.white,
fontSize: 18,
fontWeight: FontWeight.w600),
maxLines: 2,
textAlign: TextAlign.center,
),
),
),
SingleChildScrollView(
padding: EdgeInsets.only(left: 10, right: 10),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: [
Container(
margin: EdgeInsets.only(top: 10),
child: TextFormField(
controller: controller,
decoration: InputDecoration(
border: OutlineInputBorder(
gapPadding: 5.0)),
keyboardType: TextInputType.text,
maxLines: 1,
onChanged: (String cadena) async {
listado = pivote
.where((x) => x.descripcion
.split("/")[value]
.toLowerCase()
.contains(
cadena.toLowerCase()))
.toList();
setState(() {});
},
),
),
Container(
height: 200,
margin: EdgeInsets.only(
top: 10, bottom: 10.0),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(
Radius.circular(5)),
border: Border.all(
color: Colors.grey, width: 1)),
child: ListView.builder(
padding: const EdgeInsets.all(0.0),
physics: BouncingScrollPhysics(),
itemCount: listado.length,
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemBuilder: (context, index) {
return Container(
height: 34,
child: InkWell(
child: SizedBox.expand(
child: Align(
alignment:
Alignment.centerLeft,
child: Padding(
padding:
EdgeInsets.only(
right: 5,
left: 5),
child: Text(
listado[index]
.descripcion
.split(
"/")[value],
textAlign:
TextAlign.left),
),
),
),
onTap: () {
Get.back(result: {
"item": listado[index],
});
}),
);
}),
),
],
)),
],
),
),
),
),
));
});
},
pageBuilder: (context, animation1, animation2) {
return Container();
});
I need to overlay my alertdialog, that is to say that the size does not break my design, this happens after focusing on the textfield
Show us some code to help you out.
In any case, it might make sense to wrap the layout in a SingleChildScrollView.
Here is a very good explanation.
https://api.flutter.dev/flutter/widgets/SingleChildScrollView-class.html
yourAlertDialog(
context: context,
builder: (BuildContext context) {
return SingleChildScrollView(child: yourchild);
},
);
Your layout should be wrapped in Scrollview , I didn't replicate or run this code but it should be like this:
AlertDialog(
title: Text("title", style: TextStyle(color: ThemeColors.colorPrimary),),
content: Container(
width: double.maxFinite,
child: SingleChildScrollView(
scrollDirection: Axis.vertical,
child: StatefulBuilder(builder: (BuildContext context1, StateSetter setState) {
this.setState = setState;
return Container(
height: 340,
child: Column(
children: <Widget>[
//your container
],
),
),
}),
)),
);

Refresh listview based on button click

I have a list of categories and products. I want that when I click on a category:
The selected category should change color to orange.
The products listview should be refreshed with the selected category's products.
I tried to add setState and try to change both color and listview but that did not work. Any ideas what I need to rectify?
Future fetchProducts() async {
return await Provider.of<Products>(context, listen: false)
.fetchAndSetProducts('title', false);
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
appBar: AppBar(
backgroundColor: Colors.grey[100],
elevation: 0,
brightness: Brightness.light,
leading: Icon(null),
actions: <Widget>[
IconButton(
onPressed: () {},
icon: Icon(
Icons.shopping_basket,
color: Colors.grey[800],
),
)
],
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: FutureBuilder(
future: _screenFuture,
// ignore: missing_return
builder: (context, snap) {
if (snap.error != null &&
!snap.error
.toString()
.contains('NoSuchMethodError')) {
return Center(child: Text('Something went wrong!'));
} else if (snap.hasData) {
var categoriesData = Provider.of<Categories>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
FadeAnimation(
1,
Text(
'Food Delivery',
style: TextStyle(
color: Colors.grey[80],
fontWeight: FontWeight.bold,
fontSize: 30),
)),
SizedBox(
height: 20,
),
Container(
height: 50,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categoriesData.items.length,
itemBuilder: (ctx, i) => FadeAnimation(
i.toDouble(),
makeCategory(
isActive: i.toDouble() == 0
? true
: false,
title: categoriesData.items
.toList()[i]
.title)))),
SizedBox(
height: 10,
),
],
);
} else if (snap.connectionState ==
ConnectionState.waiting) {
//return Container();
return Center(child: Spinner());
}
})),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: FutureBuilder(
future: _productScreenFuture,
// ignore: missing_return
builder: (context, snap) {
if (snap.error != null &&
!snap.error
.toString()
.contains('NoSuchMethodError')) {
return Center(child: Text('Something went wrong!'));
} else if (snap.hasData) {
productsData = Provider.of<Products>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FadeAnimation(
1,
Text(
'Food Delivery',
style: TextStyle(
color: Colors.grey[80],
fontWeight: FontWeight.bold,
fontSize: 30),
)),
SizedBox(
height: 20,
),
SizedBox(
height: 300,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: productsData.items.length,
itemBuilder: (ctx, i) => FadeAnimation(
1.4,
makeItem(
image: 'assets/images/one.jpg',
title: productsData.items[i].title,
price:
productsData.items[i].price)))),
SizedBox(
height: 10,
),
],
);
} else if (snap.connectionState ==
ConnectionState.waiting) {
//return Container();
return Center(child: Spinner());
}
})),
SizedBox(
height: 30,
)
],
),
),
);
}
Widget makeCategory({isActive, title}) {
return AspectRatio(
aspectRatio: isActive ? 3 : 2.5 / 1,
child: GestureDetector(
onTap: () {
print(title + " clicked");
setState(() {
isActive = true;
productsData = Provider.of<Products>(context, listen: false)
.findBycategoryName(title);
print(productsData.first.title); // << data is available
});
},
child: Container(
margin: EdgeInsets.only(right: 10),
decoration: BoxDecoration(
color: isActive ? Colors.yellow[700] : Colors.white,
borderRadius: BorderRadius.circular(50),
),
child: Align(
child: Text(
title,
style: TextStyle(
color: isActive ? Colors.white : Colors.grey[500],
fontSize: 18,
fontWeight: isActive ? FontWeight.bold : FontWeight.w100),
),
),
),
));
}
Widget makeItem({image, String title, double price}) {
return AspectRatio(
aspectRatio: 1 / 1.5,
child: GestureDetector(
child: Container(
margin: EdgeInsets.only(right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
image: AssetImage(image),
fit: BoxFit.cover,
)),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: LinearGradient(begin: Alignment.bottomCenter, stops: [
.2,
.9
], colors: [
Colors.black.withOpacity(.9),
Colors.black.withOpacity(.3),
])),
child: //Expanded(
Padding(
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: Icon(
Icons.favorite,
color: Colors.white,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"\Tsh. $price",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
title,
style: TextStyle(color: Colors.white, fontSize: 20),
)
],
)
],
),
),
),
),
),
);
}
}
Actually When you call setState() your isActive is again changed to false because of this code:
makeCategory(
isActive: i.toDouble() == 0
? true
: false,
TRY THIS CODE:
bool currentCategory = 0;
Future fetchProducts() async {
return await Provider.of<Products>(context, listen: false)
.fetchAndSetProducts('title', false);
}
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[100],
appBar: AppBar(
backgroundColor: Colors.grey[100],
elevation: 0,
brightness: Brightness.light,
leading: Icon(null),
actions: <Widget>[
IconButton(
onPressed: () {},
icon: Icon(
Icons.shopping_basket,
color: Colors.grey[800],
),
)
],
),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: EdgeInsets.symmetric(horizontal: 10.0),
child: FutureBuilder(
future: _screenFuture,
// ignore: missing_return
builder: (context, snap) {
if (snap.error != null &&
!snap.error
.toString()
.contains('NoSuchMethodError')) {
return Center(child: Text('Something went wrong!'));
} else if (snap.hasData) {
var categoriesData = Provider.of<Categories>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
FadeAnimation(
1,
Text(
'Food Delivery',
style: TextStyle(
color: Colors.grey[80],
fontWeight: FontWeight.bold,
fontSize: 30),
)),
SizedBox(
height: 20,
),
Container(
height: 50,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: categoriesData.items.length,
itemBuilder: (ctx, i) => FadeAnimation(
i.toDouble(),
makeCategory(
isActive: i == currentCategory
? true
: false,
position: i,
title: categoriesData.items
.toList()[i]
.title)))),
SizedBox(
height: 10,
),
],
);
} else if (snap.connectionState ==
ConnectionState.waiting) {
//return Container();
return Center(child: Spinner());
}
})),
Padding(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: FutureBuilder(
future: _productScreenFuture,
// ignore: missing_return
builder: (context, snap) {
if (snap.error != null &&
!snap.error
.toString()
.contains('NoSuchMethodError')) {
return Center(child: Text('Something went wrong!'));
} else if (snap.hasData) {
productsData = Provider.of<Products>(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
FadeAnimation(
1,
Text(
'Food Delivery',
style: TextStyle(
color: Colors.grey[80],
fontWeight: FontWeight.bold,
fontSize: 30),
)),
SizedBox(
height: 20,
),
SizedBox(
height: 300,
child: ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.horizontal,
itemCount: productsData.items.length,
itemBuilder: (ctx, i) => FadeAnimation(
1.4,
makeItem(
image: 'assets/images/one.jpg',
title: productsData.items[i].title,
price:
productsData.items[i].price)))),
SizedBox(
height: 10,
),
],
);
} else if (snap.connectionState ==
ConnectionState.waiting) {
//return Container();
return Center(child: Spinner());
}
})),
SizedBox(
height: 30,
)
],
),
),
);
}
Widget makeCategory({isActive, title, position}) {
return AspectRatio(
aspectRatio: isActive ? 3 : 2.5 / 1,
child: GestureDetector(
onTap: () {
print(title + " clicked");
setState(() {
currentCategory = position;
productsData = Provider.of<Products>(context, listen: false)
.findBycategoryName(title);
print(productsData.first.title); // << data is available
});
},
child: Container(
margin: EdgeInsets.only(right: 10),
decoration: BoxDecoration(
color: isActive ? Colors.yellow[700] : Colors.white,
borderRadius: BorderRadius.circular(50),
),
child: Align(
child: Text(
title,
style: TextStyle(
color: isActive ? Colors.white : Colors.grey[500],
fontSize: 18,
fontWeight: isActive ? FontWeight.bold : FontWeight.w100),
),
),
),
));
}
Widget makeItem({image, String title, double price}) {
return AspectRatio(
aspectRatio: 1 / 1.5,
child: GestureDetector(
child: Container(
margin: EdgeInsets.only(right: 20),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
image: DecorationImage(
image: AssetImage(image),
fit: BoxFit.cover,
)),
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
gradient: LinearGradient(begin: Alignment.bottomCenter, stops: [
.2,
.9
], colors: [
Colors.black.withOpacity(.9),
Colors.black.withOpacity(.3),
])),
child: //Expanded(
Padding(
padding: EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Align(
alignment: Alignment.topRight,
child: Icon(
Icons.favorite,
color: Colors.white,
),
),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
"\Tsh. $price",
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold),
),
SizedBox(
height: 10,
),
Text(
title,
style: TextStyle(color: Colors.white, fontSize: 20),
)
],
)
],
),
),
),
),
),
);
}
}
the isActive is not a variable and when you change it in setState nothing happens. try using a int? index variable for saving your selected category index

Flutter How to get value from a custom widget

I have a container that color can change using a color list. I want to get the selected color of the container from the main page. When the container clicked, a list of colors appears and we can select the color for the container. I want to do is to get the color value from that container when I press a button
Widget build(BuildContext context) {
return Container(
height: 80,
width: 40,
padding: EdgeInsets.all(5),
child: InkWell(
onTap: () {
showDialog(
context: context,
child: Dialog(
backgroundColor: Colors.white,
// insetPadding: EdgeInsets.all(100),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20)),
child: Column(
children: [
Container(
color: Colors.red,
height: 50,
alignment: Alignment(0, 0),
child: Text(
'Select the Color',
style: TextStyle(
fontWeight: FontWeight.bold, fontSize: 20),
),
),
ListView.builder(
shrinkWrap: true,
itemCount: colors.length,
itemBuilder: (context, index) {
return GestureDetector(
child: Container(
decoration: BoxDecoration(
//border: Border.all(),
),
padding: EdgeInsets.all(5),
child: Row(
children: <Widget>[
SizedBox(
width: 20,
),
Container(
decoration: BoxDecoration(
boxShadow: <BoxShadow>[
BoxShadow(blurRadius: 10)
],
border: Border.all(),
borderRadius:
BorderRadius.circular(100),
color: color[index]),
padding: EdgeInsets.all(5),
//color: color[index],
height: 45,
width: 45,
),
Padding(
padding: EdgeInsets.all(10),
),
Text(
colors[index],
style: TextStyle(
fontFamily:
GoogleFonts.inter().fontFamily,
color: color[index],
fontSize: 20.0,
shadows: [
Shadow(
// bottomLeft
offset: Offset(0, 1.5),
blurRadius: 5,
color: Colors.black),
],
),
),
],
),
),
onTap: () {
Navigator.pop(context);
setState(() {
selectedColor = color[index];
print(index);
});
},
);
})
],
)),
);
},
child: Container(
padding: EdgeInsets.all(10),
width: 20,
height: 60,
decoration: BoxDecoration(
color: selectedColor,
borderRadius: BorderRadius.circular(10),
),
),
),
);
}
You can pass the value to parent when you call Navigator.pop().
https://flutter.dev/docs/cookbook/navigation/returning-data
onTap: () {
Navigator.pop(context, color[index]);
setState(() {
selectedColor = color[index];
print(index);
});
},
In this case, you need to wait for result after call Navigator push method.
final result = await Navigator.push(
...

The getter 'length' was called on null. Receiver null Tried calling 'length' list.bilder

this is the first page
return StreamProvider<List<User>>.value(
value: DatabaseServ().usersData,
child: Profiledata(),
);
this is the second page
final users = Provider.of<List<User>>(context);
return Scaffold(
appBar: AppBar(),
body: ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return ProfileDatau(
user: users[index],
);
},
),
);
this the third page and it's to show the data of user
final AuthService _auth = AuthService();
final User user;
ProfileDatau({this.user});
#override
Widget build(BuildContext context) {
return Card(
child: Column(
children: <Widget>[
// this container for icon of the profile
Container(
// this for the icon to change the image
child: Container(
margin: EdgeInsets.fromLTRB(100, 110, 20, 10),
width: 50,
height: 50,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Colors.grey[500],
),
child: FlatButton(
padding: EdgeInsets.all(0),
onPressed: () {
print('to change image');
},
child: Icon(
Icons.add,
),
),
),
// this is for the image it self
margin: EdgeInsets.fromLTRB(130, 35, 130, 35),
width: 200,
height: 150,
decoration: BoxDecoration(
shape: BoxShape.circle,
image: DecorationImage(
image: AssetImage('images/angel.png'),
fit: BoxFit.fill,
),
),
),
// this container for the filed under the image like name , information etc..
FlatButton(
padding: EdgeInsets.all(0),
child: Container(
height: 50,
width: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.grey[400]),
margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Center(
child: Text(
user.name,
style: TextStyle(),
),
),
),
onPressed: () {
print('pressed');
},
),
Container(
height: 50,
width: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.grey[400]),
margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Center(
child: Text(
'hello there',
style: TextStyle(),
),
),
),
Container(
height: 50,
width: 400,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(20),
color: Colors.grey[400]),
margin: EdgeInsets.fromLTRB(10, 10, 10, 0),
padding: EdgeInsets.fromLTRB(10, 10, 10, 10),
child: Center(
child: Text(
'hello there',
style: TextStyle(),
),
),
),
Container(
child: FlatButton(
onPressed: () async {
await _auth.signOut();
},
child: Icon(Icons.highlight_off),
),
)
],
),
);
}
}
the problem is when i navigate to the first page it show me red page then shows me the data normally
and it gives me this error .
The following NoSuchMethodError was thrown building Profiledata(dirty, dependencies: [_InheritedProviderScope<List>], state: _ProfiledataState#ed00b):
The getter 'length' was called on null.
Receiver: null
Tried calling: length
Sometimes it takes time to fetch data. Change your second page code like below
final users = Provider.of<List<User>>(context);
return Scaffold(
appBar: AppBar(),
body: (users.length > 0) ? ListView.builder(
itemCount: users.length,
itemBuilder: (context, index) {
return ProfileDatau(
user: users[index],
);
},
) : CircularProgressIndicator();
);