categories.dart 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:flutter/material.dart';
  2. import 'package:fooddeliveryapp/constants.dart';
  3. class Categories extends StatefulWidget {
  4. @override
  5. _CategoriesState createState() => _CategoriesState();
  6. }
  7. class _CategoriesState extends State<Categories> {
  8. List<String> categories = ["锅底", "荤菜", "素菜", "主食", "酒水", "调料"];
  9. int selectedIndex = 0;
  10. @override
  11. Widget build(BuildContext context) {
  12. return Padding(
  13. padding: EdgeInsets.symmetric(vertical: kDefaultPaddin),
  14. child: SizedBox(
  15. height: 35,
  16. child: ListView.builder(
  17. itemCount: categories.length,
  18. scrollDirection: Axis.horizontal,
  19. itemBuilder: (context, index) => buildCategory(index)),
  20. ),
  21. );
  22. }
  23. Widget buildCategory(int index) {
  24. return GestureDetector(
  25. onTap: () {
  26. setState(() {
  27. selectedIndex = index;
  28. print("[order页面]选择了第" + index.toString() + "个菜单页面");
  29. });
  30. },
  31. child: Padding(
  32. padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin),
  33. child: Column(
  34. crossAxisAlignment: CrossAxisAlignment.start,
  35. children: [
  36. Text(
  37. categories[index],
  38. style: TextStyle(
  39. fontWeight: FontWeight.bold,
  40. color: selectedIndex == index ? kTextColor : kTextLightColor),
  41. ),
  42. Container(
  43. margin: EdgeInsets.only(top: kDefaultPaddin / 4),
  44. height: 2,
  45. width: 30,
  46. color: selectedIndex == index ? Colors.black : Colors.transparent,
  47. )
  48. ],
  49. ),
  50. ),
  51. );
  52. }
  53. }