Flutter 无法滚动到颤振中的特定小部件
所以,当用户按下按钮时,我想滚动到一个特定的小部件。为此,我给小部件分配了一个键,然后使用它来获取renderobject,从而移动到该小部件。但是我得到了错误:-Flutter 无法滚动到颤振中的特定小部件,flutter,dart,flutter-layout,Flutter,Dart,Flutter Layout,所以,当用户按下按钮时,我想滚动到一个特定的小部件。为此,我给小部件分配了一个键,然后使用它来获取renderobject,从而移动到该小部件。但是我得到了错误:- NoSuchMethodError: invalid member on null: 'findRenderObject' GlobalKey ourServices= GlobalKey(); GlobalKey ourProducts= GlobalKey(); ScrollController controller = ne
NoSuchMethodError: invalid member on null: 'findRenderObject'
GlobalKey ourServices= GlobalKey();
GlobalKey ourProducts= GlobalKey();
ScrollController controller = new ScrollController();
Container(
height: MediaQuery.of(context).size.height,
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/coverPhoto.jpg'),
fit: BoxFit.cover,
),
),
child: CustomScrollView(
controller: controller,
slivers: [
SliverAppBar(
floating: false,
expandedHeight: MediaQuery.of(context).size.height*1.8/3,
elevation: 0.0,
backgroundColor: Colors.transparent,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text("Heading")
),
),
SliverList(
delegate: SliverChildListDelegate(
[
SizedBox(height: MediaQuery.of(context).size.height/4,),
OurServices(key: ourServices,),
OurProducts(key: ourProducts,),
ContactUs(key: contactUs,),
],
),
),
],
),
),
代码:-
NoSuchMethodError: invalid member on null: 'findRenderObject'
GlobalKey ourServices= GlobalKey();
GlobalKey ourProducts= GlobalKey();
ScrollController controller = new ScrollController();
Container(
height: MediaQuery.of(context).size.height,
width: double.infinity,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/coverPhoto.jpg'),
fit: BoxFit.cover,
),
),
child: CustomScrollView(
controller: controller,
slivers: [
SliverAppBar(
floating: false,
expandedHeight: MediaQuery.of(context).size.height*1.8/3,
elevation: 0.0,
backgroundColor: Colors.transparent,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text("Heading")
),
),
SliverList(
delegate: SliverChildListDelegate(
[
SizedBox(height: MediaQuery.of(context).size.height/4,),
OurServices(key: ourServices,),
OurProducts(key: ourProducts,),
ContactUs(key: contactUs,),
],
),
),
],
),
),
我的滚动按钮中的代码是:-
GestureDetector(
onTap: ()=>controller.position.ensureVisible(
ourServices.currentContext.findRenderObject(),
alignment: 0.5,
duration: const Duration(seconds: 1),
),
child: Text("Services",style: TextStyle(fontSize: 16.0,fontWeight: FontWeight.w500,color: Colors.white),),
),