Last active
December 27, 2019 12:03
-
-
Save Aaron009/2df9288d77b219a9afa718d306a39862 to your computer and use it in GitHub Desktop.
When I scroll to the second page, the tab does not switch automatically? When I click on the tab item, the page view below does not switch automatically? Why?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
import 'dart:math' as math; | |
void main() => runApp(MyApp()); | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
home: Scaffold(body: TestNestedScroll()), | |
); | |
} | |
} | |
class TestNestedScroll extends StatefulWidget { | |
@override | |
_TestNestedScrollState createState() => _TestNestedScrollState(); | |
} | |
class _TestNestedScrollState extends State<TestNestedScroll> | |
with TickerProviderStateMixin { | |
@override | |
Widget build(BuildContext context) { | |
return getTop(); | |
// return getNestedScroll(); | |
} | |
Widget getTop() { | |
List<Widget> expandedList = []; | |
for (var i = 0; i < 100; ++i) { | |
expandedList.add(Text('Expanded Text xxxxxxxxxx')); | |
} | |
return DefaultTextStyle( | |
style: TextStyle(fontSize: 30, color: Colors.blue), | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: <Widget>[ | |
Container( | |
height: MediaQuery.of(context).padding.top, // Status bar color | |
color: Colors.grey, | |
), | |
Text('this is Text'), | |
Flexible( | |
child:SingleChildScrollView( | |
child: Column( | |
mainAxisSize: MainAxisSize.min, | |
children: expandedList, | |
), | |
) | |
), | |
], | |
), | |
); | |
} | |
Widget getNestedScroll() { | |
XCTabBar tabBar = createTabBar( | |
viewList: [ | |
Center(child: Text('page view1')), | |
Center(child: Text('page view2')), | |
Center(child: Text('page view3')), | |
], | |
provider: this, | |
tabStrings: 'tab item1,tab item2,tab item3', | |
); | |
return NestedScrollView( | |
body: tabBar.getTabView(), | |
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) { | |
return <Widget>[ | |
SliverToBoxAdapter( | |
child: getTop(), | |
), | |
SliverPersistentHeader( | |
floating: true, | |
pinned: true, | |
delegate: _SliverAppBarDelegate( | |
maxHeight: 44.0, | |
minHeight: 44.0, | |
child: Container( | |
color: Colors.white, | |
child: tabBar, | |
) | |
), | |
), | |
]; | |
}, | |
); | |
} | |
} | |
class _SliverAppBarDelegate extends SliverPersistentHeaderDelegate { | |
_SliverAppBarDelegate({ | |
@required this.minHeight, | |
@required this.maxHeight, | |
@required this.child, | |
}); | |
final double minHeight; | |
final double maxHeight; | |
final Widget child; | |
@override | |
double get minExtent => minHeight; | |
@override | |
double get maxExtent => math.max((minHeight ?? kToolbarHeight), minExtent); | |
@override | |
Widget build( | |
BuildContext context, double shrinkOffset, bool overlapsContent) { | |
return child; | |
} | |
@override | |
bool shouldRebuild(_SliverAppBarDelegate oldDelegate) { | |
// return maxHeight != oldDelegate.maxHeight || | |
// minHeight != oldDelegate.minHeight || | |
// child != oldDelegate.child; | |
return true; | |
} | |
} | |
//---------------------------------------------------------------------------------------------------------------------------- | |
XCTabBar createTabBar({@required List<Widget> viewList, @required TickerProvider provider, List<Widget> tabList, String tabStrings}) { | |
if(tabList == null && tabStrings != null) { | |
tabList = tabStrings.split(',').map((item) { | |
return Text( | |
'$item', | |
style: TextStyle(fontSize: 15), | |
); | |
}).toList(); | |
} | |
return XCTabBar(viewList, tabList, TabController(vsync: provider, length: tabList.length)); | |
} | |
class XCTabBar extends StatefulWidget { | |
final TabController tabController; | |
final List<Widget> viewList; | |
final List<Widget> tabList; | |
XCTabBar(this.viewList, this.tabList, this.tabController) { | |
print('XCTabBar Constructor'); | |
} | |
@override | |
State<StatefulWidget> createState() { | |
print('XCTabBar createState'); | |
return XCTabBarState(); | |
} | |
TabBarView getTabView() { | |
return TabBarView( | |
children: viewList, | |
controller: tabController, | |
); | |
} | |
} | |
class XCTabBarState extends State<XCTabBar> with SingleTickerProviderStateMixin { | |
Color selectColor, unselectedColor; | |
TextStyle selectStyle, unselectedStyle; | |
List<Widget> _tabList; | |
TabController _tabController; | |
@override | |
void initState() { | |
super.initState(); | |
_tabList = widget.tabList; | |
_tabController = widget.tabController; | |
selectColor = Colors.black; | |
unselectedColor = Color.fromARGB(255, 117, 117, 117); | |
selectStyle = TextStyle(fontSize: 18, color: selectColor, fontWeight: FontWeight.bold); | |
unselectedStyle = TextStyle(fontSize: 18, color: selectColor); | |
} | |
@override | |
void dispose() { | |
_tabController.dispose(); | |
super.dispose(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Container( | |
margin: EdgeInsets.only(top: 0.0, bottom: 0.0), | |
child: TabBar( | |
tabs: _tabList, | |
isScrollable: true, | |
controller: _tabController, | |
indicatorColor: selectColor, | |
labelColor: selectColor, | |
labelStyle: selectStyle, | |
unselectedLabelColor: unselectedColor, | |
unselectedLabelStyle: unselectedStyle, | |
indicatorSize: TabBarIndicatorSize.label, | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I put a fixed Container on the SliverToBoxAdapter and it becomes slidable. Why is this?
This code runs, only the
SingleChildScrollView
area will scroll, the other parts are fixed, but when I put getTop in the SliverToBoxAdapter, it gave various errors. The error is as follows.Seems prompting me to change "extension" to "flexible". Column plus mainAxisSize: MainAxisSize.min.
I followed the above prompt and it was run without reporting any errors, but the "This is text" text component was not pinned and sliding.
Question one:
I want the content in the red box to always be pinned to the top, not sure why it is now sliding.
Question two:
When I scroll to the second page, the tab does not switch automatically? When I click on the tab item, the page view below does not switch automatically? Why?
But I run on dartpad.dev, there is no such problem。
The complete code:
https://dartpad.dev/2df9288d77b219a9afa718d306a39862