当前位置:   article > 正文

flutter->Scaffold左侧/右侧侧边栏

flutter->Scaffold左侧/右侧侧边栏

//appBar的 leading/actions   和 Scaffold的drawer/endDrawer  冲突只能存在一个 

  1. import 'package:flutter/material.dart';
  2. void main() {
  3. runApp(MyApp());
  4. }
  5. class MyApp extends StatelessWidget {
  6. const MyApp({super.key});
  7. @override
  8. Widget build(BuildContext context) {
  9. return const MaterialApp(title: 'contaniner', home: HomePage());
  10. }
  11. }
  12. class HomePage extends StatefulWidget {
  13. const HomePage({super.key});
  14. @override
  15. State<HomePage> createState() => _HomePageState();
  16. }
  17. class _HomePageState extends State<HomePage> {
  18. final _leftList = List.generate(
  19. 20,
  20. (index) => const Column(
  21. children: [
  22. ListTile(
  23. leading: CircleAvatar(child: Icon(Icons.swipe_left, size: 30)),
  24. title: Text('左侧数据'),
  25. trailing: Icon(Icons.keyboard_arrow_right),
  26. ),
  27. Divider()
  28. ],
  29. ),
  30. );
  31. final _rightList = List.generate(
  32. 20,
  33. (index) => const Column(
  34. children: [
  35. ListTile(
  36. leading: CircleAvatar(child: Icon(Icons.swipe_right, size: 30)),
  37. title: Text('右侧数据'),
  38. selectedColor: Colors.green,
  39. trailing: Icon(Icons.keyboard_arrow_right),
  40. ),
  41. Divider()
  42. ],
  43. ),
  44. );
  45. @override
  46. Widget build(BuildContext context) {
  47. return Scaffold(
  48. drawer: Drawer(
  49. child: Column(
  50. children: [
  51. Container(
  52. width: double.infinity,
  53. child: const DrawerHeader(
  54. decoration: BoxDecoration(color: Colors.pink),
  55. child: Text("左侧侧数据"),
  56. ),
  57. ),
  58. Expanded(
  59. child: ListView(
  60. children: [..._leftList],
  61. ),
  62. ),
  63. ],
  64. ),
  65. ),
  66. endDrawer: Drawer(
  67. child: Column(
  68. children: [
  69. Container(
  70. width: double.infinity,
  71. child: const DrawerHeader(
  72. decoration: BoxDecoration(color: Colors.green),
  73. child: Text("左侧侧数据"),
  74. ),
  75. ),
  76. Expanded(
  77. child: ListView(
  78. children: [..._rightList],
  79. ),
  80. ),
  81. ],
  82. ),
  83. ),
  84. appBar: AppBar(
  85. title: const Center(child: Text('flutter bar')),
  86. // leading: IconButton(
  87. // onPressed: () => print("我是图标按钮1"),
  88. // icon: Icon(Icons.ac_unit), //设置图标
  89. // color: Colors.red, //设置按钮颜色
  90. // splashColor: Colors.yellow, //设置水波纹
  91. // highlightColor: Colors.purple, //设置高亮的颜色
  92. // tooltip: '我是提示信息', //提示信息
  93. // ),
  94. // actions: const [
  95. // Icon(Icons.settings),
  96. // Icon(Icons.vaccines),
  97. // ],
  98. backgroundColor: Colors.cyan[800],
  99. elevation: 0.0,
  100. centerTitle: true,
  101. ),
  102. //带索引的集合循环
  103. body:HomeWidget()
  104. );
  105. }
  106. }
  107. class HomeWidget extends StatelessWidget {
  108. const HomeWidget({super.key});
  109. @override
  110. Widget build(BuildContext context) {
  111. // return const Text('我是首页数据', style: TextStyle(color: Colors.red));
  112. return ListView(
  113. children: List.generate(
  114. 20,
  115. (index) => ListTile(
  116. leading: Icon(Icons.access_alarm, size: 30),
  117. title: Text('测试$index'),
  118. subtitle: Text('子标题$index'),
  119. selected: index == 1,
  120. selectedColor: Colors.green,
  121. trailing: Icon(Icons.keyboard_arrow_right),
  122. ),
  123. ));
  124. }
  125. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/282473
推荐阅读
相关标签
  

闽ICP备14008679号