颤振:在堆栈中轻拍

时间:2020-10-30 00:35:51

标签: flutter

在颤抖的情况下,我的脚仍然湿透了,所以我希望这很容易解决

我有一段代码将加载JSON提要,将其放在一列中并显示各个项目。我想要一个onTap来将屏幕重定向到详细信息屏幕。 我想打这样的电话:

onTap: (){ showVehiclePage(context, snapshot.data.vehicles[index]); },

挑战是,我不知道如何使它工作。我无法将onTap放置在任何地方,并且尝试使用InkWell,但是我没有弄清楚如何正确放置它。我还是有点扑朔迷离,但是当我点击堆栈上的子项时,如何调用showVehiclePage函数?

class browseRC extends StatelessWidget {
  final Future<Vehicles> vehicles;

  browseRC({Key key, this.vehicles}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Browse RCs',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: Center(
          child: FutureBuilder<Vehicles>(
            future: vehicles,
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                ListView.builder(
                  itemCount: snapshot.data.vehicles.length,
                  itemBuilder: (context, index) {
                    //Vehicle thisVeh = snapshot.data.vehicles[index];
                    return Column(
                      children: <Widget>[
                        Stack(
                        children: <Widget>[
                          FadeInImage.memoryNetwork(
                            placeholder: kTransparentImage,
                            image: '${snapshot.data.vehicles[index].defImage}',
                            height: 250,
                            fit: BoxFit.cover,
                          ),
                          Container(
                            padding: EdgeInsets.all(5.0),
                            margin: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
                            //width: 250,
                            height: 250,
                            alignment: Alignment.bottomCenter,
                            decoration: BoxDecoration(
                              gradient: LinearGradient(
                                begin: Alignment.topCenter,
                                end: Alignment.bottomCenter,
                                colors: <Color>[
                                  Colors.black.withAlpha(0),
                                  Colors.black12,
                                  Colors.black87
                                ],
                              ),
                            ),

                            child: Text(
                              '${snapshot.data.vehicles[index].title}',
                              style: TextStyle(
                                  color: Colors.white, fontSize: 20.0),
                            ),
                          )
                        ])
                      ],
                    );
                  },
                );
              } else if (snapshot.hasError) {
                return Text("${snapshot.error}");
              }
              // By default, show a loading spinner.
              return CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }

  void showVehiclePage(BuildContext context, Vehicle vehicle) {
    Navigator.push(
        context,
        new MaterialPageRoute<Null>(
            settings: const RouteSettings(name: VehiclePage.routeName),
            builder: (BuildContext context) => new VehiclePage(vehicle)));
  }
}

2 个答案:

答案 0 :(得分:1)

您应该看看GestureDetector小部件。

https://api.flutter.dev/flutter/widgets/GestureDetector-class.html

您可以像这样用此小部件包装ContainerImage,然后按需要使用onTap方法。

  GestureDetector(
    onTap: () {
      showVehiclePage(context, snapshot.data.vehicles[index]);
    },
    child: Container(,
      child: OtherWidget(),
    ),
  ),

我希望这给您一个想法,尝试在代码中进行类似的操作。

答案 1 :(得分:1)

您可以在下面复制粘贴运行完整代码
步骤1:您可以检查ConnectionState
步骤2:您可以使用InkWell包装Column
代码段

 return ListView.builder(
                        itemCount: snapshot.data.vehicles.length,
                        itemBuilder: (context, index) {
                          //Vehicle thisVeh = snapshot.data.vehicles[index];
                          return InkWell(
                            onTap: () {
                              showVehiclePage(
                                  context, snapshot.data.vehicles[index]);
                            },
                            child: Column(
                              children: <Widget>[

工作演示

enter image description here

完整代码

import 'package:flutter/material.dart';
import 'package:transparent_image/transparent_image.dart';

class Vehicles {
  List<Vehicle> vehicles;
  Vehicles({this.vehicles});
}

Future<Vehicles> getVehicles() {
  var a = [
    Vehicle(title: "a", defImage: "https://picsum.photos/250?image=9"),
    Vehicle(title: "b", defImage: "https://picsum.photos/250?image=10")
  ];
  print("return");
  return Future.value(Vehicles(vehicles: a));
}

class browseRC extends StatelessWidget {
  final Future<Vehicles> vehicles;

  browseRC({Key key, this.vehicles}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Browse RCs',
        theme: ThemeData(
          primarySwatch: Colors.blue,
        ),
        home: Scaffold(
            body: Center(
          child: FutureBuilder<Vehicles>(
              future: vehicles,
              builder: (context, snapshot) {
                switch (snapshot.connectionState) {
                  case ConnectionState.none:
                    return Text('none');
                  case ConnectionState.waiting:
                    return Center(child: CircularProgressIndicator());
                  case ConnectionState.active:
                    return Text('');
                  case ConnectionState.done:
                    if (snapshot.hasError) {
                      return Text(
                        '${snapshot.error}',
                        style: TextStyle(color: Colors.red),
                      );
                    } else {
                      return ListView.builder(
                        itemCount: snapshot.data.vehicles.length,
                        itemBuilder: (context, index) {
                          //Vehicle thisVeh = snapshot.data.vehicles[index];
                          return InkWell(
                            onTap: () {
                              showVehiclePage(
                                  context, snapshot.data.vehicles[index]);
                            },
                            child: Column(
                              children: <Widget>[
                                Stack(children: <Widget>[
                                  FadeInImage.memoryNetwork(
                                    placeholder: kTransparentImage,
                                    image:
                                        '${snapshot.data.vehicles[index].defImage}',
                                    height: 250,
                                    fit: BoxFit.cover,
                                  ),
                                  Container(
                                    padding: EdgeInsets.all(5.0),
                                    margin:
                                        EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 5.0),
                                    //width: 250,
                                    height: 250,
                                    alignment: Alignment.bottomCenter,
                                    decoration: BoxDecoration(
                                      gradient: LinearGradient(
                                        begin: Alignment.topCenter,
                                        end: Alignment.bottomCenter,
                                        colors: <Color>[
                                          Colors.black.withAlpha(0),
                                          Colors.black12,
                                          Colors.black87
                                        ],
                                      ),
                                    ),

                                    child: Text(
                                      '${snapshot.data.vehicles[index].title}',
                                      style: TextStyle(
                                          color: Colors.white, fontSize: 20.0),
                                    ),
                                  )
                                ])
                              ],
                            ),
                          );
                        },
                      );
                    }
                }
              }),
        )));
  }

  void showVehiclePage(BuildContext context, Vehicle vehicle) {
    Navigator.push(
        context,
        MaterialPageRoute<Null>(
            builder: (BuildContext context) => VehiclePage(vehicle)));
  }
}

class Vehicle {
  String defImage;
  String title;

  Vehicle({this.title, this.defImage});
}

class VehiclePage extends StatelessWidget {
  final Vehicle vehicle;
  VehiclePage(this.vehicle);

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
          appBar: AppBar(
            title: Text("${vehicle.title}"),
          ),
          body: Column(
            children: [
              Text("${vehicle.title}"),
              FadeInImage.memoryNetwork(
                placeholder: kTransparentImage,
                image: '${vehicle.defImage}',
                height: 250,
                fit: BoxFit.cover,
              ),
            ],
          )),
    );
  }
}

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      home: browseRC(
        vehicles: getVehicles(),
      ),
    );
  }
}