When I run the following code on an iPhone, the GridView bounces.
If the contents of the GridView over the iPhone screen size, I want it to scroll, but if it is within the iPhone screen size, I do not want it to bounce or scroll.
How do I modify the code below?
import 'package:flutter/material.dart';
void main() {
runApp(const DemoPage());
}
class DemoPage extends StatelessWidget {
const DemoPage({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Demo'),
),
body: GridView.count(
crossAxisCount: 2,
mainAxisSpacing: 8,
crossAxisSpacing: 8,
children: const [
Text("A"),
Text("B"),
],
),
)
);
}
}
As a side note, when launched in Chrome as shown below, it does not bounce if the content is smaller than the screen size, so it is working as expected. I would like to have this behavior on iPhone app as well.
$ flutter run
Multiple devices found:
...
[1]: macOS (macos)
[2]: Chrome (chrome)
Please choose one (To quit, press "q/Q"): 2
2
Answers
I noticed that
primary
is mentioned in documentphysics
.And below is mentioned in
primary
.Just add
primary: false,
.However, once the iPhone is turned sideways, which makes the content size larger than the screen size and scrollable, and then turned back to portrait, it seems to be scrollable(bounce) for some reason (my app is fixed to portrait, so it's not a problem at the moment).
I use the responsive_builder library when I want to use different logics on different devices.
You can use the following code as an answer to your question.