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

replace Container with SizedBox #2

Open
tjx666 opened this issue Mar 2, 2021 · 0 comments
Open

replace Container with SizedBox #2

tjx666 opened this issue Mar 2, 2021 · 0 comments

Comments

@tjx666
Copy link

tjx666 commented Mar 2, 2021

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

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

1 participant