当前位置:   article > 正文

Flutter组件--分段控制器(DefaultTabController)_flutter defaulttabcontroller

flutter defaulttabcontroller

 1.代码示例

 

  1. void main() {
  2. runApp(MyApp());
  3. }
  4. class MyApp extends StatelessWidget {
  5. MyApp({Key? key}) : super(key: key);
  6. // This widget is the root of your application.
  7. @override
  8. Widget build(BuildContext context) {
  9. return MaterialApp(
  10. debugShowCheckedModeBanner: false,
  11. title: 'Flutter Demo',
  12. theme: ThemeData(
  13. //分栏颜色
  14. primarySwatch: Colors.yellow,
  15. //选中分栏时候的高亮状态颜色
  16. highlightColor: Color.fromRGBO(255, 255, 255, 0.5),
  17. //选中分栏时候,底层的水波纹颜色
  18. splashColor: Colors.blue,
  19. ),
  20. home: SegmentedControl(),
  21. );
  22. }
  23. }
  24. class SegmentedControl extends StatefulWidget {
  25. const SegmentedControl({super.key});
  26. @override
  27. State<SegmentedControl> createState() => _SegmentedControlState();
  28. }
  29. class _SegmentedControlState extends State<SegmentedControl> {
  30. @override
  31. Widget build(BuildContext context) {
  32. return DefaultTabController(
  33. length: 3,
  34. child: Scaffold(
  35. appBar: AppBar(
  36. title: const Text("Flutter App"),
  37. leading: Icon(Icons.menu),
  38. actions: [Icon(Icons.search)],
  39. //这里TabBar中的tabs数组个数需要跟TabBarView中的元素个数保持一致
  40. bottom: TabBar(
  41. unselectedLabelColor: Colors.black38, //未选中控件颜色
  42. indicatorColor: Colors.red, //指示器的颜色
  43. tabs: [
  44. Tab(icon: Icon(Icons.local_florist)),
  45. Tab(icon: Icon(Icons.change_history)),
  46. Tab(icon: Icon(Icons.directions_bike)),
  47. ]),
  48. ),
  49. body: TabBarView(children: [
  50. Tab(
  51. icon: Icon(
  52. Icons.local_florist,
  53. size: 128,
  54. color: Colors.black12,
  55. ),
  56. ),
  57. Tab(
  58. icon: Icon(
  59. Icons.change_history,
  60. size: 128,
  61. color: Colors.black12,
  62. )),
  63. Tab(
  64. icon: Icon(
  65. Icons.directions_bike,
  66. size: 128,
  67. color: Colors.black12,
  68. )),
  69. ]),
  70. ),
  71. );
  72. }
  73. }

2.效果示例

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/431971
推荐阅读
相关标签
  

闽ICP备14008679号