Gridview.count Flutter not working and showing yellow and black stripes - flutter

I did a Gridview.count for rendering 6 buttons in 2 columns and 3 rows, it's not rendering and instead of the components, it shows yellow and black stripes. I already set the height and the width, all wrapped in a container.
return Scaffold(
body: Container(
height: 450.0,
width: double.infinity,
child: GridView.count(
shrinkWrap: true,
primary: true,
crossAxisCount: 2,
children: <Widget>[
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Wifi')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
Center(
child: FlatButton(
onPressed: () => {},
padding: EdgeInsets.all(10.0),
child: Column(children: <Widget>[
Image.asset(
'assets/img/wifi.png',
height: 50.0,
width: 50.0,
),
Text('Key/Access')
]),
),
),
],
)));
}
}
Please, someone knows why it's not rendering properly? or have an alternative widget that might work better? Thanks.
This widget is imported in this one:
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Color(0xFF88B4E0),
appBar: AppBar(
leading: IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(Icons.arrow_back_ios),
color: Colors.white,
),
backgroundColor: Colors.transparent,
elevation: 0.0,
title: Text('Help and FAQ',
style: TextStyle(fontSize: 18.0, color: Colors.white)),
centerTitle: true,
actions: <Widget>[
SearchButton(),
],
),
body: ListView(children: [
Stack(children: [
Container(
height: MediaQuery.of(context).size.height - 82.0,
width: MediaQuery.of(context).size.width,
color: Colors.transparent),
Positioned(
top: 75.0,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(45.0),
topRight: Radius.circular(45.0),
),
color: Colors.white),
height: MediaQuery.of(context).size.height - 100.0,
width: MediaQuery.of(context).size.width,
child: Column(
children: [
SizedBox(height: 20.0),
Testing(),
],
),
)),
]),
]),
);
}
void moveBack() {
Navigator.pop(context);
}
}

Related

TextField hidden by keyboard in chat screen

I am working on a chat screen. This is my current code for the starting UI:
class ChatOtroUsuario extends StatefulWidget {
const ChatOtroUsuario({Key key, this.usuario, this.otroUsuario}) : super(key: key);
final Usuario usuario;
final OtroUsuario otroUsuario;
#override
_ChatOtroUsuarioState createState() => _ChatOtroUsuarioState();
}
class _ChatOtroUsuarioState extends State<ChatOtroUsuario> {
#override
void initState() {
// TODO: implement initState
super.initState();
}
#override
Widget build(BuildContext context) {
print("usuario "+widget.usuario.username);
print("otor usuario "+widget.otroUsuario.username);
final bottom = MediaQuery.of(context).viewInsets.bottom;
return Scaffold(
resizeToAvoidBottomInset: true,
resizeToAvoidBottomPadding: false,
body: SingleChildScrollView(
reverse: true,
child: Padding(
padding: EdgeInsets.only(bottom: bottom),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 60.0,
color: AppColors.blancoMovMap,
child: Row(
children: [
GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.rojoMovMap,
),
child: Icon(
Icons.arrow_back_rounded,
color: Colors.white,
),
alignment: Alignment.center,
),
),
),
GestureDetector(
onTap: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/muro', (Route<dynamic> route) => false);
},
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.rojoMovMap,
),
child: Icon(
Icons.home,
color: Colors.white,
),
alignment: Alignment.center,
),
),
SizedBox(
width: 5,
),
CircleAvatar(
radius: 25,
backgroundImage: NetworkImage(
widget.otroUsuario.profile_image),
),
SizedBox(
width: 5,
),
Text("${widget.otroUsuario.username}",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),),
],
),
),
Container(
color: Colors.amber,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height -110 ,
child: Text("hola"),
),
Container(
color: Colors.green,
height: 50,
child: TextField(
decoration: InputDecoration(
hintText: "Escribe tu mensaje...",
hintStyle: TextStyle(color: Colors.black,fontSize: 16)
),
),
)
],
),
),
),
);
}
}
The issue is that as it is now, when entering the textfield to write a message, the keyboard appears hidding the textfield.
As you may see, I have tried inserting all known solutions: wrapping the Column with a SingleChildScrollView widget, adding
resizeToAvoidBottomInset: true,
resizeToAvoidBottomPadding: false,
but keyboard is hidding the textfield, the view doesn't scroll together with the keyboard.
Try these
Remove both resizeToAvoidBottomInset: true, and resizeToAvoidBottomPadding: false,
Remove these lines padding: EdgeInsets.only(bottom: bottom), and change the Padding widget to a Container widget
`
print("usuario "+widget.usuario.username);
print("otor usuario "+widget.otroUsuario.username);
final bottom = MediaQuery.of(context).viewInsets.bottom;
return Scaffold(
resizeToAvoidBottomInset: true,
resizeToAvoidBottomPadding: false,
body: SingleChildScrollView(
reverse: true,
child: Padding(
padding: EdgeInsets.only(bottom: bottom),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
width: MediaQuery.of(context).size.width,
height: 60.0,
color: AppColors.blancoMovMap,
child: Row(
children: [
GestureDetector(
onTap: () {
Navigator.of(context).pop();
},
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.rojoMovMap,
),
child: Icon(
Icons.arrow_back_rounded,
color: Colors.white,
),
alignment: Alignment.center,
),
),
),
GestureDetector(
onTap: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/muro', (Route<dynamic> route) => false);
},
child: Container(
height: 30,
width: 30,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: AppColors.rojoMovMap,
),
child: Icon(
Icons.home,
color: Colors.white,
),
alignment: Alignment.center,
),
),
SizedBox(
width: 5,
),
CircleAvatar(
radius: 25,
backgroundImage: NetworkImage(
widget.otroUsuario.profile_image),
),
SizedBox(
width: 5,
),
Text("${widget.otroUsuario.username}",style: TextStyle(fontSize: 16,fontWeight: FontWeight.bold),),
],
),
),
Container(
color: Colors.amber,
width: MediaQuery.of(context).size.width,
height: MediaQuery.of(context).size.height -110 ,
child: Text("hola"),
),
Container(
color: Colors.green,
height: 50,
child: TextField(
decoration: InputDecoration(
hintText: "Escribe tu mensaje...",
hintStyle: TextStyle(color: Colors.black,fontSize: 16)
),
),
)
],
),
),
),
);
Please try to wrap with SingleChildScrollView, or give bottom insets padding.
SingleChildScrollView(
child: Column(
children: <Widget>[
TextField(),
TextField(),
TextField(),
],),
),
OR
MediaQuery.of(context).viewInsets.bottom

Flutter set max width for AppBar

I want to set the max width of an AppBar like this:
PreferredSize(
preferredSize: Size(500, 200),
child: AppBar(
flexibleSpace: _AppHeader(),
),
)
But this only sets the height. How is is possible to set the width?
Looks like this is an open issue (https://github.com/flutter/flutter/issues/54784).
As suggested there by #imeDevelopers you can use Stack to "hack" an AppBar with max width:
If you use Stack, you also get to decide if the AppBar will scroll with the content or not.
Use this code to keep the AppBar in place even when scrolling (default AppBar behaviour):
const double appBarWidth = 980;
Scaffold(
body: Stack(
children: [
ListView(
shrinkWrap: true,
children: [
Padding(
padding: const EdgeInsets.only(top: 56),
child: Column(
children: [
...[Colors.green, Colors.indigo, Colors.amber].map(
(c) => Container(
color: Colors.red,
child: Center(
child: Container(
height: 1000,
width: appBarWidth,
color: c,
),
),
),
)
],
),
),
],
),
Container(
width: double.infinity,
height: 56,
color: Colors.lime,
),
Positioned(
left: (MediaQuery.of(context).size.width - appBarWidth) / 2,
child: Container(
width: appBarWidth,
child: AppBar(
elevation: 0,
title: const Text('TITLE'),
actions: [
...[1, 2, 3].map(
(i) => TextButton(
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
onPressed: () {},
child: Text('ACTION $i'),
),
),
],
),
),
)
],
),
);
Use this code if you want the AppBar to scroll with the content:
const double appBarWidth = 980;
Scaffold(
body: ListView(
shrinkWrap: true,
children: [
Stack(
children: [
Container(
width: double.infinity,
height: 56,
color: Colors.lime,
),
Positioned(
left: (MediaQuery.of(context).size.width - appBarWidth) / 2,
child: Container(
width: appBarWidth,
child: AppBar(
elevation: 0,
title: const Text('TITLE'),
actions: [
...[1, 2, 3].map(
(i) => TextButton(
style: TextButton.styleFrom(
primary: Theme.of(context).colorScheme.onPrimary),
onPressed: () {},
child: Text('ACTION $i'),
),
),
],
),
),
)
],
),
Column(
children: [
...[Colors.green, Colors.indigo, Colors.amber].map(
(c) => Container(
color: Colors.red,
child: Center(
child: Container(
height: 1000,
width: appBarWidth,
color: c,
),
),
),
)
],
),
],
),
);

What is the best flutter widget to use to achieve this kinda layout?

This is the UI I want to implement, I only started playing with flutter last week so I am not as fluent. I was wondering what is the best way to achieve this layout.
This is what I get with a stack and column:
This is the code:
Column(
children: <Widget>[
Expanded(
child: Container(
constraints: BoxConstraints.expand(height: 150),
color: Colors.blue,
child: Stack(
overflow: Overflow.visible,
children: <Widget>[
Positioned(
bottom: -40,
left: MediaQuery.of(context).size.width * 0.4,
// alignment: Alignment.bottomCenter,
child: Container(
constraints: BoxConstraints.expand(height: 80, width: 80),
color: Colors.green,
),
),
],
),
),
flex: 3,
),
Expanded(
child: Container(
margin: EdgeInsets.all(15.0),
color: Colors.red,
),
flex: 7,
)
],
);
I made something like the image you provided with dummy image and some icon you can change icons if you want,use media query size instead of fixed padding tops
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: SingleChildScrollView(
child: Column(
children: [
Stack(children: [
Container(
child: FittedBox(
fit: BoxFit.fill,
child: Image.network('https://picsum.photos/300?image=10')),
height: 150,
width: double.infinity,
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: [
Padding(
padding: const EdgeInsets.only(top: 75),
child: MaterialButton(
onPressed: () {},
elevation: 2.0,
color: Colors.red,
child: Icon(
Icons.message,
size: 27.0,
),
padding: EdgeInsets.all(20),
shape: CircleBorder(),
),
),
Padding(
padding: const EdgeInsets.only(top: 75),
child: Column(children: [
CircleAvatar(
radius: 55.0,
backgroundImage:
NetworkImage('https://i.pravatar.cc/150?img=54'),
backgroundColor: Colors.transparent,
),
Text("David Beckham"),
Text("Model/SuperStar")
]),
),
Padding(
padding: const EdgeInsets.only(top: 75),
child: MaterialButton(
onPressed: () {},
elevation: 2.0,
color: Colors.blue,
child: Icon(
Icons.add,
size: 27.0,
),
padding: EdgeInsets.all(20),
shape: CircleBorder(),
),
),
],
),
]),
Container(height: 100, color: Colors.red),
Container(height: 100, color: Colors.green),
Container(height: 100, color: Colors.yellow),
Container(height: 100, color: Colors.blue),
],
)));
}
}

MediaQuery.of() called with a context that does not contain a MediaQuery even when inside MaterialApp

Im constantly getting this error while trying to expand the Container to screen's full available width and height. What stresses me out and confuses me is that it is inside Scaffold which is inside MaterialApp but still it throws this stupid error what do I do now? Please help.
Why did they made it so difficult to just get the screen size or just expand the container's width 100%? Im trying to fix this since hours but there is no solution at all.
This is the error:
Here is the code:
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
backgroundColor: Colors.grey[800],
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text('asdfasdfsfd'),
),
Expanded(
child: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
child: Image.asset('images/cover.jpg')),
),
SizedBox(
height: 5,
),
//
Container(
child: Center(child: Text('Folder Name')),
),
SizedBox(
height: 5,
),
Container(
child: Slider.adaptive(
value: 0,
onChanged: null,
min: 0,
max: 5,
label: '5',
),
),
//
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Container(
width: 60,
height: 25,
child: Center(child: Text('08:16')),
),
Container(
width: 60,
height: 25,
child: Center(child: Text('-06:44'))),
],
),
),
SizedBox(
height: 15,
),
// Bottom Third container
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
],
),
),
SizedBox(
height: 15,
),
// Bottom Second container start
Container(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 60,
height: 60,
// Previous Button
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
// Play Button
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
// Next Button
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
],
),
),
SizedBox(
height: 20,
),
// Bottom container start
Container(
// 3rd Container
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
Container(
width: 60,
height: 60,
child: FlatButton(
shape: CircleBorder(),
onPressed: () {},
padding: EdgeInsets.all(0.0),
child: Image.asset('images/playbutton.png'),
),
),
],
),
),
SizedBox(
height: 10,
)
],
)),
),
);
}
}
While technically MaterialApp is above your query in the tree, the BuildContext context that it currently accesses is above it.
You need to obtain BuildContext that comes below the MaterialApp. There are multiple ways this can be done.
You can move MaterialApp out of the MyApp widget:
import 'package:flutter/material.dart';
void main() {
runApp(MaterialApp(home: MyApp()));//`MaterialApp` moved here
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {//Now this context is below the `MaterialApp`
return Scaffold(
backgroundColor: Colors.grey[800],
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text('asdfasdfsfd'),
),
Expanded(
child: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
child: Image.asset('images/cover.jpg')),
),
...
Use a Builder to obtain a different BuildContext
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Builder(//Use `Builder` below `MaterialApp` but above where you need access to proper context
builder: (context) {//Different context provided here
return Scaffold(
backgroundColor: Colors.grey[800],
body: SafeArea(
child: Column(
children: <Widget>[
Container(
child: Text('asdfasdfsfd'),
),
Expanded(
child: Container(
color: Colors.white,
width: MediaQuery.of(context).size.width,
child: Image.asset('images/cover.jpg')),
),

How to show a Container fixed at bottom of screen without using bottom navigation bar in Flutter?

In my flutter project, I have a container with some icons and text below them. I want this entire container to be fixed at bottom of the screen without using bottom navigation bar like the below image-
So, I need an entire example to fix that container at bottom and keep my other components inside body with a scroll view.
Ok,
Here you go....
return Scaffold(
appBar: AppBar(
title: Text("Header"),
),
body: Column(
children: <Widget>[
Expanded(
child: Container(
alignment: Alignment.center,
child: Text("Hello"),
),
),
Container(
height: 50,
width: double.maxFinite,
decoration: BoxDecoration(
color: Colors.deepOrange,
borderRadius: BorderRadius.vertical(top: Radius.circular(20.0))
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(icon: Icon(Icons.arrow_forward), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_downward), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_left), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_upward), onPressed: (){},),
],
),
)
],
),
);
If you want to make a scrollable item/listview in the body section and want a fixed bottom container you can follow this code given below :
important note : Make sure wrap the listview with Expanded
import 'package:flutter/material.dart';
class ShoppingCart extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey[200],
appBar: AppBar(
backgroundColor: Colors.white,
leading: IconButton(
icon: Icon(Icons.arrow_back, color: Colors.black),
),
actions: <Widget>[
GestureDetector(
onTap: () {
//your code
},
child: Padding(
padding: const EdgeInsets.only(right: 15.0),
child: Icon(
Icons.delete_outline_sharp,
color: Colors.black,
size: 30,
),
)),
//Add more icon here
],
elevation: 0,
centerTitle: false,
title:
Text("Shopping Cart", style: TextStyle(color: Colors.black))),
body: Column(
children: [
Expanded(
child: ListView.builder(
itemCount: shoppingCartItems.length,
itemBuilder: (context, index) {
return Padding(
padding: const EdgeInsets.only(
top: 15.0, left: 12.0, right: 12.0),
child: Container(
decoration: BoxDecoration(
// color: Color(0xffF3F3F3),
color: Colors.red,
shape: BoxShape.rectangle,
borderRadius:
BorderRadius.all(Radius.circular(10.0))),
height: 120,
width: 360,
),
);
},
),
),
Container(
height: 150,
width: double.maxFinite,
decoration: BoxDecoration(
color: Colors.deepOrange[200],
borderRadius:
BorderRadius.vertical(top: Radius.circular(20.0))),
)
],
));
}
}
Update :
This is built in now. You can use bottomNavigationBar and customize is as below :
bottomNavigationBar: BottomAppBar(
child: Container(
height: //set your height here
width: double.maxFinite, //set your width here
decoration: BoxDecoration(
color: Colors.orange,
borderRadius: BorderRadius.vertical(top: Radius.circular(20.0))
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(icon: Icon(Icons.arrow_forward), onPressed: (){
//on Presses functionaluty goes here
},),
//add as many tabs as you want here
],
),
),
),
bottomNavigationBar: BottomAppBar(
child: Container(
height: 50.0,
width: double.maxFinite,
decoration: BoxDecoration(
color: Colors.deepOrange,
borderRadius: BorderRadius.vertical(top: Radius.circular(20.0))
),
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
IconButton(icon: Icon(Icons.arrow_forward), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_downward), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_left), onPressed: (){},),
IconButton(icon: Icon(Icons.arrow_upward), onPressed: (){},),
],
),
),
),