flutter 如何做这个设计

jc3wubiy  于 2023-01-31  发布在  Flutter
关注(0)|答案(3)|浏览(162)

我想让这个设计在Flutter我已经尝试了一排,但它会溢出。所以,你可以帮助我使这使用任何其他方式!

gcuhipw9

gcuhipw91#

好的,像其他答案建议的那样编写带有容器的芯片是没有必要的。因为你实际上在Flutter中有芯片小部件。请检查它们,它们有很好的文档记录并提供了示例。
https://api.flutter.dev/flutter/material/FilterChip-class.html
https://api.flutter.dev/flutter/material/ChoiceChip-class.html
https://api.flutter.dev/flutter/material/InputChip-class.html

vcudknz3

vcudknz32#

这里是确切的编码,我用在我的项目,它可能会帮助你...
根据您的设计更改..

class HomeScreen_Coffee extends StatefulWidget {
  const HomeScreen_Coffee({Key? key}) : super(key: key);

  @override
  State<HomeScreen_Coffee> createState() => _HomeScreen_CoffeeState();
}

class _HomeScreen_CoffeeState extends State<HomeScreen_Coffee> {
  List<String> titles=['All','Fav','Popular','Trending'
  ];

  int selectedindex=0;


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: 
            Container(
              height: 40,
              child: ListView.builder(
                  scrollDirection: Axis.horizontal,
                  itemCount: titles.length,
                  itemBuilder: (context,index){
                    return CoffeeTile(name: titles[index], ontap: (){
                      selectedindex=index;
                      setState(() {

                      });
                    },isselected: selectedindex==index,);
                  }),
            ),
           
        ));
  }
}
class CoffeeTile extends StatelessWidget {

  final String name;
  final bool isselected;
  final VoidCallback ontap;

  const CoffeeTile({Key? key,required this.name,this.isselected=false,required this.ontap}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.only(left: 12.0),
      child: GestureDetector(
        onTap: ontap,
        child: Container(
          decoration: BoxDecoration(
            color: Colors.white12,
            borderRadius: BorderRadius.circular(20),

          ),
          padding: EdgeInsets.symmetric(horizontal: 20),
          child: Center(
            child: Text(name.toString(),style: TextStyle(
              fontSize: 18,color: isselected?Colors.orange:Colors.grey,fontWeight: FontWeight.bold
            ),),
          ),
        ),
      ),
    );
  }
}
biswetbf

biswetbf3#

定义选定索引的变量。

int selectedIndex = 0;

只需放入您的小工具

SizedBox(
          height: 40,
          child: ListView.builder(
            itemCount: 20,
            scrollDirection: Axis.horizontal,
            shrinkWrap: true,
            physics: BouncingScrollPhysics(),
            itemBuilder: (context, index) {
              return InkWell(
                onTap: () {
                  selectedIndex = index;
                  setState(() {});
                },
                child: Container(
                  margin: EdgeInsets.symmetric(horizontal: 5),
                  decoration: BoxDecoration(
                    color: selectedIndex == index ? Colors.blueAccent.shade100 : Colors.white,
                    borderRadius: BorderRadius.circular(30),
                  ),
                  padding: EdgeInsets.symmetric(vertical: selectedIndex == index ? 12 : 10, horizontal: selectedIndex == index ? 18 : 15),
                  child: Text("Text $index", style: TextStyle(color: selectedIndex == index ? Colors.white : Colors.grey, fontSize: selectedIndex == index ? 15 : 12)),
                ),
              );
            },
          ),
        )
    • 我希望这能解决您的问题。**

相关问题