0

我想在选择 DBMS 时显示数据库的默认用户和密码。

但是当我更改下拉值时,即使我更改了它的值,用户和密码也不会更改。

看代码:

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

我更改了属性的值,但它没有反映在组件上。

我怎么能做到这一点?

谢谢。

4

0 回答 0