点击图片样式应该更改的图像或图像应该在颤动中更改

问题描述 投票:0回答:1

我在2列中有4个图像,当我点击一个图像时,它的样式应该像颜色一样改变,阴影应该改变,或者图像应该被其他图像替换。点击该图像后,其他图像应保持不变。它应该像单选按钮一样工作。怎么做?请帮助我,提前谢谢。

final img_rowi= Center(child:
new Container(
  color:   Colors.transparent,

  padding: const EdgeInsets.all(5.0),
  child: Row(
    mainAxisAlignment: MainAxisAlignment.center,
    children: <Widget>[
      Padding(padding: const EdgeInsets.all(3.0),child: Stack(

        alignment: Alignment.center,
        children: <Widget>[
          svgIcon,new GestureDetector(
            onTap: (){
              setState(() {
                pressed = !pressed;
              });

            },
            child:
          Container(

            child: new Column(
              children: <Widget>[
                new Container(

                  child: new Image.asset(
                    'images/sheep_female.png',
                    height: 50.0,
                    fit: BoxFit.cover,
                  ),
                ),
                new Container(
                  child: new Text('Sheep',style: pressed
                      ? TextStyle(color: const Color(0xFFCDCDCD),fontFamily: 'Montserrat',
                   )
                      : TextStyle(color:Colors.black,fontFamily: 'Montserrat',
                   ),),
                ),
              ],
            ),
            ),
          ),
        ],
      ),),

      Padding(padding: const EdgeInsets.all(3.0),child:
      Stack(
        alignment: Alignment.center,
        children: <Widget>[
          svgIcon,new GestureDetector(
            onTap: (){
              setState(() {
                pressed1 = !pressed1;
              });

            },
            child:
          Container(
            child: new Column(
              children: <Widget>[
                new Container(
                  child: new Image.asset(
                    'images/biily_doe.png',
                    height: 50.0,
                    fit: BoxFit.cover,
                  ),
                ),
                new Container(
                  child: new Text('Billy Doe',style: pressed1
                      ? TextStyle(color: const Color(0xFFCDCDCD),fontFamily: 'Montserrat',
                  )
                      : TextStyle(color:Colors.black,fontFamily: 'Montserrat',
                  ),),
                ),
              ],
            ),
            ),
          ),
        ],
      ),),

    ],
  ),
),

);
dart flutter
1个回答
0
投票

将Image的初始属性存储在变量中。例如,如果我想将FlutterLogo小部件的初始颜色设置为Colors.blue,则在类中声明一个状态。然后用GestureDetector小部件包装你的图像并设置onTap属性。现在调用setState方法并更改其中的所有变量(Image的属性)。

下面是一个示例,其中有一个FlutterLogo小部件,我将该小部件的初始颜色设置为Colors.blue,当我点​​击它时,FlutterLogo小部件的颜色更改为Colors.green。如果我再次点击它,如果颜色是Colors.green然后它改变颜色到Colors.yellow,依此类推。您可以对图像执行类似操作,并更改其大小,可见性和其他属性。

还有imagePath变量存储初始资产的路径,当用户点击Image.asset中的第二个小部件(Column)时,变量imagePath的值被更改,build方法再次被调用并且图像被替换。

import 'package:flutter/material.dart';

void main() => runApp(MaterialApp(home: MyApp()));

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  bool visibility;
  Color colorOfFlutterLogo = Colors.blue;
  String imagePath1 = "assets/initial-path-of-image-1";
  String imagePath2 = "assets/initial-path-of-image-2";

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          backgroundColor: Colors.black,
        ),
        body: Column(
          children: <Widget>[
            GestureDetector(
              onTap: () => setState(() {
                    if (colorOfFlutterLogo == Colors.blue)
                      colorOfFlutterLogo = Colors.green;
                    else if (colorOfFlutterLogo == Colors.green)
                      colorOfFlutterLogo = Colors.yellow;
                    else if (colorOfFlutterLogo == Colors.yellow)
                      colorOfFlutterLogo = Colors.blue;
                  }),
              child: FlutterLogo(
                size: double.infinity,
                colors: colorOfFlutterLogo,
              ),
            ),

            // Image 1
            GestureDetector(
              onTap: () => setState(() {
                    imagePath2 = "assets/new-path-for-image-2";
                  }),
              child: Image.asset(imagePath1),
            ),

            // Image 2
            GestureDetector(
              onTap: () => setState(() {
                    imagePath1 = "assets/new-path-for-image-1";
                  }),
              child: Image.asset(imagePath2),
            )
          ],
        ));
  }
}

© www.soinside.com 2019 - 2024. All rights reserved.