首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >新文本(counter.toString())显示不更新计数器增量在颤振

新文本(counter.toString())显示不更新计数器增量在颤振
EN

Stack Overflow用户
提问于 2019-11-01 11:01:45
回答 1查看 536关注 0票数 0

我正在使用flutter构建一个食品推车应用程序。但是,当Listtile的尾部增加或减少时,我不得不显示计数器值。

每当一个新的项目被添加到购物车时,计数器就会增加,但是应用程序不会显示当前的计数器值。

下面是下面的代码。我已经在faced.Thank你的问题上发表了评论。

代码语言:javascript
代码运行次数:0
运行
复制
import 'package:flutter/material.dart';
import 'package:ifhe_canteen/StudentCart.dart';
class StudentHomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: HomePage(),
    );//MaterialApp
  }
}
class HomePage extends StatefulWidget{
  @override
  HomePage1 createState() => new HomePage1();
  }
class HomePage1 extends State<HomePage>{
  int selectedPage=0;
  final pageOptions=[
    Text('Messages'),
    Text('Cart'),
    Text('Profile'),

  ];
  @override
  Widget build(BuildContext context) {
    var Listview=ListView(
      children: <Widget>[
        ListTile(
          leading: Icon(Icons.fastfood),
        )
      ],
    );
    return Scaffold(
      appBar: AppBar(
        title: Text('Categories'),
      ),
      body:new Container(
        child: new ListView.builder(
          itemBuilder: (BuildContext context, int index) {
            return new StuffInTiles(listOfTiles[index]);
          },
          itemCount: listOfTiles.length,
        ),
      ),//body: pageOptions[selectedPage],
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: selectedPage,
        onTap: (int index){
          setState(() {
            selectedPage=index;
          });
        },// this will be set when a newx  tab is tapped
        items: [
          BottomNavigationBarItem(
            icon: new Icon(Icons.home),
            title: new Text('Home'),
          ),
          BottomNavigationBarItem(
            icon: new Icon(Icons.shopping_basket),
            title: new Text('Cart'),
          ),
          BottomNavigationBarItem(
              icon: Icon(Icons.person),
              title: Text('Profile'),
          ),
        ],
      ),
    );
  }
}
class StuffInTiles extends StatelessWidget {
  final MyTile myTile;
  StuffInTiles(this.myTile);
  int _itemCount;
  String item;
  void _removeproduct(){
    _itemCount--;
    print(_itemCount);
  }
  int _addproduct(){
    _itemCount++;
    print(_itemCount);
    item=_itemCount.toString();
    print(item);
    return _itemCount;
  }
  @override
  Widget build(BuildContext context) {
    return _buildTiles(myTile);
  }

  Widget _buildTiles  (MyTile t) {
    if (t.children.isEmpty)
      {
        return new ListTile(
          dense: true,
          enabled: true,
          isThreeLine: false,
          title: new Text(t.title),
          trailing:new Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new  IconButton(icon: new Icon(Icons.remove),onPressed:()=>_removeproduct()),
              new Text(_itemCount.toString()),//here, the incremented value  is not displayed
              new IconButton(icon: new Icon(Icons.add),onPressed:()=>_addproduct()),//here the function is called
            ],
          ),
          onTap:()=>print("here cart value will be incremented"),
        );
      }
    return new ExpansionTile(
      key: new PageStorageKey<MyTile>(t),
      title: new Text(t.title),
      children: t.children.map(_buildTiles).toList(),
    );
  }
}

class MyTile {
  String title;
  List<MyTile> children;
  MyTile(this.title, [this.children = const <MyTile>[]]);
}

List<MyTile> listOfTiles = <MyTile>[
  new MyTile(
    'Mess 1',
    <MyTile>[
      new MyTile(
        'Maggie',
        <MyTile>[
          new MyTile('Veg Cheese Maggie'),
          new MyTile('Plain Maggie'),
          new MyTile('Chicken Maggie'),
        ],
      ),//Maggie
      new MyTile(
          'Paratha',
          <MyTile>[
            new MyTile('Veg Paratha'),
            new MyTile('Veg Butter Paratha'),
            new MyTile('Chicken Paratha'),
            new MyTile('Chicken Butter Paratha'),
          ],
      ),//Paratha
    ],
  ),
  new MyTile(
    'Mess 2',
    <MyTile>[
      new MyTile(
        'Maggie',
        <MyTile>[
          new MyTile('Veg Cheese Maggie'),
          new MyTile('Plain Maggie'),
          new MyTile('Chicken Maggie'),
        ],
      ),//Maggie
      new MyTile(
        'Paratha',
        <MyTile>[
          new MyTile('Veg Paratha'),
          new MyTile('Veg Butter Paratha'),
          new MyTile('Chicken Paratha'),
          new MyTile('Chicken Butter Paratha'),
        ],
      ),//Paratha
    ],
  ),
];
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-01 11:33:32

当您更新代码中的变量时,您需要调用setState,并且希望在屏幕上看到这种更改。注意,它只在StatefulWidgets中工作,这意味着如果您需要它,您需要一个StatefulWidget (就像您已经做的那样)。setState方法将刷新屏幕,查看的值将发生变化。

编辑_addproduct_removeproduct函数,以便在_itemCounter变量上有一个setState:

代码语言:javascript
代码运行次数:0
运行
复制
void _removeproduct(){
  setState((){
    _itemCount--;
  });
}
int _addproduct(){
  setState((){
    _itemCount++;
  });
}

我建议你读一些文章如何使用颤振状态。诚挚的问候。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58658105

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档