在 flutter 中,你可以设置 appbar 背景根据下拉框的值改变吗?

In flutter, can you set the appbar backgorund to change base on the value of a dropdown box?

我的下拉框循环显示 5 个字符串 ['blue','red','yellow','orange','grey']

我希望我的应用栏标题是那个下拉框,并使用下拉列表中的值来确定应用栏颜色

DropDownWidget ddw = DropDownWidget();

var color = {
    "blue": Colors.blue,
    "red": Colors.red,
    "yellow": Colors.yellow,
    "orange": Colors.orange,
    "grey": Colors.grey,
};

@override
Widget build(BuildContext context) {
   return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: ddw,
        backgroundColor: color[ddw],
   ),
}

下拉列表 (ddw) 显示为标题,没问题。

我用这些字符串作为键和相应的颜色作为值创建了一个字典,但我无法使用下拉列表的字符串值来更改背景。

有什么建议吗?

您可以复制粘贴 运行 下面的完整代码
您可以在 DropdownButton onChanged

中调用 setState

代码片段

appBar: AppBar(
    backgroundColor: _appbarColor,
...
DropdownButton<Item>(
                hint: Text("Select item"),
                value: selectedColor,
                onChanged: (Item Value) {
                  setState(() {
                    selectedColor = Value;
                    _appbarColor = selectedColor.color;
                  });
                },

工作演示

完整代码

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class Item {
  const Item(this.name, this.color);
  final String name;
  final Color color;
}

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;
  Color _appbarColor = Colors.blue;
  Item selectedColor;

  List<Item> colorList = <Item>[
    const Item('blue', Colors.blue),
    const Item('red', Colors.red),
    const Item('yellow', Colors.yellow),
  ];

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

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: _appbarColor,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            DropdownButton<Item>(
                hint: Text("Select item"),
                value: selectedColor,
                onChanged: (Item Value) {
                  setState(() {
                    selectedColor = Value;
                    _appbarColor = selectedColor.color;
                  });
                },
                items: colorList.map((Item item) {
                  return DropdownMenuItem<Item>(
                    value: item,
                    child: Row(
                      children: <Widget>[
                        Container(
                          height: 15,
                          width: 15,
                          color: item.color,
                        ),
                        SizedBox(
                          width: 10,
                        ),
                        Text(
                          item.name,
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  );
                }).toList()),
            Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headline4,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ),
    );
  }
}