How to use Flutter OrientationBuilder without GridVeiw - android

All OrientationBuilder examples I could find use GridView.
I wanted to use Column and Row widgets instead.
This is a code excerpt I used. This works for portrait, but when I turn the phone to landscape, the widgets disappear, I just see background. Turning it back to portrait brings it back to normal.
What am I doing wrong here?
body: OrientationBuilder(builder: (context, orientation) {
if (orientation == Orientation.portrait) {
return Column(
children: [
Widget1(),
Widget2(),
Widget3(),
],
);
} else {
return Row(
children: [
Widget1(),
Widget2(),
Widget3(),
],
);
}
}),

Seems there is nothing wrong with my approach, it's just that my widgets don't fit the screen. Not sure why is nothing displayed, I'd expect something to be visible and yellow/black warning to be displayed.
Anyway, using a table with FractionColumnWidth forces the widgets to be rendered on screen even if they don't fit. Building UI with flutter is not going to be easy!
return Table(columnWidths: {
0: FractionColumnWidth(.25),
1: FractionColumnWidth(.5),
2: FractionColumnWidth(.25)
}, children: <TableRow>[
TableRow(children: [
Widget1(),
Widget2(),
Widget3(),
]),
]);

Related

Lottie animation glitch - Flutter

I have a very simple animation for a splash screen of a shaking jar that I made in Adobe After Effects and exported it via LottieFiles extension as json, it works normal but every couple of times when I open the app the animation glitches (see pictures below), I can't figure it out and have tried searching for a solution without any success. The problem is the glitching happens random (sometimes it's a couple of times in row when I open app, sometimes it's every x times). This was tested on multiple android devices with same result.
Code for splash screen:
#override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: isDarkMode()? const Color(0xFF31302f) : const Color(0xFFfcfaf6),
body:
Align(
alignment: Alignment.topCenter,
child: Column(
children: [
SizedBox(height: height(context)*0.08),
Stack(
children: [
Image.asset(
isDarkMode()? 'assets/crnikruh.png' : 'assets/bijelikruh.png',
height: height(context)*0.3,
width: height(context)*0.3,
),
Padding(
padding: EdgeInsets.only(top: height(context)*0.01),
child: Lottie.asset(
'lottie/jaranimation.json',
width: height(context)*0.3,
height: height(context)*0.3,
frameRate: FrameRate.max,
fit: BoxFit.fill,
),
)
],
),
],
),
),
);
}
}
This is the normal view
This is the animation with the problem
I've found a workaround for this issue. It's not quite the solution but it works. So, I think the problem was it wasn't loading properly every time and the trick that helped me was that I wrapped the lottie asset with visibility widget and delayed the visibility by 50 milliseconds, so it got that extra 50 milliseconds on startup to properly load the asset.

Flutter camera preview didn't show up before hot reload

Long story short, I use flutter's camera dependency. Everything works fine until it's time for the camera preview to show up, but it doesn't even though the camera hardware is activated already (I use emulator so I can see the webcam indicator lamp is active). When I hot reload it, the preview shows. I tried programmatical force rebuild but it doesn't work because the problem lays on the value of camera controller (that's what the output says).
Without further ado, here's my snippet
if (camCtrl == null || !camCtrl.value.isInitialized) {
return LoadingOverlay();
} else if (camCtrl.value.isInitialized) {
return Stack(
alignment: Alignment.bottomCenter,
children: [
SizedBox(
width: displayWidth(context),
child: camCtrl.value.isInitialized
? CameraPreview(camCtrl)
: LoadingOverlay(),
),
Padding(
padding: EdgeInsets.only(bottom: 20),
child: FloatingActionButton(child: Icon(Icons.camera), onPressed: () {}),
)
],
);
}
This happens due to the mishandling of Futures.
Try to go through your Code again.
I guess you have missed the await keyboard somewhere.
Also, you can use a FutureBuilder here.
If none helps, refer this - https://flutter.dev/docs/cookbook/plugins/picture-using-camera

How to solve this widget problem on flutter?

In 'body', two 'child' cannot be in? just one allowed?
If it is, how can I add several widgets like in the picture? ex) ElevatedButton(), Column(children: ?
And there is a red underline below "Column(children: ".
Could someone tell me why this happened?
you need to do the following
body: Center(
child: Column(
children: [
ElevatedButton(child: ...)
Container(child: ....)
...
]

Flutter listview always scrolling to bottom when reverse is true

Why I am using reverse is true, Its for achieving fetch messages from realtime database sorted by time added. after doing this, New messages at ToP and old messages lies down in listview. Everything works well, this is the condition I wanted. But The list view is always scrolling to bottom instead of lying on top.If user enters the page, he is will see old posts first, instead of new posts. Here is my ocde
return ListView.builder(
controller: _scrollController,
shrinkWrap: true,
//physics: const AlwaysScrollableScrollPhysics(),
itemCount: nots.length,
reverse: true,
itemBuilder: (context, index) {
return Container(
color: Colors.amber[50],
child: Padding(
padding: const EdgeInsets.all(0.0),
child: Row(
children: <Widget>[
column(nots[index].title,nots[index].date,nots[index].body,nots[index].photourl),
//column,
],
),
),
);
},
);
If I remove reverse: True. then old posts lies on top and new messages lies on bottom. But in this case listview is not scrolling to bottom. In short words, When I use both cases. Users can only see old posts first. But I want to see them new posts first.
ListView reverse: true displays the List fed to it from the bottom. If you'd like for the ListView to start at the top, you'd need to sort the List to be displayed on the ListView instead.

How to avoid full page reloading when keyboard pops up?

Check out this video for problem demo
https://youtu.be/GsdWcTEbUbg
As there is a large number of code, I will try to summarize the structure of the code here.
In a nutshell:
Scaffold(
appBar: AppBar(),
body: StreamBuilder<dynamic>(
stream: globals.chatRoomBloc.threadScreen,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return Column(
crossAxisAlignment:
CrossAxisAlignment.stretch, // sticks to the keyboard
children: <Widget>[
Expanded(
child: Scrollbar(
child: ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return TileWidget();
},
),
),
),
],
);
},
),
)
TileWidget is stateful, appends the TextField and resize the widget when the reply button is pressed. Then when the user clicks on the Textfield, the keyboard pops up.
Now my problem is that the screen is reloaded when the keyboard pops up.
I tried the following solutions:
Using TextField inside a Streambuilder : I am setting up the stream only once, that is when the page is loaded the first time. Any changes to the stream is made when a new chat or entry is added. In my case, this does not happen.
Flutter Switching to Tab Reloads Widgets and runs FutureBuilder I am not sure if this is the same problem, but the solution does not change anything for me.
Issue #11895 - I went through this as well, but is not helping.
I think the screen is trying to resize and redraw to accommodate the keyboard drawer. But it is for some reason failing to do that and loading everything over again. Am I missing something ? Is there a way around this ?
As one of the links you provides points out your buildmethod fires whenever the state of the app changes i.e the keyboard pops up, so move the streambuilder outside of it. There are a few more changes you could do. Try the following,
create a variable outside of the build method.
var myStreamBuilder;
//....
//inside initialstate method
myStreamBuilder = StreamBuilder<dynamic>(
stream: globals.chatRoomBloc.threadScreen,
builder: (BuildContext context, AsyncSnapshot snapshot) {
return Column(
crossAxisAlignment:
CrossAxisAlignment.stretch, // sticks to the keyboard
children: <Widget>[
Expanded(
child: Scrollbar(
child: ListView.builder(
shrinkWrap: true,
itemCount: list.length,
itemBuilder: (BuildContext context, int index) {
return TileWidget();
},
),
),
),
],
);
},
),
Then in your build method call the variable.
Scaffold(
appBar: AppBar(),
resizeToAvoidBottomInset: false, //don't forget this!
body: myStreamBuilder
)
EDIT: Make sure you check your snapshot that it hasData or not. If there is no data then something should be returned until it does have data, this way the user is informed what the app is doing.
Also this property might also be helpful to you. resizeToAvoidBottomInset - https://api.flutter.dev/flutter/material/Scaffold/resizeToAvoidBottomInset.html
I think if you use of MediaQuery So that's why Probleam create and one more Solution is Create constructor and getList again.
class Demo(){
getlist(); // get list again
}
I faced similar issue when keyboard open or closed the page reloads
basically I just delete the following line from MainActivity.XML
and all works fine
android:windowSoftInputMode="adjustResize">

Categories

Resources