首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带卡片的颤振列表视图

问带卡片的颤振列表视图
EN

Stack Overflow用户
提问于 2022-04-07 08:57:10
回答 4查看 642关注 0票数 0

我是一个颤振编程的初学者,我正处于学习阶段。我试图只创建一个列表的UI使用虚拟数据的项目,可以购买非常频繁的客户。例如,一位顾客经常买铅笔,而一支笔却不常买,所以铅笔会排在第一位,笔会在铅笔下面等等。下面是我想要创建的等待您的建议的图片。谢谢

简而言之,经常购买的物品排在第一位。

​

​

EN

回答 4

Stack Overflow用户

发布于 2022-04-07 09:28:07

代码语言:javascript
复制
     List dataItems = [
    {"product": "pencil", "frequency" :4},
      {"product": "pencil2", "frequency" :4},
      {"product": "pencil4", "frequency" :4}
  ];


 Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                 Row(children: const[
                   Text("No"),
                   SizedBox(width: 16),
                   Text("Product"),
                    SizedBox(width: 16),
                   Text("Frequency"),
                  
                 ]),
                Expanded(
                child:      ListView.builder(
                  itemCount: 2,
                itemBuilder: (ctx, index){
                  return       Row(children: [
                   Text(index.toString()),
                     SizedBox(width: 16),
                   Text(dataItems[index]["product"]),
                     SizedBox(width: 16),
                    Text(dataItems[index]["frequency"].toString()),
                    
                    Spacer(),
                    
                     MaterialButton(onPressed: (){}, child: Text("Deliver"), ),
                    MaterialButton(onPressed: (){}, child: Text("Self Pickup"), )
                   
                 ]);
                }
                  
                ))
              ],
            ),

您可以使用这样的实现,尝试使用row方法来划分行中的项。

票数 0
EN

Stack Overflow用户

发布于 2022-04-07 09:42:14

我希望这帮不了什么忙

代码语言:javascript
复制
class DemoWork extends StatefulWidget {
  const DemoWork({Key? key}) : super(key: key);

  @override
  State<DemoWork> createState() => _DemoWorkState();
}

class _DemoWorkState extends State<DemoWork> {

  List product=[
    {'product':'pencil', 'frequency': 10}, {'product':'pen','frequency':24}, {'product':'notebook','frequency':12}, {'product':'markers','frequency':2}, {'product':'erasers','frequency':21}
  ];
Iterable<dynamic>? data;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    product.sort((a, b) {
      return a['frequency'].compareTo(b['frequency']);
    });
   //output is {product: pen, frequency: 24}, {product: erasers, frequency: 21}, {product: notebook, frequency: 12}, {product: pencil, frequency: 10}, {product: markers, frequency: 2}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: ListView.builder(
          itemCount: 5,
          shrinkWrap: true,
          reverse: true,// because list show low to high so reverse use to show high to low value and shrink wrap adjust its size use as you want to show and adjust it 
          itemBuilder: (context, index) {
        return ListTile(
          leading:Text(index.toString()) ,
          title: Text(product[index]['product']),
          trailing: Text(product[index]['frequency'].toString()),
        );
      }),
    );
  }
}
票数 0
EN

Stack Overflow用户

发布于 2022-04-07 09:52:44

你可以数数时间,用户购买的东西,并使用它来排序的列表。当然,你需要为自己构建你的小部件,我只会建议你的逻辑。

示例

代码语言:javascript
复制
void main() {
  var stuffs = ['ball', 'pen', 'pencil', 'glass']; // your stuff here
  var history = ['pencil', 'pencil', 'glass', 'pen', 'glass', 'pencil']; // your buy history here, you can add more if you want
  
  print('stuff will dispaly in order: $stuffs');
  
  stuffs.sort((a,b) => history.timesOf(b).compareTo(history.timesOf(a))); // Function that sort the list by 'buy times' of user store in `history` variable
  print('stuff will dispaly in order when using history: $stuffs');
  
}

extension HistoryCheck on List<String> {
  int timesOf(String name) => where((_) => _ == name).length;
}

// ===== Result =====
stuff will dispaly in order: [ball, pen, pencil, glass]
stuff will dispaly in order when using history: [pencil, glass, pen, ball]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71779148

复制
相关文章

相似问题

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