Skip to content

replace Container with SizedBox #2

@tjx666

Description

@tjx666

Now, when some child is not loaded, it would be replaced by an empty container, but it will cause It size fit parent.

For Example:

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

class TabView extends StatefulWidget {
  int index;
  TabView({Key key, this.index}) : super(key: key);

  @override
  _TabViewState createState() => _TabViewState();
}

class _TabViewState extends State<TabView> {
  @override
  void initState() {
    super.initState();
    print('init tabView ${widget.index}');
  }

  @override
  void dispose() {
    super.dispose();
    print('dispose tabView ${widget.index}');
  }

  @override
  Widget build(BuildContext context) {
    print('build tabView ${widget.index}');

    return Container(
      width: 200,
      height: 200,
      color: Colors.lightGreenAccent,
      child: Center(
        child: Text('view ${widget.index}'),
      ),
    );
  }
}

class TestLazyIndexedStack extends StatefulWidget {
  TestLazyIndexedStack({Key key}) : super(key: key);

  @override
  _TestLazyIndexedStackState createState() => _TestLazyIndexedStackState();
}

class _TestLazyIndexedStackState extends State<TestLazyIndexedStack> {
  int viewIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('测试 IndexedStack'),
      ),
      body: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Container(
            color: Colors.lightBlueAccent,
            child: LazyIndexedStack(
              index: viewIndex,
              itemCount: 3,
              itemBuilder: (context, index) {
                return TabView(index: index);
              },
            ),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: List<Widget>.generate(
              3,
              (index) => FlatButton(
                onPressed: () {
                  setState(() {
                    viewIndex = index;
                  });
                },
                child: Text('$index'),
              ),
            ),
          )
        ],
      ),
    );
  }
}

result in:

Screenshot_2021-03-02-15-25-16-280_com example flutter_app_demo

When replace Container with SizedBox:

  @override
  void initState() {
    _loaded = [];
    _children = [];
    for (int i = 0; i < widget.itemCount; ++i) {
      if (i == widget.index) {
        _children.add(widget.itemBuilder(context, i));
        _loaded.add(true);
      } else {
        _children.add(const SizedBox());
        _loaded.add(false);
      }
    }
    super.initState();
  }

Screenshot_2021-03-02-15-28-04-353_com example flutter_app_demo

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