2021年3月31日星期三

How I can change a text in a TextFormField when a ComboBox changes (in Flutter)?

I would like show the default user and password for a database when I select the DBMS.

But when I change the dropdown value the user and password is not changed even I have changed its value.

Look the code:

import 'dart:html';    import 'package:flutter/material.dart';    void main() => runApp(MyApp());    class MyApp extends StatelessWidget {    @override    Widget build(BuildContext context) {      final appTitle = 'Test App';        return MaterialApp(        title: appTitle,        home: Scaffold(          appBar: AppBar(            title: Text(appTitle),          ),          body: MyForm(),        ),      );    }  }    class MyForm extends StatefulWidget {    const MyForm({Key? key}) : super(key: key);      @override    MyFormState createState() => MyFormState();  }    class MyFormState extends State<StatefulWidget> {    String _dbms = 'PostgreSQL';    String _user = 'postgres';    String _pasw = 'postgres';      @override    Widget build(BuildContext context) {      return Column(        mainAxisAlignment: MainAxisAlignment.start,        children: <Widget>[          Container(            child: Row(              mainAxisAlignment: MainAxisAlignment.start,              children: [                Text('DBMS'),                Container(                  width: 150.0,                  child: DropdownButton(                    value: _dbms,                    items: <String>['SQLite', 'Firebird', 'MySQL', 'PostgreSQL']                        .map<DropdownMenuItem<String>>((String value) {                      return DropdownMenuItem<String>(                        value: value,                        child: Text(value),                      );                    }).toList(),                    onChanged: (String? newValue) {                      setState(() {                        _dbms = newValue!;                        switch (_dbms) {                          case 'PostgreSQL':                            _user = 'postgres';                            _pasw = 'postgres';                            break;                          case 'MySQL':                            _user = 'root';                            _pasw = '';                            break;                          case 'Firebird':                            _user = 'SYSDBA';                            _pasw = 'masterkey';                            break;                          default:                            _user = '';                            _pasw = '';                            break;                        }                      });                    },                  ),                ),                Text('User'),                Container(                  width: 150.0,                  child: TextFormField(                    initialValue: '$_user',                    onChanged: (value) => _host,                  ),                ),                Text('Password'),                Container(                  width: 150.0,                  child: TextFormField(                    initialValue: _pasw,                    onChanged: (value) => _pasw,                  ),                ),              ],            ),          ),        ],      );    }  }  

I change the value of the properties but it is not reflected on components.

How I can do this?

Thanks.

https://stackoverflow.com/questions/66896398/how-i-can-change-a-text-in-a-textformfield-when-a-combobox-changes-in-flutter April 01, 2021 at 07:05AM

没有评论:

发表评论