问题:在带有按钮和滚动条的GridView中添加小部件时出现问题。

huangapple go评论73阅读模式
英文:

Problem adding widget to GridView with button and scroll

问题

以下是代码的中文翻译部分:

一开始,我有一个Wrap,通过点击“添加”按钮可以向Wrap添加一个新的卡片。后来,我将Wrap更改为GridView,以便可以进行滚动。然而,现在当我按下按钮时,在设备上什么都不显示,但如果我将GridView改回Wrap并进行热重载,卡片就会显示出来?我不确定问题出在哪里。

此外,我无法滚动,我知道我做错了些什么,但我不知道是什么。

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() => _MainScreenState();
}

class _MainScreenState extends State<MainScreen> {

  List<Widget> cards = [];

  dynamic addReusableCard (String activity, int goal, double elapsed){
    return cards.add(ReusableCard(activityText: activity, activityLength: goal, activityTimeElapsed: elapsed,));
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: <Widget>[
              Text('时间卡片',
              style: TextStyle(
                fontSize: 20.0,
                letterSpacing: 5.0,
              ),),
              Text('您已经醒来了总共16小时',
                  style: TextStyle(
                    fontSize: 16.0,
                    fontStyle: FontStyle.italic,
                  ),),
              Expanded(
                flex: 9,
                child: GridView.count(
                    crossAxisCount: 2,
                    childAspectRatio: (175.0 / 220.0),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    physics: ScrollPhysics(),
                  children: cards
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(18.0),
                child: Align(
                  alignment: Alignment.bottomRight,
                  child: FloatingActionButton(
                    onPressed: (){
                      setState(() {
                        addReusableCard('阅读', 2, 0.3);
                      });
                    },
                    backgroundColor: kAccentColor,
                    elevation: 0.0,
                    child: Icon(Icons.add),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

希望这对你有帮助。

英文:

At first I had a Wrap that by clicking on the "add" button would add a new card to the Wrap. Then I changed the Wrap to GridView so that I could add scrolling. However, now when I press the button nothing shows on the device, but if I change GridView back to a Wrap and hot reload, the cards show up? I'm not sure what's wrong.

Also I can't scroll, I know I'm doing something wrong, but I have no idea what.

class MainScreen extends StatefulWidget {
  @override
  _MainScreenState createState() =&gt; _MainScreenState();
}

class _MainScreenState extends State&lt;MainScreen&gt; {

  List&lt;Widget&gt; cards = [];

  dynamic addReusableCard (String activity, int goal, double elapsed){
    return cards.add(ReusableCard(activityText: activity, activityLength: goal, activityTimeElapsed: elapsed,));
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Padding(
          padding: const EdgeInsets.symmetric(horizontal: 8.0),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.max,
            children: &lt;Widget&gt;[
              Text(&#39;TIME CARDS&#39;,
              style: TextStyle(
                fontSize: 20.0,
                letterSpacing: 5.0,
              ),),
              Text(&#39;You\&#39;re awake for a total of 16h&#39;,
                  style: TextStyle(
                    fontSize: 16.0,
                    fontStyle: FontStyle.italic,
                  ),),
              Expanded(
                flex: 9,
                child: GridView.count(
                    crossAxisCount: 2,
                    childAspectRatio: (175.0 / 220.0),
                    scrollDirection: Axis.vertical,
                    shrinkWrap: true,
                    physics: ScrollPhysics(),
                  children: cards
                ),
              ),
              Padding(
                padding: const EdgeInsets.all(18.0),
                child: Align(
                  alignment: Alignment.bottomRight,
                  child: FloatingActionButton(
                    onPressed: (){
                      setState(() {
                        addReusableCard(&#39;reading&#39;, 2, 0.3);
                      });
                    },
                    backgroundColor: kAccentColor,
                    elevation: 0.0,
                    child: Icon(Icons.add),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Thanks in advance

答案1

得分: 2

要实现动态列表长度,你应该使用 gridView.builder()

以下是相关的代码:

Expanded(
  flex: 9,
  child: GridView.builder(
    itemCount: cards.length,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2
    ),
    itemBuilder: (BuildContext context, int index) {
      return cards[index];
    }
  )
),
英文:

For a dynamic list length you should use gridView.builder()

Here is the relevant code:

Expanded(
  flex: 9,
  child: GridView.builder(
    itemCount: cards.length,
    gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      crossAxisCount: 2
    ),
    itemBuilder: (BuildContext context, int index) {
      return cards[index];
    }
  )
),

huangapple
  • 本文由 发表于 2020年1月6日 23:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614339.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定