1

我正在尝试创建一个包含在页面视图中的屏幕,该屏幕还包含部分屏幕的页面视图。

为了实现这一点,我为整个页面本身提供了无限的页面视图,然后每个页面都有一个标题视图,下半部分有一个带有 3 个可能选项的页面视图。我有这个工作,但是,我正在使用的页面我想要一个 StreamBuilder ......这就是导致问题的地方。

class DiaryPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _DiaryPage();
}

class _DiaryPage extends State<DiaryPage> with TickerProviderStateMixin {
  DiaryBloc _diaryBloc;
  TabController _tabController;
  PageController _pageController;

  @override
  void initState() {
    _diaryBloc = BlocProvider.of<DiaryBloc>(context);
    _diaryBloc.init();

    _tabController = TabController(length: 3, vsync: this);
    _pageController = PageController(initialPage: _diaryBloc.initialPage);

    super.initState();
  }

  @override
  void dispose() {
    _diaryBloc.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {

    return Flexible(
      child: PageView.builder(
        controller: _pageController,
        itemBuilder: (BuildContext context, int position) {
          return _buildPage(_diaryBloc.getDateFromPosition(position));
        },
        itemCount: _diaryBloc.amountOfPages,
      ),
    );
  }

  Widget _buildPage(DateTime date) {
    return Column(
      mainAxisSize: MainAxisSize.max,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: <Widget>[_getHeader(date), _getTabBody()],
    );
  }

  Widget _getHeader(DateTime date) {
    return Card(
      child: SizedBox(
        width: double.infinity,
        height: 125,
        child: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(8, 16, 8, 0),
              child: Text(
                '${DateFormat('EEEE').format(date)} ${date.day} ${DateFormat('MMMM').format(date)}',
                style: Theme.of(context).textTheme.subtitle,
                textScaleFactor: 1,
                textAlign: TextAlign.center,
              ),
            ),
            Row(
              mainAxisSize: MainAxisSize.max,
              children: <Widget>[
                IconButton(
                  icon: const Icon(Icons.chevron_left),
                  onPressed: () => {
                        _pageController.previousPage(
                            duration: Duration(milliseconds: 250),
                            curve: Curves.ease)
                      },
                ),
                const Expanded(child: LinearProgressIndicator()),
                IconButton(
                  icon: const Icon(Icons.chevron_right),
                  onPressed: () => {
                        _pageController.nextPage(
                            duration: Duration(milliseconds: 250),
                            curve: Curves.ease)
                      },
                ),
              ],
            ),
            Container(
              height: 40.0,
              child: DefaultTabController(
                length: 3,
                child: Scaffold(
                  backgroundColor: Colors.white,
                  appBar: TabBar(
                    controller: _tabController,
                    unselectedLabelColor: Colors.grey[500],
                    labelColor: Theme.of(context).primaryColor,
                    tabs: const <Widget>[
                      Tab(icon: Icon(Icons.pie_chart)),
                      Tab(icon: Icon(Icons.fastfood)),
                      Tab(icon: Icon(Icons.directions_run)),
                    ],
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _getTabBody() {
    return Expanded(
      child: TabBarView(
        controller: _tabController,
        children: <Widget>[
          _getOverviewScreen(),
          _getFoodScreen(),
          _getExerciseScreen(),
        ],
      ),
    );
  }

  // TODO - this seems to be the issue, wtf and why
  Widget _getBody() {
    return Flexible(
      child: StreamBuilder<Widget>(
        stream: _diaryBloc.widgetStream,
        initialData: _diaryBloc.buildEmptyWidget(),
        builder: (BuildContext context, AsyncSnapshot<Widget> snapshot) {
          return snapshot.data;
        },
      ),
    );
  }

  Widget _getExerciseScreen() {
    return Text("Exercise Screen"); //_getBody();
  }

  Widget _getFoodScreen() {
    return Text("Food Screen"); //_getBody();
  }

  Widget _getOverviewScreen() {
    return _getBody();
  }
}

如您所见,作为子页面视图的一部分返回了三个小部件,其中两个是正确显示的文本小部件,但是正确填充另一个文本小部件的 StreamBuilder 似乎给了我红屏死机. 有任何想法吗?

4

1 回答 1

2

修复了这个问题,它与 StreamBuilder 被包裹在一个灵活的而不是一个列中有关。然后,我添加了列以使 mainAxisSize 为 max... 似乎可以工作。

于 2019-06-23T14:33:23.083 回答