Skip to content

size problem with SizedBox and Container not child in Scaffold #18399

Closed

Description

@wanghuangang

in main dart return Scaffold Widget

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    SizedBox sziebox =  new SizedBox(
      width: 100.0,
      height: 100.0,
      child: new DecoratedBox(
        decoration: new BoxDecoration(color: Colors.red),
      ),
    );

    return new Scaffold(
      appBar: new AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.
        title: new Text(widget.title),
      ),
      body: sziebox,
      floatingActionButton: new FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: new Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

the red color size is 100*100,this is right .

the problem code:

  @override
  Widget build(BuildContext context) {
    // This method is rerun every time setState is called, for instance as done
    // by the _incrementCounter method above.
    //
    // The Flutter framework has been optimized to make rerunning build methods
    // fast, so that you can just rebuild anything that needs updating rather
    // than having to individually change instances of widgets.
    SizedBox sziebox =  new SizedBox(
      width: 100.0,
      height: 100.0,
      child: new DecoratedBox(
        decoration: new BoxDecoration(color: Colors.red),
      ),
    );
    return sziebox;
 }

the red color size fill the whole screen.why size is not 100 * 100??
this is right image

image

this is problem image

image

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel master, v0.5.3-pre.21, on Microsoft Windows [Version 10.0.17134.48], locale zh-CN)
[√] Android toolchain - develop for Android devices (Android SDK 27.0.3)
[√] Android Studio (version 3.1)
X Flutter plugin not installed; this adds Flutter specific functionality.
X Dart plugin not installed; this adds Dart specific functionality.
[√] IntelliJ IDEA Ultimate Edition (version 2018.1)
[√] Connected devices (1 available)

• No issues found!

Activity

  1. changed the title [-]size problem whth SizedBox and Container ont in Scaffold[/-] [+]size problem whth SizedBox and Container not in Scaffold[/+] on Jun 12, 2018
  2. changed the title [-]size problem whth SizedBox and Container not in Scaffold[/-] [+]size problem with SizedBox and Container not child in Scaffold[/+] on Jun 12, 2018
  3. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jun 12, 2018
  4. zoechi commented on Jun 12, 2018

    @zoechi
    Contributor
  5. HansMuller commented on Jun 12, 2018

    @HansMuller
    Contributor

    The MaterialApp's home widget is constrained to a fixed size that matches the size of the screen.

    A parent widget's constraints trump its child's constraints, which is why the red container in the second version of your example matches the size of the screen.

    You can get the result you want by adding an additional container, like Align:

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: new Align(
            alignment: Alignment.topLeft,
            child: new Container(
              width: 100.0,
              height: 100.0,
              color: Colors.red,
            ),
          ),
        );
      }
    }
    
  6. wanghuangang commented on Jun 13, 2018

    @wanghuangang
    Author

    @HansMuller thank you, this is the key point A parent widget's constraints trump its child's constraints,

  7. josvos commented on Sep 24, 2018

    @josvos

    @HansMuller can you point me to the explanation of the theory "a parent widget's constraints trump its child's constraints" for the case a parent is larger than its child? I had this same problem (and solved it now with the Scaffold), but to me it sounds very illogical that (e.g.) the size of a SizedBox is ignored, while its parent is much larger (i.e. the whole screen).

  8. footurist commented on Nov 3, 2018

    @footurist

    @HansMuller

    A parent widget's constraints trump its child's constraints, which is why the red container in the second version of your example matches the size of the screen.

    Would it be useful to add a description of this behavior to the following part of Container's documentation ? From the current description, it seems unclear to me which constraints win.

    If the widget has no child and no alignment, but a height, width, or constraints are provided, then the Container tries to be as small as possible given the combination of those constraints and the parent's constraints.

  9. github-actions commented on Sep 1, 2021

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions