Codelessly / ResponsiveFramework

Easily make Flutter apps responsive. Automatically adapt UI to different screen sizes. Responsiveness made simple. Demo: https://gallery.codelessly.com/flutterwebsites/minimal/
https://codelessly.com
MIT License
1.25k stars 150 forks source link

Should not calculate breakpoints when the app is inactive #149

Closed crizant closed 1 year ago

crizant commented 1 year ago

Run the following code on iPad simulator in landscape mode:

import 'package:flutter/material.dart';
import 'package:responsive_framework/responsive_framework.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return ResponsiveBreakpoints(
      breakpoints: const [
        Breakpoint(start: 0, end: 600, name: PHONE),
        Breakpoint(start: 601, end: 900, name: TABLET),
        Breakpoint(start: 901, end: double.infinity, name: DESKTOP),
      ],
      useShortestSide: true,
      landscapePlatforms: const [],
      child: MaterialApp(
        title: 'Flutter Demo',
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
          useMaterial3: true,
        ),
        home: const MyHomePage(),
      ),
    );
  }
}

class MyHomePage extends StatelessWidget {
  const MyHomePage({super.key});

  @override
  Widget build(BuildContext context) {
    print(ResponsiveBreakpoints.of(context).breakpoint);
    return const Placeholder();
  }
}

Check the console, you should see the following output:

flutter: Breakpoint(start: 601.0, end: 900.0, name: TABLET)

Press home button to go back to home screen, the breakpoint is calculated again, you can validate that by seeing the following on console:

flutter: Breakpoint(start: 0.0, end: 600.0, name: PHONE)

Bring back the app, you can see the breakpoint is calculated again:

flutter: Breakpoint(start: 601.0, end: 900.0, name: TABLET)

Since the ResponsiveBreakpointsState is already a WidgetsBindingObserver, why don't we observe the AppLifecycleState and calculate breakpoints only when the app is in foreground?

Related to this issue.

crizant commented 1 year ago

i'm wrong. The app would not switch layout on split view if it does not calculate breakpoint when inactive. This issue can be closed.