正如上面的标题,我希望我向下滚动的页面在按下底部导航栏(双击)时向上滚动.我成功了,没问题.但是,如果页面上有一个包含选项卡栏的选项,则会出现问题.
例如
HomePage
和ProfilePage
在主页上
FirstTabBarView
和SecondTabBarView
问题是当我向下滚动这两个页面时(主页和档案页面位于不同的位置)
这只是一个简单的代码示例,您可以复制粘贴并直接在dart pad上测试它
import 'package:flutter/material.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
static const String _title = 'Flutter Code Sample';
@override
Widget build(BuildContext context) {
return const MaterialApp(
title: _title,
home: MyStatefulWidget(),
);
}
}
class MyStatefulWidget extends StatefulWidget {
const MyStatefulWidget({Key? key}) : super(key: key);
@override
State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}
class _MyStatefulWidgetState extends State<MyStatefulWidget> {
final ScrollController _firstTabBarScrollController = ScrollController();
final ScrollController _secondTabBarScrollController = ScrollController();
List<Widget> _widgetOptions = <Widget>[];
int _selectedIndex = 0;
@override
void initState(){
_widgetOptions = <Widget>[
MyHomePage(
firstTabBarScrollController: _firstTabBarScrollController,
secondTabBarScrollController: _secondTabBarScrollController,
title: "Home Page",
),
Text('Profile'),
];
}
void _onItemTapped(int index) {
setState(() {
_selectedIndex = index;
});
if(index == 0 && _firstTabBarScrollController.hasClients){
_firstTabBarScrollController.animateTo(0,
duration: const Duration(milliseconds: 500), curve: Curves.easeOut,);
}
if(index == 0 && _secondTabBarScrollController.hasClients){
_secondTabBarScrollController.animateTo(0,
duration: const Duration(milliseconds: 500), curve: Curves.easeOut,);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: _widgetOptions.elementAt(_selectedIndex),
),
bottomNavigationBar: BottomNavigationBar(
items: const <BottomNavigationBarItem>[
BottomNavigationBarItem(
icon: Icon(Icons.home),
label: 'Home',
),
BottomNavigationBarItem(
icon: Icon(Icons.business),
label: 'Profile',
),
],
currentIndex: _selectedIndex,
selectedItemColor: Colors.amber[800],
onTap: _onItemTapped,
),
);
}
}
class MyHomePage extends StatefulWidget{
final ScrollController firstTabBarScrollController;
final ScrollController secondTabBarScrollController;
final String title;
const MyHomePage({
Key? key,
required this.title,
required this.firstTabBarScrollController,
required this.secondTabBarScrollController,
}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> with TickerProviderStateMixin {
late TabController _tabController;
@override
void initState() {
super.initState();
_tabController = TabController(length: 2, vsync: this);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
bottom: TabBar(
controller: _tabController,
tabs: const <Widget>[
Tab(
icon: Icon(Icons.cloud_outlined),
),
Tab(
icon: Icon(Icons.beach_access_sharp),
),
],
),
),
body: TabBarView(
controller: _tabController,
children: <Widget>[
FirstTabBarView(firstTabScrollController: widget.firstTabBarScrollController),
SecondTabBarView(secondTabScrollController: widget.secondTabBarScrollController),
],
),
);
}
}
class FirstTabBarView extends StatefulWidget{
final ScrollController firstTabScrollController;
const FirstTabBarView({
Key? key,
required this.firstTabScrollController,
}) : super(key: key);
@override
_FirstTabBarView createState() => _FirstTabBarView();
}
class _FirstTabBarView extends State<FirstTabBarView> with AutomaticKeepAliveClientMixin<FirstTabBarView> {
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
body: ListView.builder(
controller: widget.firstTabScrollController,
itemCount: 1000,
itemBuilder: (context, index) {
return ListTile(
title: Text('$index'),
);
},
),
);
}
@override
bool get wantKeepAlive => true;
}
class SecondTabBarView extends StatefulWidget{
final ScrollController secondTabScrollController;
const SecondTabBarView({
Key? key,
required this.secondTabScrollController,
}) : super(key: key);
@override
_SecondTabBarView createState() => _SecondTabBarView();
}
class _SecondTabBarView extends State<SecondTabBarView> with AutomaticKeepAliveClientMixin<SecondTabBarView> {
@override
Widget build(BuildContext context) {
super.build(context);
return Scaffold(
backgroundColor: Colors.yellow,
body: ListView.builder(
controller: widget.secondTabScrollController,
itemCount: 1000,
itemBuilder: (context, index) {
return ListTile(
title: Text('$index'),
);
},
),
);
}
@override
bool get wantKeepAlive => true;
}
注:例如上述代码的情况
-
向下滚动第一个选项卡BarView,
-
打开第二个TabBarView,然后向下滚动第二个TabBarView,
-
双击
Home Icon
按钮 -
You will notice that both the
FirstTabBarView
和SecondTabBarView
pages will scroll up. What I want is only the currently open page to scroll to the top (one of them).