【问题标题】:checkbox can not see from the interface flutter从界面flutter看不到checkbox
【发布时间】:2018-10-26 00:17:26
【问题描述】:

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()),
                          );
                        },
                      ),

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    重新创建您的代码并使其正常工作。

    你需要在 Checkbox 的 onChanged 中更改_isChecked 像:

    onChanged: (val) {
      setState(() {
        _isChecked = !_isChecked;
      });
    }),
    

    确保在setState 中执行此操作,否则更改不会反映在 UI 上。

    我的代码:

    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());
                      },
                    ),
                  ]),
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 谢谢你,我明白了,我也必须尝试这种方式顺便说一句,你知道如何更改复选框的边框颜色。
    • 如果您认为此答案正确,请将其标记为正确。我很快就会回复复选框边框颜色
    • 您可以通过更改ThemeData 中的属性unselectedWidgetColor 来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2022-08-06
    • 2023-02-15
    • 2021-07-22
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2015-06-18
    • 2014-07-12
    相关资源
    最近更新 更多