无法在初始化程序中访问实例成员 'context'。扑
The instance member 'context' can't be accessed in an initializer. Flutter
Heelo Community 我正在做一个液体滑动屏幕。该应用程序有 4 个容器,带有一个页面索引。
在最后一个容器上,我添加了一个按钮,当按下时: (){ Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);} 由于某些原因,我的代码抱怨上下文。
我尝试过的东西---->
我尝试创建一个
setState(() {
if(isSelected == true){
print("Selected is true2");
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
}
然后在我的 MaterialButton onPressed 中我有:
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
isSelected = true,
if(isSelected == true){
print("Selected is true1")
}
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
但是当我运行程序按钮不起作用时
我只想在按下 MaterialRawButton 时导航到主屏幕
这是我的代码:
class InformationScreens extends StatefulWidget {
static const String idScreen = "informationScreen";
@override
_WithPages createState() => _WithPages();
}
class _WithPages extends State<InformationScreens> {
int page = 0;
LiquidController liquidController;
UpdateType updateType;
@override
void initState() {
liquidController = LiquidController();
super.initState();
}
final pages = [
Container(),
Container(),
Container(),
Container(),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
height: 600,
child: SingleChildScrollView(
child: Text(
''' this is some text'''
),
),
),
Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
],
),
],
),
),
),
];
Widget _buildDot(int index) {
double selectedness = Curves.easeOut.transform(
max(
0.0,
1.0 - ((page ?? 0) - index).abs(),
),
);
double zoom = 1.0 + (2.0 - 1.0) * selectedness;
return new Container(
width: 25.0,
child: new Center(
child: new Material(
color: Colors.blueAccent,
type: MaterialType.circle,
child: new Container(
width: 8.0 * zoom,
height: 8.0 * zoom,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: <Widget>[
LiquidSwipe(
pages: pages,
positionSlideIcon: 0.8,
slideIconWidget: Icon(Icons.arrow_back_ios),
onPageChangeCallback: pageChangeCallback,
waveType: WaveType.liquidReveal,
liquidController: liquidController,
ignoreUserGestureWhileAnimating: true,
),
Padding(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Expanded(child: SizedBox()),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List<Widget>.generate(pages.length, _buildDot),
),
],
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(25.0),
child: FlatButton(
onPressed: () {
liquidController.animateToPage(
page: pages.length - 1, duration: 700);
},
child: Text("Skip to End"),
color: Colors.white.withOpacity(0.01),
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: const EdgeInsets.all(25.0),
child: FlatButton(
onPressed: () {
liquidController.jumpToPage(page: liquidController.currentPage + 1 >
pages.length - 1
? 0
: liquidController.currentPage + 1);
},
child: Text("Next"),
color: Colors.white.withOpacity(0.01),
),
),
),
],
),
),
);
}
pageChangeCallback(int lpage) {
setState(() {
page = lpage;
});
}
}
除非您知道自己在做什么,否则不要将小部件存储在变量中。它只会制造更多你无法理解的问题。要么使它们像您拥有的 _buildDot
方法一样,要么直接将其插入 build
.
使用 getter 方法的示例:
List<Widget> get pages => [
Container(),
Container(),
Container(),
Container(),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
height: 600,
child: SingleChildScrollView(
child: Text(
''' this is some text'''
),
),
),
Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
],
),
],
),
),
),
];
Heelo Community 我正在做一个液体滑动屏幕。该应用程序有 4 个容器,带有一个页面索引。 在最后一个容器上,我添加了一个按钮,当按下时: (){ Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);} 由于某些原因,我的代码抱怨上下文。
我尝试过的东西----> 我尝试创建一个
setState(() {
if(isSelected == true){
print("Selected is true2");
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
}
然后在我的 MaterialButton onPressed 中我有:
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
isSelected = true,
if(isSelected == true){
print("Selected is true1")
}
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
但是当我运行程序按钮不起作用时
我只想在按下 MaterialRawButton 时导航到主屏幕
这是我的代码:
class InformationScreens extends StatefulWidget {
static const String idScreen = "informationScreen";
@override
_WithPages createState() => _WithPages();
}
class _WithPages extends State<InformationScreens> {
int page = 0;
LiquidController liquidController;
UpdateType updateType;
@override
void initState() {
liquidController = LiquidController();
super.initState();
}
final pages = [
Container(),
Container(),
Container(),
Container(),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
height: 600,
child: SingleChildScrollView(
child: Text(
''' this is some text'''
),
),
),
Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
],
),
],
),
),
),
];
Widget _buildDot(int index) {
double selectedness = Curves.easeOut.transform(
max(
0.0,
1.0 - ((page ?? 0) - index).abs(),
),
);
double zoom = 1.0 + (2.0 - 1.0) * selectedness;
return new Container(
width: 25.0,
child: new Center(
child: new Material(
color: Colors.blueAccent,
type: MaterialType.circle,
child: new Container(
width: 8.0 * zoom,
height: 8.0 * zoom,
),
),
),
);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Stack(
children: <Widget>[
LiquidSwipe(
pages: pages,
positionSlideIcon: 0.8,
slideIconWidget: Icon(Icons.arrow_back_ios),
onPageChangeCallback: pageChangeCallback,
waveType: WaveType.liquidReveal,
liquidController: liquidController,
ignoreUserGestureWhileAnimating: true,
),
Padding(
padding: EdgeInsets.all(20),
child: Column(
children: <Widget>[
Expanded(child: SizedBox()),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: List<Widget>.generate(pages.length, _buildDot),
),
],
),
),
Align(
alignment: Alignment.bottomRight,
child: Padding(
padding: const EdgeInsets.all(25.0),
child: FlatButton(
onPressed: () {
liquidController.animateToPage(
page: pages.length - 1, duration: 700);
},
child: Text("Skip to End"),
color: Colors.white.withOpacity(0.01),
),
),
),
Align(
alignment: Alignment.bottomLeft,
child: Padding(
padding: const EdgeInsets.all(25.0),
child: FlatButton(
onPressed: () {
liquidController.jumpToPage(page: liquidController.currentPage + 1 >
pages.length - 1
? 0
: liquidController.currentPage + 1);
},
child: Text("Next"),
color: Colors.white.withOpacity(0.01),
),
),
),
],
),
),
);
}
pageChangeCallback(int lpage) {
setState(() {
page = lpage;
});
}
}
除非您知道自己在做什么,否则不要将小部件存储在变量中。它只会制造更多你无法理解的问题。要么使它们像您拥有的 _buildDot
方法一样,要么直接将其插入 build
.
使用 getter 方法的示例:
List<Widget> get pages => [
Container(),
Container(),
Container(),
Container(),
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 20),
height: 600,
child: SingleChildScrollView(
child: Text(
''' this is some text'''
),
),
),
Column(
children: <Widget>[
Container(
padding: EdgeInsets.fromLTRB(20, 10, 20, 40),
child: RawMaterialButton(
onPressed: () => {
Navigator.pushNamedAndRemoveUntil(context, MainScreen.idScreen, (route) => false);
displayToastMessage("you are logged-in now.", context);
},
fillColor: Colors.blueAccent,
splashColor: Colors.blue,
child: Padding(
padding: EdgeInsets.all(10.0),
child: Row(
mainAxisSize: MainAxisSize.min,
children: const <Widget>[
SizedBox(
width: 20.0,
),
Text(
"Accept EULA",
maxLines: 1,
style: TextStyle(color: Colors.white),
),
Icon(
Icons.arrow_forward,
color: Colors.white,
),
],
),
),
shape: const StadiumBorder(),
),
),
],
),
],
),
),
),
];