0

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

我希望我的 appbar 标题是那个下拉框,并希望下拉框中的值确定 appbar 颜色

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)显示为标题,没问题。

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

有什么建议么?

4

1 回答 1

0

您可以在下面复制粘贴运行完整代码
您可以调用setStateonChangedDropdownButton

代码片段

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),
      ),
    );
  }
}
于 2020-02-10T01:57:42.697 回答