Flutter: How to create a beautiful Curve oval shape Container / Divider between other widgets in Flutter - flutter

How to create this beautiful curve shape divider in a flutter App.

Simply use this customDivider as your divider
customDivider(String title) {
return Row(
children: [
Expanded(
child: Container(
color: Colors.white,
height: 10,
),
),
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(13),
color: Colors.white,
),
child: Center(child: Text(title)),
),
Expanded(
child: Container(
color: Colors.white,
height: 10,
),
),
],
);
}
Here is an example
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.grey,
appBar: AppBar(
title: Text(widget.title),
),
body: ListView.builder(
itemCount: 5,
shrinkWrap: true,
itemBuilder: (context, index) => listItem(index),
),
);
}
customDivider(String title) {
return Row(
children: [
Expanded(
child: Container(
color: Colors.white,
height: 10,
),
),
Container(
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(13),
color: Colors.white,
),
child: Center(child: Text(title)),
),
Expanded(
child: Container(
color: Colors.white,
height: 10,
),
),
],
);
}
listItem(int index) {
return Column(
children: [
Container(
height: 200,
width: 200,
margin: EdgeInsets.all(10),
color: index.isEven ? Colors.orange : Colors.deepPurpleAccent,
),
customDivider("your title"),
],
);
}
}
OUTPUT:

There can be many ways to do this in flutter. This is one of the simplest approach.
main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: SO(),
),
);
}
}
class SO extends StatelessWidget {
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.pink.shade100,
appBar: AppBar(),
body: Center(
child: CapsuleWidget(
label: 'organizing data'.toUpperCase(),
ribbonHeight: 8,
),
),
);
}
}
class CapsuleWidget extends StatelessWidget {
final Color fillColor;
final Color textColor;
final String label;
final double ribbonHeight;
final double ribbonRadius;
const CapsuleWidget({
Key key,
this.fillColor = Colors.white,
this.textColor = Colors.black,
#required this.label,
#required this.ribbonHeight,
this.ribbonRadius = 1000,
}) : super(key: key);
#override
Widget build(BuildContext context) {
return Row(
children: <Widget>[
Expanded(
child: Container(
height: ribbonHeight,
color: fillColor,
),
),
Container(
decoration: BoxDecoration(color: fillColor, borderRadius: BorderRadius.circular(ribbonRadius)),
child: Padding(
padding: const EdgeInsets.all(12.0),
child: Text(
label,
style: TextStyle(color: textColor, fontWeight: FontWeight.w500),
),
),
),
Expanded(
child: Container(
height: ribbonHeight,
color: fillColor,
),
),
],
);
}
}

it use Stack class
link url : https://api.flutter.dev/flutter/widgets/Stack-class.html
pseudo code
Stack {
Container(), // background
Contanier(), // white Line
Text() , // center Text
}

Related

Message on any screen

I want to show the snack bar or Dialog on any screen in the Flutter app, anyone knows the way for that ?
For example.. let's say, I receive a notification when the user is in-app, on any screen in-app. How can I display the snack bar message in that situation regardless of which screen the user is currently on?
You can do something like that :
pip_flutter: ^0.0.3
Example:
import 'package:flutter/material.dart';
import 'package:pip_flutter/pipflutter_player.dart';
import 'package:pip_flutter/pipflutter_player_configuration.dart';
import 'package:pip_flutter/pipflutter_player_controller.dart';
import 'package:pip_flutter/pipflutter_player_data_source.dart';
import 'package:pip_flutter/pipflutter_player_data_source_type.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.pink,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Picture in Picture Mode'),
),
body: Center(
child: InkWell(
onTap: () {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => PictureInPicturePage()));
},
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Center(
child: Container(
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Text(
'Picture in Picture Mode',
style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold,fontSize: 16),
),
),
),
),
),
),
);
}
}
class PictureInPicturePage extends StatefulWidget {
#override
_PictureInPicturePageState createState() => _PictureInPicturePageState();
}
class _PictureInPicturePageState extends State<PictureInPicturePage> {
late PipFlutterPlayerController pipFlutterPlayerController;
final GlobalKey pipFlutterPlayerKey = GlobalKey();
#override
void initState() {
PipFlutterPlayerConfiguration pipFlutterPlayerConfiguration =
const PipFlutterPlayerConfiguration(
aspectRatio: 16 / 9,
fit: BoxFit.contain,
);
PipFlutterPlayerDataSource dataSource = PipFlutterPlayerDataSource(
PipFlutterPlayerDataSourceType.network,
'http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4',
);
pipFlutterPlayerController =
PipFlutterPlayerController(pipFlutterPlayerConfiguration);
pipFlutterPlayerController.setupDataSource(dataSource);
pipFlutterPlayerController
.setPipFlutterPlayerGlobalKey(pipFlutterPlayerKey);
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Picture in Picture player"),
leading: IconButton(onPressed: (){
Navigator.of(context).pop();
}, icon: const Icon(Icons.arrow_back_ios,color: Colors.white,)),
),
body: Column(
children: [
const SizedBox(height: 20),
Flexible(
flex: 1,
fit: FlexFit.loose,
child: AspectRatio(
aspectRatio: 16 / 9,
child: PipFlutterPlayer(
controller: pipFlutterPlayerController,
key: pipFlutterPlayerKey,
),
),
),
Container(
margin: const EdgeInsets.only(top: 20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: const Center(child: Text("Show PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () {
pipFlutterPlayerController
.enablePictureInPicture(pipFlutterPlayerKey);
},
),
InkWell(
child: Container(
width: MediaQuery.of(context).size.width * 0.4,
padding: const EdgeInsets.all(8.0),
margin: const EdgeInsets.all(8.0),
decoration: BoxDecoration(color: Colors.pink,borderRadius: BorderRadius.circular(12.0)),
child: Center(child: const Text("Disable PiP",style: TextStyle(color: Colors.white,fontWeight: FontWeight.bold),))),
onTap: () async {
pipFlutterPlayerController.disablePictureInPicture();
},
),
],
),
),
],
),
);
}
}

flutter: image overlap card

i'm new to flutter and i wanted to create simple design for menu app as shown in image below ... i tried below code but it didn't give same design, is there any way to achieve it?
enter image description here
import 'package:flutter/material.dart';
main() => runApp(test());
class test extends StatefulWidget {
#override
_testState createState() => _testState();
}
class _testState extends State<test> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: Text("Card over stack"),
),
body: Stack(
children: <Widget>[
Align(
alignment: Alignment.topCenter,
child: Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(10.0)),
color: Colors.lightBlueAccent),
height: 100,
),
),
Positioned(
top: 60,
right: 10,
left: 10,
child: Card(
child: ListTile(
leading: SizedBox(
height: 150.0,
width: 150.0, // fixed width and height
child: Image.asset("assets/images/test.png"))),
),
),
],
),
),
);
}
}
Stack is the rignt choice, check screenshot below:
Full working code:
import 'package:flutter/material.dart';
main() => runApp(const DemoApp());
class DemoApp extends StatefulWidget {
const DemoApp({Key? key}) : super(key: key);
#override
State<StatefulWidget> createState() {
return _DemoState();
}
}
class _DemoState extends State<DemoApp> {
#override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
alignment: Alignment.bottomCenter,
children: [
Container(
color: Colors.white,
),
Positioned(
bottom: 150,
// replace with your Card here
child: Card(
child: Container(
width: 250,
height: 300,
color: Colors.blue,
),
),
),
Positioned(
bottom: 320,
// replace with your image here
child: Container(
width: 200,
height: 280,
color: Colors.pink,
),
),
],
),
),
);
}
}
Here is a solution for this. You can also refer to the following link to learn more.
https://www.flutterbeads.com/flutter-position-widget-in-stack/
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
#override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
#override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
height: MediaQuery.of(context).size.height,
decoration: const BoxDecoration(
color: Colors.redAccent,
//add your gradiant here
),
child: SafeArea(
child: Stack(
alignment: AlignmentDirectional.topCenter,
children: [
Positioned(
top: 180,
child: Container(
width: MediaQuery.of(context).size.width * 0.9,
height: 500,
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(7),
),
),
),
Positioned(
top: 20,
child: Container(
width: MediaQuery.of(context).size.width * 0.7,
height: 200,
decoration: BoxDecoration(
color: Colors.blueAccent,
borderRadius: BorderRadius.circular(7),
),
//add your image here
//child: Center(child: Image.asset(name)),
),
),
],
),
),
),
);
}
}

I am trying to change the color of a container onTap but for some reason it isnt working

This is the code that I used. I am trying to implement a UI where a quiztaker can see highlighted the option that they selected.
I am new to this please tell me where I went wrong?
#override
Widget build(BuildContext context) {
int selectedOption = 0;
return Scaffold(
key: scaffoldKey,
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () { setState(() {
selectedOption = 1;
}
);
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: selectedOption == 1 ? Colors.black: Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
}
}
You need to move 'selectedOption' variable to outside of 'build' method
because when call 'setState', build is called and variable is reset.
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int selectedOption = 0;
#override
void initState() {
super.initState();
}
#override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () {
setState(() {
selectedOption = 1;
});
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: selectedOption == 1 ? Colors.black : Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
}
Widget _buildBody() {
return Container();
}
}
With the current code Container color is updating only once. You need to change your code as follows to get the required output
return Scaffold(
key: scaffoldKey,
body: SafeArea(
child: Column(
mainAxisSize: MainAxisSize.max,
children: [
Card(
clipBehavior: Clip.antiAliasWithSaveLayer,
color: Color(0xFFF5F5F5),
// onTap is here
child: InkWell(
onTap: () {
setState(() {
_isSelected = !_isSelected;
});
},
child: Container(
width: 100,
height: 100,
decoration: BoxDecoration(
color: _isSelected ? Colors.black : Colors.cyan,
),
child: Text(
'Hello World',
style: TextStyle(),
),
),
),
),
],
),
),
);
Instead of using the int variable you should use a boolean and update its state when tapped.
Please move selectedOption to state class. then setstate() will work.

Creating a Custom widget in Flutter

import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
int _weight =60;
class RoundIconData extends StatefulWidget {
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: Icon(icon),
onPressed: (){
setState(() {
if(icon == FontAwesomeIcons.minus){
_weight--;
}
else{
_weight++
}
});
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
i am getting error while creating this.
What i Want
Custom widget with RawmaterialButton through which i can add icons.
if i add icon.minus then my given private weight wants to be decremented
else
given private weights to be incremented
You can copy paste run full code below
You have to move the following code to RoundIconData
RoundIconData({#required this.icon,#required this.pressme});
final IconData icon;
final int pressme;
and pass callback for refresh
working demo
full code
import 'package:flutter/material.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
int weight = 60;
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
refresh() {
setState(() {});
}
void _incrementCounter() {
setState(() {
_counter++;
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RoundIconData(
icon: Icon(FontAwesomeIcons.minus),
notifyParent: refresh,
),
RoundIconData(
icon: Icon(Icons.add),
notifyParent: refresh,
),
Text(
'${weight}',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class RoundIconData extends StatefulWidget {
final Icon icon;
final int pressme;
final Function() notifyParent;
RoundIconData(
{#required this.icon,
#required this.pressme,
#required this.notifyParent});
#override
_RoundIconDataState createState() => _RoundIconDataState();
}
class _RoundIconDataState extends State<RoundIconData> {
#override
Widget build(BuildContext context) {
return RawMaterialButton(
child: widget.icon,
onPressed: () {
print(widget.icon.toString());
print(Icon(FontAwesomeIcons.minus).toString());
if (widget.icon.toString() == Icon(FontAwesomeIcons.minus).toString()) {
weight--;
widget.notifyParent();
print(weight);
} else {
weight++;
widget.notifyParent();
print(weight);
}
},
elevation: 6.0,
constraints: BoxConstraints.tightFor(
width: 56.0,
height: 56.0,
),
shape: CircleBorder(),
fillColor: Color(0xFF4C4F5E),
);
}
}
Creating a Custom widget in Flutter
import 'package:flutter/material.dart';
class WelcomePage extends StatelessWidget {
// This widget is the root of your application.
#override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Flutter Auth',
theme: ThemeData(
primaryColor: Colors.purple,
scaffoldBackgroundColor: Colors.white,
),
home:Scaffold(
body: Center(
child: SingleChildScrollView(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"WELCOME TO XYZ",
style: TextStyle(fontWeight: FontWeight.bold,color: Colors.purple,fontSize: 25),
),
Padding(
padding: const EdgeInsets.only(right: 40),
child: Image.asset(
"assets/images/food_order.png",
height: 200,
),
),
SizedBox(height: 10 ),
loginMethod(),
signUpMethod(),
],
),
),
),
),
);
}
// Login Button Method Widget
Widget loginMethod(){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Login',
style: TextStyle(color: Colors.white),
),
),
),
);
}
// Signup button method widget
Widget signUpMethod (){
return Container(
margin: EdgeInsets.symmetric(vertical: 10),
width: 200,
height: 50,
child: ClipRRect(
borderRadius: BorderRadius.circular(29),
child: FlatButton(
padding: EdgeInsets.symmetric(vertical: 10, horizontal: 40),
color: Colors.blue,
onPressed: (){},
child: Text(
'Sign up',
style: TextStyle(color: Colors.white),
),
),
),
);
}
}

Gridview not filling body in landscape

I am using building a view that needs to be responsive on both landscape and portrait modes, I am using a gridview when I change to landscape however when in landscape the gridview doesnt fill the entire body
Gridview doesn't seem to have a default padding or anything
Example
In App Example
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() async {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double height;
final key = GlobalKey();
#override
initState() {
//calling the getHeight Function after the Layout is Rendered
WidgetsBinding.instance.addPostFrameCallback((_) => getHeight());
super.initState();
}
getHeight() {
final RenderBox renderBoxRed = key.currentContext.findRenderObject();
final size = renderBoxRed.size.height;
print("SIZE of container: $size");
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
height = constraints.maxHeight;
return (MediaQuery.of(context).size.width >= 600 &&
MediaQuery.of(context).orientation == Orientation.landscape)
? GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
children: [
Container(
height: height,
color: Colors.black,
),
Container(
key: key,
height: height,
color: Colors.red,
child: buildChildren(),
)
],
)
: Column(
children: <Widget>[
Container(
height: height / 2,
color: Colors.black,
),
Container(
key: key,
height: height / 2,
color: Colors.red,
child: buildChildren(),
)
],
);
},
),
backgroundColor: Colors.yellow,
);
}
buildChildren() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"Body height: " + height.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
Center(
child: MediaQuery.of(context).orientation == Orientation.landscape ? Text(
"Container height: " + 640.0.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
) : Text(
"Container height: " + 576.0.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
Center(
child: Text(
"Device height: " + MediaQuery.of(context).size.height.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
],
);
}
}
This is the minimal code needed to reproduce this issue the expected output is the black containers take up the entire available screen real estate, so that none of the scaffold background color is visible. Any help is appreciated.
My goal is to get an Image to fill the entire black container which should fill the entire body in landscape mode on all tablets I am testing on the nexus 10 emu
If I understand you clear, In landscape, only black will show.
emulator is slow and I have to push button to let emulator know I change to landscape
Edit I have edit my full code, In image part, you need
FittedBox(
child: Image.asset(
'assets/images/bg.jpg',
),
fit: BoxFit.fill,
))
full code
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() async {
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.landscapeLeft,
DeviceOrientation.landscapeRight
]);
runApp(
MyApp(),
);
}
class MyApp extends StatelessWidget {
#override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
#override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
double height;
double width;
final key = GlobalKey();
#override
initState() {
//calling the getHeight Function after the Layout is Rendered
WidgetsBinding.instance.addPostFrameCallback((_) => getHeight());
super.initState();
}
getHeight() {
final RenderBox renderBoxRed = key.currentContext.findRenderObject();
final size = renderBoxRed.size.height;
print("SIZE of container: $size");
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: LayoutBuilder(
builder: (BuildContext context, BoxConstraints constraints) {
height = constraints.maxHeight;
width = constraints.maxWidth;
return (MediaQuery.of(context).size.width >= 600 &&
MediaQuery.of(context).orientation == Orientation.landscape)
? GridView.count(
crossAxisCount: 2,
shrinkWrap: true,
children: [
Container(
height: height,
width: width / 2,
color: Colors.black,
child: FittedBox(
child: Image.asset(
'assets/images/bg.jpg',
),
fit: BoxFit.fill,
)),
Container(
key: key,
height: height,
color: Colors.red,
child: buildChildren(),
)
],
)
: Column(
children: <Widget>[
Container(
height: height / 2,
width: width ,
color: Colors.black,
child: FittedBox(
child: Image.asset(
'assets/images/bg.jpg',
),
fit: BoxFit.fill,
)),
Container(
key: key,
height: height / 2,
width: width ,
color: Colors.red,
child: buildChildren(),
)
],
);
},
),
backgroundColor: Colors.yellow,
);
}
buildChildren() {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Center(
child: Text(
"Body height: " + height.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
Center(
child: MediaQuery.of(context).orientation == Orientation.landscape
? Text(
"Container height: " + 640.0.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
)
: Text(
"Container height: " + 576.0.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
Center(
child: Text(
"Device height: " + MediaQuery.of(context).size.height.toString(),
style: TextStyle(color: Colors.white, fontSize: 30),
),
),
],
);
}
}