Как я могу поместить виджет, который может хорошо работать, в виджет PositionedTransition

Я помещаю AnimationController в виджет PositionedTransition.

И я установил его дочерний элемент на виджет InkWell (или GestureDetector или IconButton), чтобы уловить поведение касания во время анимации.

Но это не работает. Если я нажму на это, ничего не произойдет.

Я хочу знать, зачем и как это делать.

Вместо этого теперь я помещаю виджет GestureDetector в то же положение.

спасибо за чтение и за хороший совет.

import 'package:flutter/material.dart';
import 'dart:math' as math;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: Page0(),
    );
  }
}
class Page0 extends StatefulWidget {
  @override
  Page0State createState() => Page0State();
}
class Page0State extends State<Page0> with SingleTickerProviderStateMixin{

  AnimationController _animationController;
  var _isMoved = false;

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
        duration: const Duration(milliseconds: 2000),
    );
  }
  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {

    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    return Scaffold(
      body: Stack(
        children: <Widget> [
          Container(
            height : 800,
            width  : 800,
            color : Colors.blue,
            child :
              GestureDetector(
                onHorizontalDragEnd: null,  //set no action
              )
          ),
          Positioned(
            top:height/2,
            left:width * 5/6-30,
            child: InkWell(
              onTap: (){debugPrint('a');},
              child : Container(
              height: 50,
              width: 100,
              ),
            ),
          ),
          PositionedTransition(   //This widget!!!
            child:                //I set this child to IconButton
              IconButton(         //But it can't work 
                icon: Icon(
                  Icons.reply,
                  textDirection: TextDirection.rtl),
                  color: Colors.green,
                onPressed: () {
                  debugPrint('bb');  //no debug
                  Navigator.push(    //no push(Actually Page2 class exists.)
                    context,
                    _createNextRoute(Page2()));
                },
              ),
            rect: _animationController
              .drive(
                  CurveTween(
                    curve: Curves.bounceIn,
                  ),
              )
              .drive(
                RelativeRectTween(
                  begin:  RelativeRect.fromLTRB(width * 5/6 -20, height/2 , width / 6 +20, height/2 ),
                  end: RelativeRect.fromLTRB(width * 5/6, height/2, width / 6, height/2),
                )
              ),
          ),
          Positioned(
            top:200,
            left:200,
            child:IconButton(
              icon: Icon(Icons.forward),
              onPressed: () {debugPrint('ccc');},  //this debug works well.
            ),
          ),
        ]
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          if (_isMoved) {
            _animationController.repeat();
          } else {
            _animationController.reset();
            _animationController.forward();
          }
          _isMoved = !_isMoved;
        },
        child:
          Transform.rotate(
            angle: math.pi, // 45 deg
            child:Icon(Icons.reply,
              textDirection: TextDirection.ltr,
              color: Colors.greenAccent,
              size: 36.0,
            ),
          )
      ),
    );
  }
}

Route _createNextRoute(Widget classRtnWidget) {
  return PageRouteBuilder(
    pageBuilder: (context, animation,secondaryAnimation) => classRtnWidget,
    transitionsBuilder: (context, animation, secondaryAnimation,child){
      var begin = Offset(1.0,0.0);
      var end = Offset.zero;
      var curve = Curves.ease;
      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve:curve));

      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}

person tonakainohana    schedule 02.01.2020    source источник


Ответы (1)


Вы можете скопировать и вставить полный код ниже
Значение, которое вы указали для RelativeRect.fromLTRB, неверно
изменение на следующее работает нормально

double w1 = width * 5 / 6 - 20;
double h1 = height / 2;
double w2 = 0;
double h2 = 0;

double w3 = 0;
double h3 = 0;
double w4 = width / 6;
double h4 = height / 2;

рабочая демонстрация

введите здесь описание изображения

полный код

import 'package:flutter/material.dart';
import 'dart:math' as math;

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Page0(),
    );
  }
}

class Page0 extends StatefulWidget {
  @override
  Page0State createState() => Page0State();
}

class Page0State extends State<Page0> with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  var _isMoved = false;
  /*double width;
  double height;*/

  @override
  void initState() {
    super.initState();
    _animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 2000),
    );
  }

  @override
  void dispose() {
    _animationController.dispose();
    super.dispose();
  }

  final RelativeRectTween relativeRectTween = RelativeRectTween(
    begin: RelativeRect.fromLTRB(40, 40, 0, 0),
    end: RelativeRect.fromLTRB(0, 0, 40, 40),
  );

  @override
  Widget build(BuildContext context) {
    double width = MediaQuery.of(context).size.width;
    double height = MediaQuery.of(context).size.height;

    double w1 = width * 5 / 6 - 20;
    double h1 = height / 2;
    double w2 = 0;
    double h2 = 0;

    double w3 = 0;
    double h3 = 0;
    double w4 = width / 6;
    double h4 = height / 2;

    print('  ${w1}   ${h1}  ${w2}  ${h2} ');
    print('  ${w3}   ${h3}  ${w4}  ${h4} ');
    return Scaffold(
      body: Stack(children: <Widget>[
        Container(
            height: 800,
            width: 800,
            color: Colors.blue,
            child: GestureDetector(
              onHorizontalDragEnd: null, //set no action
            )),
        Positioned(
          top: height / 2,
          left: width * 5 / 6 - 30,
          child: InkWell(
            onTap: () {
              debugPrint('a');
            },
            child: Container(
              height: 50,
              width: 100,
            ),
          ),
        ),
        PositionedTransition(
          child: IconButton(
            //But it can't work
            icon: Icon(Icons.ac_unit, textDirection: TextDirection.rtl),
            color: Colors.green,
            onPressed: () {
              debugPrint('bb'); //no debug
              _animationController.forward();
            },
          ),
          rect: _animationController
              .drive(
                CurveTween(
                  curve: Curves.bounceIn,
                ),
              )
              .drive(RelativeRectTween(
                begin: RelativeRect.fromLTRB(w1, h1, w2, h2),
                end: RelativeRect.fromLTRB(
                    w3, h3, w4, h4),
              )),
        ),
        Positioned(
          top: 200,
          left: 200,
          child: IconButton(
            icon: Icon(Icons.forward),
            onPressed: () {
              debugPrint('ccc');
            }, //this debug works well.
          ),
        ),
      ]),
      floatingActionButton: FloatingActionButton(
          onPressed: () {
            if (_isMoved) {
              _animationController.repeat();
            } else {
              _animationController.reset();
              _animationController.forward();
            }
            _isMoved = !_isMoved;
          },
          child: Transform.rotate(
            angle: math.pi, // 45 deg
            child: Icon(
              Icons.reply,
              textDirection: TextDirection.ltr,
              color: Colors.greenAccent,
              size: 36.0,
            ),
          )),
    );
  }
}

Route _createNextRoute(Widget classRtnWidget) {
  return PageRouteBuilder(
    pageBuilder: (context, animation, secondaryAnimation) => classRtnWidget,
    transitionsBuilder: (context, animation, secondaryAnimation, child) {
      var begin = Offset(1.0, 0.0);
      var end = Offset.zero;
      var curve = Curves.ease;
      var tween = Tween(begin: begin, end: end).chain(CurveTween(curve: curve));

      return SlideTransition(
        position: animation.drive(tween),
        child: child,
      );
    },
  );
}
person chunhunghan    schedule 03.01.2020
comment
Рад помочь. пожалуйста, отметьте это как ответ, если это поможет, спасибо. - person chunhunghan; 03.01.2020