1 [这是我的页面工作方式。我想要勾选复选框并获得后付费号码,然后点击继续按钮。在我的代码中,复选框位于文本和文本表单字段之间为什么我在这里看不到我的复选框,这是我的代码。]
new Container(
padding: const EdgeInsets.all(40.0),
child: new Form(
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Text(
"Are you a post paid customer",
style: new TextStyle(
color: Colors.blue,
fontSize: 25.0,
),
),
new Checkbox(
activeColor: Colors.blue,
value: _isChecked,
onChanged: (bool val){
onChanged(val);}
),
new TextFormField(
decoration: new InputDecoration(
labelText: "Post Paid Number",
),
obscureText: true,
keyboardType: TextInputType.text,
),
new Padding(
padding: const EdgeInsets.only(top: 60.0),
),
new MaterialButton(
height: 50.0,
minWidth: 150.0,
color: Colors.blue,
splashColor: Colors.blue,
textColor: Colors.white,
child: new Text("Continue"),
onPressed: () {
Navigator.push(
context,
new MaterialPageRoute(builder: (context) => new RegPage()),
);
},
),
答案 0 :(得分:1)
重新创建代码并使其正常运行。
您需要更改复选框中的_isChecked
onChanged,如:
onChanged: (val) {
setState(() {
_isChecked = !_isChecked;
});
}),
确保在setState
中执行此操作,否则更改将不会反映在用户界面上。
我的代码:
import 'package:flutter/material.dart';
import 'package:so_demo/sample_page.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
MyHomePageState createState() {
return new MyHomePageState();
}
}
class MyHomePageState extends State<MyHomePage> {
bool _isChecked = false;
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('SO HELP'),
),
body: new Container(
padding: const EdgeInsets.all(40.0),
child: new Form(
child: new Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
new Text(
"Are you a post paid customer",
style: new TextStyle(
color: Colors.blue,
fontSize: 25.0,
),
),
new Checkbox(
activeColor: Colors.blue,
value: _isChecked,
onChanged: (val) {
setState(() {
_isChecked = !_isChecked;
});
}),
new TextFormField(
decoration: new InputDecoration(
labelText: "Post Paid Number",
),
obscureText: true,
keyboardType: TextInputType.text,
),
new Padding(
padding: const EdgeInsets.only(top: 60.0),
),
new MaterialButton(
height: 50.0,
minWidth: 150.0,
color: Colors.blue,
splashColor: Colors.blue,
textColor: Colors.white,
child: new Text("Continue"),
onPressed: () {
Navigator.of(context).push(new MaterialPageRoute(builder: (context) => new RegPage());
},
),
]),
),
),
);
}
}