【问题标题】:How to change background image on tap on a drop-down list?如何在点击下拉列表时更改背景图像?
【发布时间】:2018-10-24 13:35:41
【问题描述】:

我想在点击下拉列表时更改背景图像,我将点击 BG 1 并且图像会改变,我如何访问下拉列表的单个元素?这是我的代码,我该怎么做?

    new Column( children: <Widget>[
            new Padding(
                padding: new EdgeInsets.fromLTRB(100.0, 350.0, 100.0, 50.0)),
            new DropdownButton<String>(
              onChanged: (String value) {
                setState(() {
                  return new Container(
            padding: const EdgeInsets.fromLTRB(100.0, 10.0, 100.0, 00.0),
            decoration: BoxDecoration(
              image: DecorationImage(
                image: new AssetImage('asset/bg.png'),
                alignment: Alignment.topRight,
              ),
            ),
          );
                });
              },
              hint: new Text('Select Type'),
              value: selectedValues,
              items: <String>[
                "BG 1",
                "BG 2",
              ].map((String value) {
                return new DropdownMenuItem<String>(
                  value: value,
                  child: new Text(value),
                );
              }).toList(),
            ),

          ],),
        ImageRotate(),
      ]),
    );
  }
}

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    您可以创建一个变量来保存 DropdownButton 所选项目的值并创建一个 AssetImage 列表。我希望下面的例子能澄清你的想法。

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Firebase Auth Demo',
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
    
      List<String> _locations = ['A', 'B'];
      int imageindex = 0;
      String _selcted = 'A';
      List<ImageProvider> bg = [ AssetImage("images/p1.png"), AssetImage("images/p2.jpg")];
    
      @override
      Widget build(BuildContext context) {
    
        return Scaffold(
          appBar: new AppBar(
            title: new Text("cds"),
          ),
          body:  Container(
            height: double.infinity,
            width: double.infinity,
            decoration: BoxDecoration(
              image: DecorationImage(image: bg[imageindex])
            ),
            child: Center(
              child:new DropdownButton<String>(
                items: _locations.map((String value) {
                  return new DropdownMenuItem<String>(
                    value: value,
                    child: new Text(value),
                  );
                }).toList(),
                hint: new Text("csdc"),
                value: _selcted,
                onChanged: (value) {
                  setState(() {
                    _selcted = value;
                    imageindex = _locations.indexOf(value);
                  });
                },
              )
            ),
          ),
        );
      }
    }
    

    【讨论】:

    • 非常感谢,它真的做到了我想要的,再次感谢
    • 接受并支持答案,以便其他人使用完整。
    猜你喜欢
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 1970-01-01
    • 2021-02-21
    • 2014-07-15
    • 1970-01-01
    相关资源
    最近更新 更多