PageView内的GestureDetector在第一页上不起作用

时间:2018-12-08 22:45:46

标签: dart flutter

如果选择Page2,则在点击Page1 / Page3时GestureDetector可以正常工作。 Page3和点击Page2 / Page4的情况相同。 Page4和在Page3上的轻按也可以正常工作,但是一旦选择了Page1,在Page2上的轻按将根本不起作用。我的错误在哪里,或者这仅仅是个错误?

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home:MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatelessWidget {
  MyHomePage({this.title});
  final String title;

  _createBox(String title, Color color) {
    return Center(
      child: GestureDetector(
        onTap: () => print("Tapped on $title."),
        child: Container(
          width: 200,
          height: 200,
          color: color,
          child: Center(
            child: Text(title),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: Center(
        child: Container(
          width: 300,
          height: 300,
          color: Colors.yellow,
          child: PageView(
            controller: PageController(viewportFraction: 0.7),
            children: <Widget>[
              _createBox("Box 1", Colors.green),
              _createBox("Box 2", Colors.blue),
              _createBox("Box 3", Colors.green),
              _createBox("Box 4", Colors.blue),
              _createBox("Box 5", Colors.green),
            ],
          ),
        ),
      ),
    );
  }
}

Screenrecord

0 个答案:

没有答案