Flutter 颤振-对齐彼此相邻的元件(柱状)

Flutter 颤振-对齐彼此相邻的元件(柱状),flutter,alignment,spacing,Flutter,Alignment,Spacing,我正在开发一个应用程序在颤振,我想使文本,组合框和文本字段垂直对齐,并有相同的宽度。我还想在它们之间留一些空间,这样它们就不会粘在一起了。此外,文本字段中不显示初始值。在颤振中如何做 代码如下: import 'dart:math'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; import 'dart:convert'; import 'package:graphic/grap

我正在开发一个应用程序在颤振,我想使文本,组合框和文本字段垂直对齐,并有相同的宽度。我还想在它们之间留一些空间,这样它们就不会粘在一起了。此外,文本字段中不显示初始值。在颤振中如何做

代码如下:

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:graphic/graphic.dart' as graphic;

import 'main.dart';
import 'gold.dart';

import 'dart:math';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'package:graphic/graphic.dart' as graphic;

import 'main.dart';
import 'gold.dart';

class Convert extends StatefulWidget {
  Convert({Key key, this.title = 'We Earn Finance'}) : super(key: key);

  // This widget is the home page of your application. It is stateful, meaning
  // that it has a State object (defined below) that contains fields that affect
  // how it looks.

  // This class is the configuration for the state. It holds the values (in this
  // case the title) provided by the parent (in this case the App widget) and
  // used by the build method of the State. Fields in a Widget subclass are
  // always marked "final".

  final String title;

  @override
  _ConvertState createState() => _ConvertState();
}

class _ConvertState extends State<Convert>
{

  List<String> currencies = [
    "USD", "EUR", "TRY", 'CAD', 'GBP', 'AUD', 'JPY', 'CHF', 'AED', 'QAR', 'BGN', 'DKK', 'SAR', 'CNY', 'RUB', 'NOK', 'SEK'
  ];
  String selectedCurrency1;
  String selectedCurrency2;
  double price;
  TextEditingController _controller1 = new TextEditingController();
  TextEditingController _controller2 = new TextEditingController();

  double round_to_2(double d)
  {
    double fac = pow(10.0, 3);
    double x = d * fac/10.0;
    fac = pow(10.0, 2);
    return (x).round() / fac;
  }

  onChangeDropdownItem1(String selectedCurrency) {

    selectedCurrency1 = selectedCurrency;
    if(selectedCurrency1 != selectedCurrency2)
    {
      fetchPairValue();
    }
    else {
      setState(() {
        price = 1.00;
        _controller1.text = '1.00';
        _controller2.text = '1.00';
      });
    }
  }

  onChangeDropdownItem2(String selectedCurrency) {
    setState(() {
      selectedCurrency2 = selectedCurrency;
      if(selectedCurrency1 != selectedCurrency2)
      {
        fetchPairValue();
      }
      else {
        setState(() {
          price = 1.00;
          _controller1.text = '1.00';
          _controller2.text = '1.00';
        });
      }
    });
  }

  void _onItemTapped(int index) {

    if(index == 0)
    {
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (BuildContext context) => MyHomePage(),
          ));
    }

    else if(index == 1)
    {
      Navigator.pushReplacement(
          context,
          MaterialPageRoute(
            builder: (BuildContext context) => goldRate(),
          ));
    }
  }

  void fetchPairValue() async
  {
    final response = await http.get('https://www.currencyconverterrate.com/' +
        selectedCurrency1 +
        '/' +
        selectedCurrency2 +
        '.html');

    if (response.statusCode == 200) {
      // If the call to the server was successful, parse the JSON

      String htmlToParse = response.body;
      int idx1 = htmlToParse.indexOf("Bid Price") + 11; //
      int idx2 = htmlToParse.indexOf("Ask Price", idx1) + 11;

      int idx3 = htmlToParse.indexOf("<", idx1);
      int idx4 = htmlToParse.indexOf("<", idx2);

      setState(() {
        price = round_to_2((double.parse(htmlToParse.substring(idx1, idx3)) + double.parse(htmlToParse.substring(idx2, idx4)))/2.0);
        _controller1.text = '1.00';
        _controller2.text = price.toString();
      });
    } else {
      // If that call was not successful, throw an error.
      throw Exception('Failed to load post');
    }
  }

  @override
  void initState()
  {
    super.initState();
    selectedCurrency1 = "USD";
    selectedCurrency2 = "EUR";
    fetchPairValue();
  }

  onFieldChanged1(String value)
  {
    setState(() {
      _controller2.text = round_to_2(price*double.parse(value)).toString();
    });
  }

  onFieldChanged2(String value)
  {
    setState(() {
    _controller1.text = round_to_2(double.parse(value)/price).toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        // Here we take the value from the MyHomePage object that was created by
        // the App.build method, and use it to set our appbar title.

        title: Row(children: <Widget>[
          Image.asset(
            'images/logobig.png',
            width: 40.0,
            height: 40.0,
          ),
          Text(widget.title),
        ]),
        backgroundColor: Colors.blue,
      ),
      body:  Column(children: <Widget>[
            Row(children: [
            Text(
            "Select First Symbol",
            textScaleFactor: 1,
            style: TextStyle(fontWeight: FontWeight.bold),
          ),
              Text(
                "Select Second Symbol",
                textScaleFactor: 1,
                style: TextStyle(fontWeight: FontWeight.bold),
              )]),
        Row(children: [
        DropdownButton(
            value: selectedCurrency1,
            onChanged: (newValue) {
              onChangeDropdownItem1(newValue);
            },
            items: currencies.map((currency) {
              return DropdownMenuItem(
                child: new Text(currency),
                value: currency,
              );
            }).toList(),),
          DropdownButton(
            value: selectedCurrency2,
            onChanged: (newValue) {
              onChangeDropdownItem2(newValue);
            },
            items: currencies.map((currency) {
              return DropdownMenuItem(
                child: new Text(currency),
                value: currency,
              );
            }).toList(),)]),
        Row(children: [
        Container(
        width: 100, // do it in both Container
    child:
          TextField(keyboardType: TextInputType.numberWithOptions(decimal: true),
                    controller: _controller1,
                    onChanged:(value) {
                      onFieldChanged1(value);
                    }, )),
    Container(
    width: 100, // do it in both Container
    child:
          TextField(keyboardType: TextInputType.numberWithOptions(decimal: true),
                    controller: _controller2,
                    onChanged: (value) {
                      onFieldChanged2(value);
                    }, ))
        ],)
          ]),

      bottomNavigationBar: BottomNavigationBar(
        items: <BottomNavigationBarItem>[
          BottomNavigationBarItem(
            icon: Image.asset('images/dollar_world_grid.png',
              width: 46.0,
              height: 46.0,
            ),
            label: 'Currency',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              'images/gold-bars.png',
              width: 46.0,
              height: 46.0,
            ),
            label: 'Gold',
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              'images/curr_conv1_selected.png',
              width: 46.0,
              height: 46.0,
            ),
            label: 'Convert',
          )
        ],
        currentIndex: 2,
        unselectedItemColor: Color.fromRGBO(127, 127, 127, 0.4),
        selectedItemColor: Color.fromRGBO(43, 73, 193, 0.4),
        onTap: _onItemTapped,
      ),);
  }
}
import'dart:math';
进口“包装:颤振/材料.省道”;
将“package:http/http.dart”导入为http;
导入“dart:convert”;
导入“包装:图形/图形.省道”作为图形;
导入“main.dart”;
进口“黄金飞镖”;
导入“dart:math”;
进口“包装:颤振/材料.省道”;
将“package:http/http.dart”导入为http;
导入“dart:convert”;
导入“包装:图形/图形.省道”作为图形;
导入“main.dart”;
进口“黄金飞镖”;
类转换扩展StatefulWidget{
Convert({Key-Key,this.title='We-Earn-Finance'}):super(Key:Key);
//此小部件是应用程序的主页。它是有状态的,表示
//它有一个状态对象(定义如下),其中包含影响
//看起来怎么样。
//此类是状态的配置。它保存值(在此
//案例名称)由家长(在本例中为应用程序小部件)提供,以及
//由State的build方法使用。小部件子类中的字段包括
//始终标记为“最终”。
最后的字符串标题;
@凌驾
_ConvertState createState()=>\u ConvertState();
}
类_ConvertState扩展了状态
{
列出货币=[
“美元”、“欧元”、“土耳其里拉”、“加元”、“英镑”、“澳元”、“日元”、“瑞士法郎”、“AED”、“卡塔尔币”、“BGN”、“丹麦克朗”、“特区币”、“人民币”、“卢布”、“挪威克朗”、“瑞典克朗”
];
字符串selectedCurrency1;
字符串selectedCurrency2;
双倍价格;
TextEditingController_controller1=新的TextEditingController();
TextEditingController_controller2=新的TextEditingController();
双圆_至_2(双d)
{
双fac=功率(10.0,3);
双x=d*fac/10.0;
fac=pow(10.0,2);
返回(x).round()/fac;
}
OnChangedDropDownItem1(字符串选择的货币){
selectedCurrency1=selectedCurrency;
如果(selectedCurrency1!=selectedCurrency2)
{
fetchPairValue();
}
否则{
设置状态(){
价格=1.00;
_controller1.text='1.00';
_controller2.text='1.00';
});
}
}
OnChangedDropDownItem2(字符串选择的货币){
设置状态(){
selectedCurrency2=selectedCurrency;
如果(selectedCurrency1!=selectedCurrency2)
{
fetchPairValue();
}
否则{
设置状态(){
价格=1.00;
_controller1.text='1.00';
_controller2.text='1.00';
});
}
});
}
void\u未映射(整数索引){
如果(索引==0)
{
导航器。更换(
上下文
材料路线(
生成器:(BuildContext context)=>MyHomePage(),
));
}
else if(索引==1)
{
导航器。更换(
上下文
材料路线(
生成器:(BuildContext上下文)=>goldRate(),
));
}
}
void fetchPairValue()异步
{
最终响应=等待http.get('https://www.currencyconverterrate.com/' +
选定货币1+
'/' +
选定货币2+
“.html”);
如果(response.statusCode==200){
//如果对服务器的调用成功,则解析JSON
字符串htmlToParse=response.body;
int idx1=HTMLTOPASE.indexOf(“投标价格”)+11//
int idx2=htmlToParse.indexOf(“要价”,idx1)+11;
int idx3=htmlToParse.indexOf(“尝试使用扩展的:

body: Expanded(
    child: Column(
        children: <Widget>[...]
    )
 )
正文:已展开(
子:列(
儿童:[……]
)
)

要将窗口小部件向左对齐并展开,可以使用:

  Column( 
    mainAxisAlignment: MainAxisaAignment.start,
    crossAxisAlignment: CrossAxisAlignment.stretch,
    children: [//your children]
  )