简体   繁体   English

Flutter - 使用 onPressed 更改图像(不在按钮中)

[英]Flutter - image change with onPressed (not in a button)

i'm new in the game of StackOverflow and of flutter and try to write an app for web.我是 StackOverflow 和 flutter 游戏的新手,并尝试为 web 编写应用程序。 Maybe it is a simple problem, but i don't know how to do it.也许这是一个简单的问题,但我不知道该怎么做。 I've the problem, that i don't know how to change a image, which i've displayed.我有问题,我不知道如何更改已显示的图像。

My target is 3 buttons, which can change an image above them.我的目标是 3 个按钮,可以更改它们上方的图像。 One of the images could be displayed or at the begin replaced with a placeholder, it's unimportant.可以显示其中一张图像或在开始时用占位符替换,这并不重要。

Maybe in the code are some unnecessary function, ignore them.可能代码中有一些不必要的function,忽略它们。

It would be very nice, if you could help me.如果你能帮助我,那就太好了。

Image图片

import 'package:flutter/material.dart';

void main() {
  runApp(StarCounter());
}

class StarCounter extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Screen',
      theme: ThemeData(
        primarySwatch: Colors.orange,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: MyHomePage(title: 'Screen'),
    );
  }
}

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

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  var _itemImage = Image.asset("Screenshot1.png");

  tmpFunction() {
    Container(
      child: _itemImage,
      width: 500,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Container(
              child: Placeholder(
                fallbackWidth: 500,
              ),
            ),
            Text(
              'Wählen Sie den gewünschten Button aus:',
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                FlatButton(onPressed: tmpFunction, child: Text("Button 1")),
                FlatButton(onPressed: tmpFunction, child: Text("Button 2")),
                FlatButton(onPressed: tmpFunction, child: Text("Button 3")),
              ],
            ),
          ],
        ),
      ),
    );
  }
}


Each button will change image you can change user asset image or network image每个按钮都会改变图像你可以改变用户资产图像或网络图像

import 'package:flutter/material.dart'; 
void main() {
runApp(StarCounter());
}

class StarCounter extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
  title: 'Screen',
  theme: ThemeData(
    primarySwatch: Colors.orange,
    visualDensity: VisualDensity.adaptivePlatformDensity,
  ),
  home: MyHomePage(title: 'Screen'),
  );
  }
  }

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

   final String title;

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

    class _MyHomePageState extends State<MyHomePage> {
       var _itemImage = [
        Image.network("https://i.picsum.photos/id/203/200/300.jpg? 
        hmac=mJaqsySlyEjr8fLBHytyVCUyqlfPSxqXePXEIhZZi_Y"),
        Image.network("https://i.picsum.photos/id/14/200/200.jpg?hmac=bwQwH7- 
        0RPCqUVkFzd3hFhc6yDfC6_e7vgaKXZ7vFOA"),
        Image.network("https://picsum.photos/id/870/200/300?grayscale&blur=2")];

        int index =0;


        tmpFunction() {
        Container(
        child: _itemImage[index],
        width: 500,
        );
        }

       @override
       Widget build(BuildContext context) {
       return Scaffold(
        appBar: AppBar(
        title: Text(widget.title),
       ),
       body: Center(
       child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
       
         Container(
         child: _itemImage[index],
         width: 500,
         ),
        Text(
          'Wählen Sie den gewünschten Button aus:',
        ),
        Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            FlatButton(onPressed: (){
              
             
             setState(() { index = 0;});
            }, child: Text("Button 1")),
            FlatButton(onPressed: (){
               setState(() { index = 1;});
              
            }, child: Text("Button 2")),
            FlatButton(onPressed: (){
               setState(() {  index = 2;});
               
              
            }, child: Text("Button 3")),
          ],
           ),
          ],
         ),
           ),
        );
        }
       }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM