【发布时间】:2021-08-27 13:58:07
【问题描述】:
我用 PageController 做了一个网站来控制一些屏幕的滚动。我制作了一个带有屏幕名称的菜单部分,当按下其中任何一个时,用户将导航到相应的屏幕。
应用程序运行良好。但是我重构了菜单按钮,这样我就可以控制它的样式,并在将来添加动画。
但是当我重构按钮时,我无法传递 PageController 索引,或者 nextPage 函数。
这就是我想到使用提供程序包的原因。但是,我之前没有打包,而且我的设置似乎很复杂。因为我应该将 PageController 状态传递给按钮,并且按钮应该发送带有新编号的 nextPage 函数。
我怎样才能做到这一点?
这是我的代码:
按钮:
class NavigationButton extends StatefulWidget {
const NavigationButton({
Key key,
this.title,
// this.onPressed
}) : super(key: key);
final String title;
// final VoidCallback onPressed;
@override
_NavigationButtonState createState() => _NavigationButtonState();
}
class _NavigationButtonState extends State<NavigationButton> {
bool isHovering = false;
@override
Widget build(BuildContext context) {
return InkWell(
child: Container(
decoration: BoxDecoration(
border: isHovering == true
? Border(right: BorderSide(color: Colors.blueGrey, width: 10))
: Border(right: BorderSide.none)
),
child: Text(
widget.title,
style: TextStyle(
color: Colors.white,
fontSize: 25
)
)
),
onHover: (value) {
setState(() {
isHovering = value;
});
},
onTap: () {}
);
}
}
主要布局:
class MainLayout extends StatefulWidget {
@override
_MainLayoutState createState() => _MainLayoutState();
}
class _MainLayoutState extends State<MainLayout> {
int _index = 0;
int selectedButton;
bool isHovering = false;
PageController pageController = PageController();
final List<String> _sectionsName = [
"Home",
"About",
"Services",
"Portfolio",
"Testimonials",
"News",
"Contact"
];
@override
Widget build(BuildContext context) {
return Scaffold(
extendBodyBehindAppBar: true,
appBar: MediaQuery.of(context).size.width > 760
? null
: AppBar(
elevation: 0.0,
backgroundColor: Colors.transparent
),
drawer: MediaQuery.of(context).size.width < 760 ? DrawerWidget() : null,
body: Stack(
children: [
// MediaQuery.of(context).size.width < 760 ? Container() : DrawerWidget(),
Listener(
onPointerSignal: (pointerSignal) {
if (pointerSignal is PointerScrollEvent) {
if (pointerSignal.scrollDelta.dy > 0) {
if(_index < _sectionsName.length) {
// int newIndex = _index + 1;
// pageController.jumpToPage(newIndex);
pageController.nextPage(
curve: Curves.easeIn, duration: Duration(milliseconds: 500)
);
}
}
else
{
if(_index < _sectionsName.length) {
// int newIndex = _index - 1;
// pageController.jumpToPage(newIndex);
pageController.previousPage(
curve: Curves.easeIn, duration: Duration(milliseconds: 500)
);
}
}
}
},
child: PageView(
controller: pageController,
scrollDirection: Axis.vertical,
physics: NeverScrollableScrollPhysics(),
children: [
HeroSection(),
AboutSection(),
ServicesSection(),
PortfolioSection(),
TestimonialsSection(),
NewsSection(),
ContactSection()
],
onPageChanged: (index) {
_index = index;
}
)
),
MediaQuery.of(context).size.width < 760
? Container()
: Align(
alignment: Alignment.centerRight,
child: Container(
height: 205,
width: 200,
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
for (int index = 0; index < _sectionsName.length; index++)
NavigationButton(title: _sectionsName[index])
]
)
)
)
]
)
);
}
}
提前谢谢...
编辑:
我关注了@EdwynZN 的回答,这很有帮助。但是我必须像这样将索引从 MainLayout 传递给 NavigationButton:
主布局: NavigationButton(title: _sectionsName[index], index: index)
导航按钮: 将此添加到构造函数中:this.index。 而这个类:final int index;
终于:
onTap: () {
controller.animateToPage(widget.index, curve: Curves.easeIn, duration: Duration(milliseconds: 500));
}
我希望有一天这会对某人有所帮助......
【问题讨论】:
-
你到底想完成什么?您希望 NavigationButton 在 PageController 更改时做出反应,并且在那里还有一个 PageController 的实例,以便您可以在 NavigationButton 中使用 nextPage ? (我想使用提供者)
-
感谢您的评论和您的时间...我将 NavigationButton 重构为另一个类,以便我可以设置它的样式。单击该按钮时,应导航到另一个屏幕(页面)。而且,按钮在被点击后应该会改变颜色。
标签: flutter flutter-web flutter-provider