ListView с тайлами, не работающими в Flutter - PullRequest
0 голосов
/ 22 декабря 2018

Почему в моем ListView не отображаются ожидаемые листы?

class DepartureCell extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 20,
      itemBuilder: (BuildContext context, int index) {
        return ListTile(
          title: new Text("Test"),
          onTap: () {},
        );
      }
    );
  }
}

Ошибка говорит:

flutter: ══╡ EXCEPTION CAUGHT BY RENDERING LIBRARY ╞═════════════════════════════════════════════════════════
flutter: The following assertion was thrown during performResize():
flutter: Vertical viewport was given unbounded height.
flutter: Viewports expand in the scrolling direction to fill their container.In this case, a vertical
flutter: viewport was given an unlimited amount of vertical space in which to expand. This situation
flutter: typically happens when a scrollable widget is nested inside another scrollable widget.
flutter: If this widget is always nested in a scrollable widget there is no need to use a viewport because
flutter: there will always be enough vertical space for the children. In this case, consider using a Column
flutter: instead. Otherwise, consider using the "shrinkWrap" property (or a ShrinkWrappingViewport) to size
flutter: the height of the viewport to the sum of the heights of its children.

Поскольку что-то происходит с unbound height, я также попыталсяследующее:

Код также не работает:

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemCount: 20,
      itemBuilder: (BuildContext context, int index) {
        return SizedBox(
          width: double.infinity,
          height: 40.0,
          child: ListTile(
            title: Text("Test"),
            onTap: () {},
            trailing: Text("Test"),
          ),
        );
      }
    );
  }
}

Как мне заставить это работать во Флаттере ???

Я также пытался с Column - ното же самое .... не работает!

Вот столбец-код, который опять не работает:

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        ListView.builder(
          padding: EdgeInsets.all(0.0),
          itemCount: 10,
          itemBuilder: (BuildContext context, int position) {
            return SizedBox(
              width: double.infinity,
              height: 40.0,
              child:ListTile(
                title: Text("Test"),
                onTap: () {},
                trailing: Text("Test"),
              ),
            );
          }
        ),
      ]
    );
  }
}

Ответы [ 2 ]

0 голосов
/ 22 декабря 2018

Я не знаю, какой у вас был предыдущий код.Если вы можете опубликовать свой предыдущий код, я могу объяснить вам исключение.

Вот упрощенная версия кода.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
          appBar: AppBar(
            title: Text("CH Station Demo"),
          ),
          body: DepartureCell()),
      //home: TrackerApp(),
    );
  }
}

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        padding: EdgeInsets.all(8.0),
        itemCount: 10,
        itemBuilder: (context, index) {
          return ListTile(
            title: Text("Test"),
            onTap: () {},
            trailing: Text("Test2"),
          );
        });
  }
}
0 голосов
/ 22 декабря 2018

Вот рабочий код, который я нашел (см. Ниже) ...

Однако я уверен, что его можно упростить.Но как ??Любые предложения ??

Вот весь рабочий код:

Сначала я вызываю DepartueCell () в main.dart ....

import 'package:flutter/material.dart';
import './departures/cells/departure_cell.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Text("CH Station Demo"),
        ),
        body: DepartureCell()
      ),
    );
  }
}

Затем я делаюrest ....

import 'package:flutter/material.dart';
import './../../models/ch_departure.dart';

class DepartureCell extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Center(
      child: ListView.builder(
          padding: EdgeInsets.all(8.0),
          itemCount: 10,
          itemBuilder: (context, index) {
            return Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                SizedBox(
                  width: double.infinity,
                  height: 50.0,
                  child: ListTile(
                    title: Text("Test"),
                    onTap: () {},
                    trailing: Text("Test2"),
                  ),
                )
              ],
            );
          }
        ),
    );
  }
}

Может кто-нибудь, пожалуйста, объясните, почему он вдруг работает, когда я упаковываю все внутри Center и помещаю столбец в ListView.builder ??

Есть ли здесьболее простое решение этого вопроса *

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...