【问题标题】:How to make dynamic listview in tabbar如何在标签栏中制作动态列表视图
【发布时间】:2021-03-26 23:36:39
【问题描述】:

我想在这里列出 cicrleavater,并且在那个 cicleavter 大小问题中,宽度不超过 20 !我想制作像 instagram 故事一样的列表...并且每次点击我都想显示相同的页面,但数据不同,当前的圆形头像边框需要显示黄色...!怎么做我告诉你我的屏幕尺寸问题顶部 whre cicleavter 尺寸问题我想制作动态列表视图并在我点击它时显示在边框上

    class FolderPageTabBAr extends StatefulWidget {
    @override
   _FolderPageTabBArState createState() => _FolderPageTabBArState();
    }

  class _FolderPageTabBArState extends State<FolderPageTabBAr> {

  List<Widget> pages = [
 CampaignFolder(),
 ShelfScreen(),
 CampaignFolder(),
 ShelfScreen(),

];

double Redius = 40.0;

@override
Widget build(BuildContext context) {
return DefaultTabController(
  length: pages.length,
  initialIndex: 0,
  child: Scaffold(
    body: Stack(
      children: <Widget>[
        TabBarView(
          children: pages,
        ),
        Container(
            margin: EdgeInsets.only(top: 110,left: 1),
            child: SizedBox(
              height: 80,
              width: 500,
              child: TabBar(
                tabs: [
                  Column(
                    children: <Widget>[
                      CircleAvatar(
                        backgroundImage: NetworkImage(Globals.PhotographerProf),
                        radius: 22,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child:Text(
                          'ALL',
                          overflow: TextOverflow.ellipsis,
                          style: new TextStyle(
                            fontSize: 12.0,
                            fontFamily: 'Roboto',
                            color: new Color(0xFF212121),
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      )
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      CircleAvatar(
                        radius: 22,
                        backgroundImage: NetworkImage(Globals.PhotographerProf),
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          Globals.Buisnessname,
                          overflow: TextOverflow.ellipsis,
                          style: new TextStyle(
                            fontSize: 11.0,
                            fontFamily: 'Roboto',
                            color: new Color(0xFF212121),
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      )
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      CircleAvatar(
                        backgroundImage: NetworkImage(Globals.PhotographerProf),
                        radius: 22,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: Text(
                          "Family",
                          overflow: TextOverflow.ellipsis,
                          style: new TextStyle(
                            fontSize: 10.0,
                            fontFamily: 'Roboto',
                            color: new Color(0xFF212121),
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      )
                    ],
                  ),
                  Column(
                    children: <Widget>[
                      CircleAvatar(
                        backgroundImage: NetworkImage(Globals.PhotographerProf),
                        radius: 22,
                      ),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child:Text(
                          "Album",
                          overflow: TextOverflow.ellipsis,
                          style: new TextStyle(
                            fontSize: 9.0,
                            fontFamily: 'Roboto',
                            color: new Color(0xFF212121),
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      )
                    ],
                  ),
                ],
                unselectedLabelColor: Colors.orange,
                labelColor: Colors.deepOrange,
                indicatorColor: Colors.transparent,
              ),
            )
          ),
      ],
    ),
  ),
  );
  }
  }

【问题讨论】:

    标签: flutter listings


    【解决方案1】:

    要创建看起来相似的多个(动态)视图,请使用List View Builder 完整示例:

    import 'package:flutter/cupertino.dart';
    import 'package:flutter/material.dart';
    
    class MyClass {
      final String url;
      final int id;
    
      MyClass(this.url, this.id);
    }
    
    class Foo extends StatefulWidget {
      @override
      State<StatefulWidget> createState() => FooState();
    }
    
    class FooState extends State<Foo> {
      int selectedIndex = 0;
      List<MyClass> items = [
        MyClass('https://picsum.photos/250?image=9', 1),
        MyClass('https://picsum.photos/250?image=10', 5),
        MyClass('https://picsum.photos/250?image=11', 33)
      ];
      @override
      Widget build(BuildContext context) {
        print("build");
        return Scaffold(
          appBar: AppBar(),
          body: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                height: 90,
                child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  shrinkWrap: true,
                  itemCount: items.length,
                  itemBuilder: (context, index) {
                    return GestureDetector(
                      onTap: () {
                        setState(() {
                          selectedIndex = index;
                        });
                      },
                      child: getAvatarView(items[index], index == selectedIndex),
                    );
                  },
                ),
              ),
              Text(
                "here is my page for id ${items[selectedIndex].id}",
                style: TextStyle(backgroundColor: Colors.black, color: Colors.red),
              ),
            ],
          ),
        );
      }
    
      Widget getAvatarView(MyClass item, bool isSelected) {
        return Container(
          margin: isSelected ? const EdgeInsets.all(5.0) : null,
          decoration: BoxDecoration(
              border: isSelected ? Border.all(color: Colors.yellow) : null),
          child: Column(
            children: <Widget>[
              CircleAvatar(
                backgroundImage: NetworkImage(item.url),
                radius: 22,
              ),
              Padding(
                padding: const EdgeInsets.all(8.0),
                child: Text(
                  'ALL',
                  overflow: TextOverflow.ellipsis,
                  style: new TextStyle(
                    fontSize: 12.0,
                    fontFamily: 'Roboto',
                    color: new Color(0xFF212121),
                    fontWeight: FontWeight.bold,
                  ),
                ),
              )
            ],
          ),
        );
      }
    }
    

    要在一个项目中传递多个属性(回调函数、img 的 url,...),请使用自定义类列表。

    【讨论】:

    • 感谢您的回复...!我无法在上面的代码中添加动态列表??
    • 不能在代码中添加列表是什么意思?您需要完整的代码示例吗?还是有什么具体问题?
    • 如果可能的话请给我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多