Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Breakpoint value is null on initial build #155

Open
jlin5 opened this issue Sep 14, 2023 · 6 comments
Open

Breakpoint value is null on initial build #155

jlin5 opened this issue Sep 14, 2023 · 6 comments

Comments

@jlin5
Copy link

jlin5 commented Sep 14, 2023

I noticed that the breakpoint value will be null on initial build and a subsequent build update would have the breakpoint value. This would cause a sudden change on the screen which is most noticeable when using the SingleChildScrollView widget. I used an iPad (10th generation) simulator and ran Flutter's hot restart multiple times to see the screen changing size. This is happening on v1.0.0 to v1.1.1.

Print logs:

flutter: MediaQuery width: 1180.0
flutter: breakpoint: Breakpoint(start: 0.0, end: 0.0, name: null)
flutter: isDesktop: false
flutter: isTablet: false
flutter: isMobile: false
flutter: isPhone: false
flutter: -=-=-=-=-=-=-=-=
flutter: MediaQuery width: 700.0
flutter: breakpoint: Breakpoint(start: 801.0, end: 1920.0, name: DESKTOP)
flutter: isDesktop: true
flutter: isTablet: false
flutter: isMobile: false
flutter: isPhone: false
flutter: -=-=-=-=-=-=-=-=

Code:

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});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      builder: (context, child) => ResponsiveBreakpoints.builder(
        child: child!,
        breakpoints: [
          const Breakpoint(start: 0, end: 450, name: MOBILE),
          const Breakpoint(start: 451, end: 800, name: TABLET),
          const Breakpoint(start: 801, end: 1920, name: DESKTOP),
          const Breakpoint(start: 1921, end: double.infinity, name: '4K'),
        ],
      ),
      onGenerateRoute: (RouteSettings settings) {
        return MaterialPageRoute(
          builder: (context) {
            return MaxWidthBox(
              maxWidth: 1200,
              background: Container(color: const Color(0xFFF5F5F5)),
              child: ResponsiveScaledBox(
                width: ResponsiveValue<double>(
                  context,
                  conditionalValues: [
                    Condition.equals(name: MOBILE, value: 450),
                    Condition.between(start: 800, end: 1100, value: 800),
                    Condition.between(start: 1000, end: 1200, value: 700),
                  ],
                ).value,
                child: BouncingScrollWrapper.builder(
                  context,
                  const MyHomePage(title: 'Flutter Demo Home Page'),
                  dragWithMouse: true,
                ),
              ),
            );
          },
        );
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    print('MediaQuery width: ${MediaQuery.of(context).size.width}');
    print('breakpoint: ${ResponsiveBreakpoints.of(context).breakpoint}');
    print('isDesktop: ${ResponsiveBreakpoints.of(context).isDesktop}');
    print('isTablet: ${ResponsiveBreakpoints.of(context).isTablet}');
    print('isMobile: ${ResponsiveBreakpoints.of(context).isMobile}');
    print('isPhone: ${ResponsiveBreakpoints.of(context).isPhone}');
    print('-=-=-=-=-=-=-=-=');

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: SingleChildScrollView(
        child: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              const Text(
                'You have pushed the button this many times:',
              ),
              Text(
                '$_counter',
                style: Theme.of(context).textTheme.headlineMedium,
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
@rayliverified
Copy link
Contributor

Thank you for reporting with the info.
This could be the null size on initial frame issue.
MediaQuery return null on the initial frame which makes layouts that rely on it not work on the first frame.

@pulsafi
Copy link

pulsafi commented Feb 10, 2024

Hello, has this been resolved? @jlin5 were you able to figure something out? I'm having the same problem :/

@jesus-villalobos
Copy link

Same problem here :( Any fixes or temporary work around to address that initial null value? @jlin5 @rayliverified I really want to use this framework in my new app. Thank you 🙏🏽

@jlin5
Copy link
Author

jlin5 commented Feb 13, 2024

@pulsafi @jesus-villalobos @rayliverified

I'm not sure what happened but the screen doesn't change suddenly anymore when I run the sample code. The breakpoint value is still null though on initial build. I noticed that in version 0.2.0, there was a conditional that would return an empty background if the screenWidth is 0.

@override
Widget build(BuildContext context) {
// Platform initialization requires context.
setPlatform();
// Initialization check. Window measurements not available until postFrameCallback.
// Return first frame with empty background.
if (screenWidth == 0) {
return buildBackground(
background: widget.background, color: widget.backgroundColor);
}
return InheritedResponsiveWrapper(

The fix would be to add a similar conditional before InheritedResponsiveBreakpoints in the latest version.

    // Initialization check. Window measurements not available until postFrameCallback.
    // Return first frame with empty widget.
    if (screenWidth == 0) {
      return Container();
    }

@override
Widget build(BuildContext context) {
// Platform initialization requires context.
setPlatform();
return InheritedResponsiveBreakpoints(
data: ResponsiveBreakpointsData.fromWidgetState(this),
child: widget.child,
);
}
}

@jesus-villalobos
Copy link

Thank you for looking into this @jlin5 🙏🏽 From my understanding, though, there is nothing that I can do on my end to catch this Null? Should I just add a check for if screenSize is Null? Or would we just have to wait until @rayliverified gets around to addressing this? Thanks :)

@rayliverified
Copy link
Contributor

rayliverified commented Feb 24, 2024

You're right, the initial frame 0 width and height issue is still present in the Flutter framework.

Let me read through the issues and see how people want to solve this.
So far, the v0.2.0 solution was an empty container with a configurable color.

Solution 1: Empty container
Complaints: Black, flickering.
Solution 2: Empty container with configurable color

We're currently at Solution 2.
I'll add Solution 2 back after doing some investigation into if it's compatible with initial loading screens / indicators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants