Advertisement
Guest User

Untitled

a guest
May 29th, 2021
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. class TabExample extends StatefulWidget {
  2. const TabExample({Key? key}) : super(key: key);
  3.  
  4. @override
  5. _TabExampleState createState() => _TabExampleState();
  6. }
  7.  
  8. class _TabExampleState extends State<TabExample> {
  9. int currentIndex = 0;
  10.  
  11. @override
  12. Widget build(BuildContext context) {
  13. return DefaultTabController(
  14. length: 2,
  15. child: Builder(
  16. builder: (ctx) {
  17. final TabController tabController = DefaultTabController.of(ctx)!;
  18. tabController.addListener(() {
  19. if (!tabController.indexIsChanging) {
  20. setState(() => currentIndex = tabController.index);
  21. }
  22. });
  23. return Scaffold(
  24. appBar: AppBar(
  25. bottom: TabBar(
  26. tabs: [
  27. Tab(icon: Text('Tab ONE')),
  28. Tab(icon: Text('Tab TWO')),
  29. ],
  30. ),
  31. title: Text('Tabs Demo'),
  32. ),
  33. body: TabBarView(
  34. children: [
  35. _buildWidgetA(),
  36. _buildWidgetB(),
  37. ],
  38. ),
  39. );
  40. },
  41. ),
  42. );
  43. }
  44.  
  45. Widget _buildWidgetA() {
  46. List<Widget> children = [];
  47. for (int i = 0; i < 20; i++) {
  48. children.add(
  49. Padding(
  50. padding: EdgeInsets.symmetric(vertical: 16),
  51. child: Container(
  52. height: 100,
  53. width: double.infinity,
  54. color: Colors.blue,
  55. ),
  56. ),
  57. );
  58. }
  59. return Scrollbar(
  60. isAlwaysShown: true,
  61. showTrackOnHover: true,
  62. child: SingleChildScrollView(
  63. primary: currentIndex == 0,
  64. child: Column(
  65. children: children,
  66. ),
  67. ),
  68. );
  69. }
  70.  
  71. Widget _buildWidgetB() {
  72. List<Widget> children = [];
  73. for (int i = 0; i < 20; i++) {
  74. children.add(
  75. Padding(
  76. padding: EdgeInsets.symmetric(vertical: 16),
  77. child: Container(
  78. height: 100,
  79. width: double.infinity,
  80. color: Colors.green,
  81. ),
  82. ),
  83. );
  84. }
  85. return Scrollbar(
  86. isAlwaysShown: true,
  87. showTrackOnHover: true,
  88. child: SingleChildScrollView(
  89. primary: currentIndex == 1,
  90. child: Column(
  91. children: children,
  92. ),
  93. ),
  94. );
  95. }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement