Skip to content

Should not calculate breakpoints when the app is inactive #149

@crizant

Description

@crizant

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions