Related
I have a Widget to build Cards using the data stored in the Firestore i want to convert this Widget into a StatelessWidget to use the buttons of the Card.
This is the Widget
Widget buildProducts(Products products) => Card(
clipBehavior: Clip.antiAlias,
child: SizedBox(
width: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Image(image: NetworkImage(products.imageUrl)),
ListTile(
title: Text(products.name),
subtitle: Text('Precio: ${products.price} \$'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 8, 8),
child: ElevatedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(
const Color(0xFFFFFFFF),
),
backgroundColor: MaterialStateProperty.all<Color>(
const Color(0xFF6750A4),
),
),
onPressed: () => showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
title: const Text('Subir Producto'),
content: const Text(
'El producto ha añadido a la base de datos correctamente'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'))
],
),
),
child: const Text(
'Añadir al Carro',
),
),
),
],
),
],
),
),
);
Create a StatelessWidget and pass the Products through constructor and paste the card on return section.
class ProductWidet extends StatelessWidget {
final Products products;
const ProductWidet({required this.products,Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return Card(
clipBehavior: Clip.antiAlias,
child: SizedBox(
width: 300,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Image(image: NetworkImage(products.imageUrl)),
ListTile(
title: Text(products.name),
subtitle: Text('Precio: ${products.price} \$'),
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Padding(
padding: const EdgeInsets.fromLTRB(0, 0, 8, 8),
child: ElevatedButton(
style: ButtonStyle(
foregroundColor: MaterialStateProperty.all<Color>(
const Color(0xFFFFFFFF),
),
backgroundColor: MaterialStateProperty.all<Color>(
const Color(0xFF6750A4),
),
),
onPressed: () => showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) => AlertDialog(
title: const Text('Subir Producto'),
content: const Text(
'El producto ha añadido a la base de datos correctamente'),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.pop(context);
},
child: const Text('OK'))
],
),
),
child: const Text(
'Añadir al Carro',
),
),
),
],
),
],
),
),
);
}
}
I can't figure out how to do it like this. Everything I try messes it up
You will need to set mainAxisAlignment and CrossAxisAlignment arguments
You can also use GridView.count like code below. this way has less repeated code and more
flexible:
class Test1 extends StatelessWidget {
List<ButtonWidget> list = [
ButtonWidget(
text: 'test',
onPressed: (){//do something
},
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
ButtonWidget(
text: 'test',
),
];
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: GridView.count(
padding: EdgeInsets.all(8),
shrinkWrap: true,
scrollDirection: Axis.vertical,
crossAxisCount: 3,
physics: ScrollPhysics(),
children: List.generate(list.length, (index) => list[index]),
),
));
}
}
class ButtonWidget extends StatelessWidget {
final String text;
final Function onPressed;
ButtonWidget({#required this.text, this.onPressed});
#override
Widget build(BuildContext context) {
return Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: onPressed,
child: Text(text),
color: Colors.lightBlue,
),
],
);
}
}
Try this,
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
],
),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
RaisedButton(
onPressed: () {},
child: Text("data"),
),
],
),
],
),
I am new to flutter. I try to get two grid-views in one child. How can I achieve this with flutter.
Which widget can I use for this. I tried to assign both grid-views to one column-widget and assign this to the child, but without success.
Can I make multiple children and assign them to one body ?
Thank you for your help
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Padding(padding: const EdgeInsets.only(top: 300.0),
child: Column(,
GridView.count(
padding: const EdgeInsets.all(18.9),
crossAxisSpacing: 10,
mainAxisSpacing: 55,
crossAxisCount: 4,
children: <Widget>[
FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_1_button();
},
child: Text("1"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_2_button();
},
child: Text("2"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_3_button();
},
child: Text("3"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_4_button();
},
child: Text("+"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_5_button();
},
child: Text("4"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_6_button();
},
child: Text("5"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_7_button();
},
child: Text("6"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_8_button();
},
child: Text("-"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_9_button();
},
child: Text("7"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_8_button();
},
child: Text("8"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_9_button();
},
child: Text("9"),)
, FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_8_button();
},
child: Text("*"),),
],)
,GridView.count(
padding: const EdgeInsets.all(18.9),
crossAxisSpacing: 10,
mainAxisSpacing: 55,
crossAxisCount: 2,
children: <Widget>[
FloatingActionButton.extended(
elevation: 0.2,
onPressed: () {
press_9_button();
},
label: Text("="), isExtended: true,)
,FloatingActionButton(
elevation: 0.2,
onPressed: () {
press_9_button();
},
child: Text("/"),), ],),)
),
),);
}
}
you have to give Expanded to your every gridView otherwise it throws hasSize error.
you have forgot to give children to column widget
new Column(
children: <Widget>[
new Expanded(
child: GridView.count(
......
),
),
new Expanded(
child: GridView.count(
......
),
),
],
),
Yes, you are in right way, but you forgot the "children" in column widget.
For example:
Column(
children: <Widget>[
Text('Deliver features faster'),
Text('Craft beautiful UIs'),
Expanded(
child: FittedBox(
fit: BoxFit.contain, // otherwise the logo will be tiny
child: const FlutterLogo(),
),
),
],
)
So, here:
child: Column(,
GridView.count(
You have to use:
child: Column(children: [
GridView.count( ... ,
GridView.count( ... ,
])
In your case, column and rows is a better solution:
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
body: Padding(
padding: const EdgeInsets.only(top: 300.0),
child: Column(
children: <Widget>[
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("1"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("2"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("3"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("+"),
),
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("4"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("5"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("6"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("-"),
),
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("7"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("8"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("9"),
),
),
Expanded(
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("*"),
),
),
],
),
),
Expanded(
child: Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 3,
child: FloatingActionButton.extended(
elevation: 0.2,
onPressed: () {},
label: Text("="),
isExtended: true,
),
),
Expanded(
flex: 1,
child: FloatingActionButton(
elevation: 0.2,
onPressed: () {},
child: Text("/"),
),
),
],
),
),
],
)),
),
);
}
I have a drawer in my Flutter app, but the default drawer has a very small drag area to show it. How can I increase it?
#override
Widget build(BuildContext context) {
Size size = MediaQuery.of(context).size;
return Scaffold(
drawerDragStartBehavior: DragStartBehavior.down,
drawer: Drawer(
child: Column(
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text(
fullName ?? "",
style: TextStyle(
color: Colors.white,
letterSpacing: 1.0,
fontSize: 16.0,
),
),
You can simply use the drawerEdgeDragWidth property of the Scaffold:
return Scaffold(
appBar: MyAppBar(),
drawer: MyDrawer(),
// Put the width you want here
// In this case the drag area fills the whole screen
drawerEdgeDragWidth: MediaQuery.of(context).size.width,
body: MyBody(),
);
drawer: SizedBox(
width: MediaQuery.of(context).size.width/1.2,
child: Drawer(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
SizedBox(
height: 10,
),
ListTile(
dense: true,
title: Text(
"Apply for Leave",
style: drawerFont,
),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ApplyLeave()));
},
),
ListTile(
dense: true,
title: Text(
"Approval for Bills",
style: drawerFont,
),
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ApplyBill()));
},
)
],
),
Column(
crossAxisAlignment: CrossAxisAlignment.end,
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
Container(
color: Colors.grey[200],
width: double.infinity,
height: 60,
child: Padding(
padding: const EdgeInsets.only(
left: 28.0, top: 15, bottom: 15),
child: GestureDetector(
onTap: () {
Navigator.pop(context);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => ContactUs()));
},
child: Text(
"Contact Us",
style: drawerFont,
),
),
),
),
],
)
],
),
),
),
In my Flutter project, in one page I have some rows including card align vertically. Now, I want this screen to make scroll-able.
I have tried replacing the column to Listview but it didn't work. I also tried to wrap it with SingleChildScrollview but didn't work. It shows like below image-
Here's the code -
HomeFragment.dart
class HomeFragment extends StatefulWidget {
#override
_HomeFragmentState createState() => new _HomeFragmentState();
}
String jwt;
class _HomeFragmentState extends State<HomeFragment> {
List<LastEngagement> _lastEngagements = List<LastEngagement>();
#override
void initState() {
super.initState();
_getLastEngagement;
_getLastEngagement2();
}
void _getLastEngagement() {
Webservice().load(LastEngagement.allLastEngagement).then((newsArticles) => {
setState(() => {
_lastEngagements = newsArticles
})
});
}
void _getLastEngagement2() {
Webservice().load(LastEngagement.allLastEngagement).then((newsArticles) => {
setState(() => {
_lastEngagements = newsArticles
})
});
}
Card showCard(int index) {
return new Card(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
CircleAvatar(
backgroundImage: NetworkImage(
"https://randomuser.me/api/portraits/men/1.jpg"
),
),
SizedBox(
width: 10.0,
),
Expanded(child: Text(_lastEngagements[index].title)),
],
),
),
);
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News'),
),
body:Column(
children: <Widget>[
Row(
children: <Widget>[
Expanded(
child: Container(
color: Colors.blue,
child: SizedBox(
height: 100.0,
child: new ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: _lastEngagements.length,
itemBuilder: (BuildContext ctxt, int index) {
return Container(
width: 200.0,
child: showCard(index),
);
},
),
),
),
),
],
mainAxisAlignment: MainAxisAlignment.spaceBetween,
),
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
],
),
Row(
children: <Widget>[
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Card(
child: InkWell(
splashColor: Colors.blue.withAlpha(30),
onTap: () {
print('Card tapped.');
},
child: Container(
height: 100,
child: Text('A card that can be tapped'),
),
),
),
),
),
],
)
],
)
);
}
}
Replacing the Column by Listview causes following error-
So, I need a permanent solution to make my screen scroll-able, doesn't matter whatever widgets I use.
You can use SingleChildScrollView or change the column widget to ListView
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News'),
),
body: SingleChildScrollView(
child: Column(
children: <Widget>[
],
),
));
}
Or
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News'),
),
body: ListView(
children: <Widget>[],
));
}
Well the previous answers do contain the solution .. at least that's what I think .. but they are buggy answers and that's why it's not working .. try this
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("News"),
),
body: Column(
children: <Widget>[
Expanded(
child: SingleChildScrollView(),
)
],
),
);
}
The expanded gives SingleChildScrollView() full height of the screen allowing it to work :)
I think it should work . Good luck
SingleChildScrollView(
scrollDirection: Axis.vertical,)
Try this instead.
Try below code:
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('News'),
),
body: Column(
children: <Widget>[
Expanded
(
child: SingleChildScrollView()
),
flex: 1,
]
)
);
}