Flutter DropdownButtonFormField setState在onChanged方法之外无法使用

时间:2020-05-11 14:09:55

标签: flutter

我刚刚用两个DropdownButtonFormField小部件创建了一个简单的flutter应用程序。 如果选择第一个,我想更新两个小部件。

期望:

  • 从第一个下拉菜单中选择号码
  • 相同的数字出现在第二个下拉列表中

结果: -从第一个下拉菜单中选择数字 -仅第一个下拉菜单被更新

有人可以告诉我调用第一个下拉菜单小部件的setState onChaged方法时我在做什么吗?

    import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  List<int> myList = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
  int currentNumber = 1;
  int anotherNumber = 9;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
            DropdownButtonFormField<int>(
              value: currentNumber,
              items: myList.map((int value) {
                return DropdownMenuItem<int>(
                  value: value,
                  child: Container(
                    width: 200,
                    child: Text(
                      value.toString(),
                    ),
                  ),
                );
              }).toList(),
              onChanged: (int newValue) {
                setState(() {
                  currentNumber = newValue;
                  anotherNumber = newValue;
                });
              },
            ),
            DropdownButtonFormField<int>(
              value: anotherNumber,
              items: myList.map((int value) {
                return DropdownMenuItem<int>(
                  value: value,
                  child: Container(
                    width: 200,
                    child: Text(
                      value.toString(),
                    ),
                  ),
                );
              }).toList(),
              onChanged: (int newValue) {
                setState(() {
                  anotherNumber = newValue;
                });
              },
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }}

0 个答案:

没有答案