代码之家  ›  专栏  ›  技术社区  ›  Europa

颤振LBS到KG应用程序未注册“转换”提交

  •  0
  • Europa  · 技术社区  · 7 年前

    我已经创建了两个双变量,一个用于 价值观。

    我有一个方法,将磅输入转换成公斤输出。

    但是,当我按下转换按钮时,我的应用程序中什么都没有发生。

    enter image description here

    import 'package:flutter/material.dart';
    
    void main() => runApp(new MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        final appTitle = 'LBS to KG converter';
    
        return MaterialApp(
          title: appTitle,
          home: Scaffold(
            appBar: AppBar(
              title: Text(appTitle),
            ),
            body: MyCustomForm(),
          ),
        );
      }
    }
    
    // Create a Form Widget
    class MyCustomForm extends StatefulWidget {
      @override
      MyCustomFormState createState() {
        return MyCustomFormState();
      }
    }
    
    // Create a corresponding State class. This class will hold the data related to
    // the form.
    class MyCustomFormState extends State<MyCustomForm> {
      final _formKey = GlobalKey<FormState>();
      double _lbs;
      double _kg;
    
      void _convert(){
        setState(() {
          _kg = _lbs * 0.45359237;
        });
    
      }
    
      @override
      Widget build(BuildContext context) {
        return new Form(
    
          key: _formKey,
          child: Column(
            children: <Widget>[
    
              /*- LBS ---------------- */
              new Row(
                children: <Widget>[
                  Flexible(
                    child: Padding(
                      padding: const EdgeInsets.only(
                          top: 2.0, left: 10.0),
                      child: new Text(
                        "LBS:",
                        style: new TextStyle(fontSize: 18.0),
                      )
                    ),
                  ),
                  Flexible(
                    child: Padding(
                      padding: const EdgeInsets.only(
                          top: 2.0, left: 10.0),
                      child: new TextField(decoration: InputDecoration(
                          hintText: '$_lbs'
                      ),)
    
                    ),
                  )
                ],
              ),
    
    
    
              /*- KG ----------------- */
              new Row(
                children: <Widget>[
                  Flexible(
                    child: Padding(
                        padding: const EdgeInsets.only(
                            top: 2.0, left: 10.0),
                        child: new Text(
                          "KG:",
                          style: new TextStyle(fontSize: 18.0),
                        )
                    ),
                  ),
                  Flexible(
                    child: Padding(
                        padding: const EdgeInsets.only(
                            top: 2.0, left: 10.0),
                        child: new TextField(decoration: InputDecoration(
                            hintText: '$_kg'
                        ),)
    
                    ),
                  )
                ],
              ),
    
              /*- Convert ---------------- */
              new Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Flexible(
                    child: Padding(
                      padding: const EdgeInsets.only(
                          top: 15.0, right: 10.0, left: 10.0),
                      child: GestureDetector(
                        onTap: _convert,
                        child: new Container(
                            alignment: Alignment.center,
                            height: 60.0,
                            decoration: new BoxDecoration(
                                color: Color(0xFF18D191),
                                borderRadius: new BorderRadius.circular(9.0)),
                            child: new Text("Convert",
                                style: new TextStyle(
                                    fontSize: 20.0, color: Colors.white))),
                      ),
                    ),
                  )
                ],
              )
            ],
          ),
        );
      }
    }
    

    1) 对我的应用程序缺少什么有什么建议吗?

    2) “hintText”是表示价值的最佳方法吗?没有“TextField.value”吗?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Andrii Turkovskyi    7 年前
    final lbsController = TextEditingController();
    final kgController = TextEditingController();
    
    void _convert(){
    _lbs = lbsController.text; // just add checking for digital input there
      setState(() {
        _kg = _lbs * 0.45359237;
      });
    }
    

    还有里面 build

    lbsController.text = _lbs;
    kgController.text = _kg;
    ...
    child: new TextField(controller: lbsController, ...