我是 Flutter 的新手。我正在制作一个用于发布的网站。我正在尝试使用 SingleChildScrollView 的控制器跳转到我的页面。我有一个自定义应用程序栏。自定义应用栏有 4 个按钮。但问题是:自定义应用程序栏位于不同的页面中。我的目的是:当我单击此按钮时。我的 SingleChildScrollView 跳到了重点。如果不使用同一页面,我无法移动它。但我想在不同的页面使用它。
这是我的代码:(自定义应用程序栏页面)
import 'package:flutter/material.dart';
class TopBarContents extends StatefulWidget {
TopBarContents();
@override
_TopBarContentsState createState() => _TopBarContentsState();
}
class _TopBarContentsState extends State<TopBarContents> {
final List _isHovering = [
false,
false,
false,
false,
false,
false,
false,
false
];
static int _counter = 0;
@override
Widget build(BuildContext context) {
double width = MediaQuery.of(context).size.width;
double height = MediaQuery.of(context).size.height;
var screenSize = MediaQuery.of(context).size;
ScrollController scrollController = ScrollController();
void _scroller() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
// final double start = 0;
// final double start = 1350;
final double start = 4050;
scrollController.jumpTo(start);
// scrollController.animateTo(start, duration: Duration(seconds: 1), curve: Curves.easeIn);
});
}
return Container(
color: Colors.white.withOpacity(0.5),
child: Padding(
padding: EdgeInsets.all(20),
child:
Expanded(
child: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: [
SizedBox(width: screenSize.width/6,),
Image.asset("images/logo_hesap.png", width: width/10,),
SizedBox(width: screenSize.width / 15),
InkWell(
onHover: (value) {
setState(() {
value
? _isHovering[0] = true
: _isHovering[0] = false;
});
},
onTap: () {
_scroller();
final double start = 4050;
scrollController.jumpTo(start);
},
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text(
'Ana Sayfa',
style: TextStyle(
color: _isHovering[0]
? Color(0xff000000)
: Color(0xff32b146),
fontWeight: FontWeight.bold,
fontSize: 16
),
),
主页面:(页面只有scrollview和其他小部件)
SingleChildScrollView(
controller: scrollController,)
通过在第二个页面的类中调用第一个页面的类来在两个页面之间传递值,并且需要传递的值参数。然后,在第二个类中的类中,创建一个等于要传递的值的值,然后您可以在您的按钮中使用它。