Multiple GridViews in child - flutter

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("/"),
),
),
],
),
),
],
)),
),
);
}

Related

Limit button width

Below is a part of the code that displays the buttons below the body.
......
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("One button"),
),
),
SizedBox(
//space between button
width: 5,
),
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("Two button"),
),
),
],
),
)
......
But the problem is that the width of the buttons is obtained for the entire page. And I would like it to be limited to the width of the body BoxConstraints(maxWidth: 800).
In the photo, I marked the desired width of the buttons with red borders.
How can I achieve this.
You wrap full button widget with ConstrainedBox.
ConstrainedBox(
constraints: BoxConstraints(maxWidth: 800),
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("One button"),
),
),
SizedBox(
//space between button
width: 5,
),
Expanded(
child: OutlinedButton(
onPressed: () {},
child: Text("Two button"),
),
),
],
),
),
)
If you like to avoid padding, you can remove it or use ConstrainedBox on Row.
You could add 2 Expanded Widgets and use the flex property. Expanded, flex
Padding(
padding: const EdgeInsets.all(8.0),
child: Row(
children: [
Expanded(
flex: 10,
child: Container(),
),
Expanded(
flex: 40,
child: OutlinedButton(
onPressed: () {},
child: Text("One button"),
),
),
SizedBox(
//space between button
width: 5,
),
Expanded(
flex: 40,
child: OutlinedButton(
onPressed: () {},
child: Text("Two button"),
),
),
Expanded(
flex: 10,
child: Container(),
),
],
),
)

Icons get together in flutter bottom navigation bar with floating button

I'm making a bottom navigation bar using Inkwell, but as much as I tried solutions, I couldn't separate each icon in its respective space, I don't know what is failing me.
I tried with materialbuttom but the result was almost the same, although if there is any other alternative or suggestion I would like to know! Thankyou
I added the code
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
notchMargin: 10,
child: SizedBox(
height: 60,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 0;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.home),
Text("Home"),
//const Padding(padding: EdgeInsets.all(10))
],
),
),
),
),
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 1;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.group),
Text("Grupos"),
//const Padding(padding: EdgeInsets.only(left: 10))
],
),
),
),
),
],
),
Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 2;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.checklist_outlined),
Text("Completadas"),
//const Padding(padding: EdgeInsets.only(right: 10))
],
)),
),
),
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 3;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person),
Text("Perfil")
//const Padding(padding: EdgeInsets.only(left: 10))
],
),
),
),
),
],
),
],
),
),
),
We can think bottomNavigationBar contains 5 widgets, one of the widget is FloatingActionButton. Therefor, we will include another widget as middle item of row's children that will cover FloatingActionButton space.
While items may have different sizes, we also need to align properly. We can wrap every widget with Expanded. ALso you can test with crossAxisAlignment: CrossAxisAlignment.stretch, or using LayoutBuilders constraints width on each item. If you find text overflow, you can wrap Flexible(child: Text(). More about text on overflow
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Expanded(child: Material(...)),
Expanded(child: Material(...)),
Expanded(child: const SizedBox()), // this will handle the fab spacing
Expanded(child: Material(...)),
Expanded(child: Material(...)),
]
try this
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
bottomNavigationBar: BottomAppBar(
shape: const CircularNotchedRectangle(),
notchMargin: 10,
child: SizedBox(
height: 60,
child: Row(
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 0;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.home),
Text("Home"),
//const Padding(padding: EdgeInsets.all(10))
],
),
),
),
),
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 1;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.group),
Text("Grupos"),
//const Padding(padding: EdgeInsets.only(left: 10))
],
),
),
),
),
SizedBox(),//to make space for the floating button
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 2;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.checklist_outlined),
Text("Completadas"),
//const Padding(padding: EdgeInsets.only(right: 10))
],
)),
),
),
Material(
child: Center(
child: InkWell(
focusColor: Colors.transparent,
hoverColor: Colors.transparent,
highlightColor: Colors.transparent,
onTap: () {
setState(() {
currentTab = 3;
});
},
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(Icons.person),
Text("Perfil")
//const Padding(padding: EdgeInsets.only(left: 10))
],
),
),
),
),
],
),
),
),
Result looks like this
Try below code hope its help to you, I have try another way.
You can refer here some packages for
return Scaffold(
body: Center(
child: Text(
'Hello, World!',
style: Theme.of(context).textTheme.headline4,
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {},
child: Icon(Icons.add),
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
bottomNavigationBar: BottomAppBar(
shape: CircularNotchedRectangle(),
notchMargin: 5,
child: Row(
//children inside bottom appbar
mainAxisSize: MainAxisSize.max,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
IconButton(
icon: Icon(
Icons.home,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.group,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.checklist,
),
onPressed: () {},
),
IconButton(
icon: Icon(
Icons.person,
),
onPressed: () {},
),
],
),
),
);
Your Result Screen->

Can't turn back to Homescreen (stacked with a Drawerscreen) with WillPopScope

I'm building a language learning app and on my homepage there are specific topics like colors etc. which you can choose and a learning page (via JSON) opens. My problem is that I can't find a solution on how to get back to the Homescreen. It's a Stack.
here's the code from my HomePage class with the Stack:
static const String id = 'home_page';
#override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
DrawerScreen(),
HomeScreen(),
],
),
);
}
}
I already tried these solutions for my BackButton, but the app either crashes (when using the WillPopScope) or I'm getting a black screen (when using the
Widget build(BuildContext context) {
SystemChrome.setPreferredOrientations(
[DeviceOrientation.portraitDown, DeviceOrientation.portraitUp]);
return WillPopScope(
onWillPop: () {
return showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('Möchtest du das Quiz beenden?'),
actions: <Widget>[
RaisedButton(
child: Text('Ja'),
onPressed: () =>
Navigator.pushNamed(context, DrawerScreen.id),
),
RaisedButton(
child: Text('Nein'),
onPressed: () => Navigator.of(context).pop(false),
),
]));
},
child: Scaffold(
body: Padding(
padding: const EdgeInsets.fromLTRB(8.0, 64.0, 8.0, 8.0),
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
BackButton(),
],
),
Expanded(
flex: 3,
child: Container(
padding: EdgeInsets.all(15.0),
alignment: Alignment.bottomCenter,
child: Stack(
children: [
AnimatedOpacity(
opacity: opacity1,
duration: Duration(seconds: 1),
child: Center(
child: Text(
mydata[0][i.toString()],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 48.0,
fontFamily: "Circular",
fontWeight: FontWeight.bold,
),
),
),
),
AnimatedOpacity(
opacity: opacity2,
duration: Duration(seconds: 2),
child: Center(
child: Text(
mydata[1][i.toString()],
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 48.0,
fontFamily: "Circular",
fontWeight: FontWeight.bold,
),
),
),
),
],
),
),
),
Expanded(
flex: 6,
child: AbsorbPointer(
absorbing: disableAnswer,
child: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
choicebutton1('a'),
choicebutton2('b'),
],
),
),
),
),
],
),
),
),
);
}
IconButton(
onPressed: () {
Navigator.of(context).pop();
},
icon: Icon(
Icons.arrow_back,
size: 30,
color: Colors.black,
),
),
onPressed: () => Navigator.of(context).pop(false)
remove the false

Shaping appbar in flutter

Someone asked this question before with no straight answer:
Is there a way to shape the appbar to look like this photo ?
i dont think you can do it with the AppBar widget, but you can build your own like this:
not my best code but i think this can help you have an idea on how to do it
Widget build(BuildContext context)
return Scaffold(
body: SafeArea(
child: SingleChildScrollView(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
mainAxisAlignment: MainAxisAlignment.start,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
IconButton(
onPressed: () {}, icon: const Icon(Icons.arrow_back)),
Row(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.end,
children: [
IconButton(
onPressed: () {}, icon: const Icon(Icons.search)),
IconButton(
onPressed: () {},
icon: const Icon(Icons.notifications),
),
],
)
],
),
SizedBox(
height: 40,
),
Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(30), color: Colors.green),
height: MediaQuery.of(context).size.height,
)
],
),
)),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
),
);
}`

Change Footer Color in Flutter. (persistentFooterButtons)

I have a persistentFooterButtons option for my Scaffold.
persistentFooterButtons: [
FloatingActionButton(
heroTag: "editself",
child: Icon(Icons.edit),
onPressed: () {
print(InterfaceViewRoute);
Navigator.pushNamed(context, InterfaceViewRoute, arguments: "-1");
}),
FloatingActionButton(
heroTag: "scanneredit",
child: Icon(Icons.qr_code_scanner_rounded),
onPressed: () => _scan(),
),
],
);
But now the whole background is filled there. See image:
Is there a way to make this background transparent so that you can see the list all the way down
Transparent Footer with Floating Action buttons
This is a Code Snippet.
floatingActionButton: Container(
width: MediaQuery.of(context).size.width,
padding: const EdgeInsets.only(
left: 40.0,
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
FloatingActionButton(
tooltip: 'Home',
child: Icon(
Icons.home,
color: Colors.black,
),
onPressed: () {},
backgroundColor: Colors.white.withOpacity(.4),
),
Spacer(),
FloatingActionButton(
tooltip: 'Details',
child: Icon(
Icons.details,
color: Colors.black,
),
onPressed: () {},
backgroundColor: Colors.white.withOpacity(.4),
),
Spacer(),
FloatingActionButton(
tooltip: 'Bar Chart',
child: Icon(
Icons.bar_chart_outlined,
color: Colors.black,
),
onPressed: () {},
backgroundColor: Colors.white.withOpacity(.4),
),
Spacer(),
FloatingActionButton(
tooltip: 'Settings',
child: Icon(
Icons.settings,
color: Colors.black,
),
onPressed: () {},
backgroundColor: Colors.white.withOpacity(.4),
),
],
),
),
Have you tried put Row in floatingActionButton?
floatingActionButton: Row(
mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[
FloatingActionButton(
),
SizedBox(width: 10,),
FloatingActionButton(
),
],
)),
Another way is to use bottomNavigationBar of Scaffold
...
extendBody: true, // if you want body to be extended below buttons
bottomNavigationBar: SafeArea(
child: Container(
child: TextButton(child: Text('button'), onPressed: () {}), //or row of buttons
width: MediaQuery.of(context).size.width,
color: Colors.transparent, //background color
padding: EdgeInsets.only(top: 5, bottom: 3),
),
),
...