在 SOF 上找到的以下应用程序中,滚动列表导致 BottomAppBar 隐藏 FloatingActionButtonCircularNotchedRectangle 更改大小。

flutter - 隐藏具有相同动画的 bottomAppBar 和 FloatingActionButton-LMLPHP

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: WorkoutDetailsPage(Workout()),
    );
  }
}

class Exercise {
  String name;

  Exercise({@required name}) {
    this.name = name;
  }
}

class Workout {
  String name = "my name";
}

class WorkoutDetailsPage extends StatefulWidget {
  Workout _workout = Workout();

  WorkoutDetailsPage(this._workout);

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

class _WorkoutDetailsPageState extends State<WorkoutDetailsPage> {
  final List<Exercise> exercises = [
    Exercise(name: "Push Ups"),
    Exercise(name: "Bench press"),
    Exercise(name: "Pull ups"),
    Exercise(name: "Press ups"),
    Exercise(name: "Crunches"),
    Exercise(name: "Sit ups"),
    Exercise(name: "BIceps curl"),
    Exercise(name: "Something else"),
    Exercise(name: "Push Ups"),
    Exercise(name: "Bench press"),
    Exercise(name: "Pull ups"),
    Exercise(name: "Press ups"),
    Exercise(name: "Crunches"),
    Exercise(name: "Sit ups"),
    Exercise(name: "BIceps curl"),
    Exercise(name: "Something else"),
    Exercise(name: "Push Ups"),
    Exercise(name: "Bench press"),
    Exercise(name: "Pull ups"),
  ];

  ScrollController _hideButtonController;

  bool _isVisible = true;

  @override
  void initState() {
    super.initState();
    _isVisible = true;
    _hideButtonController = new ScrollController();
    _hideButtonController.addListener(() {
      print("listener");
      if (_hideButtonController.position.userScrollDirection == ScrollDirection.reverse) {
        setState(() {
          _isVisible = false;
          print("**** $_isVisible up");
        });
      }
      if (_hideButtonController.position.userScrollDirection == ScrollDirection.forward) {
        setState(() {
          _isVisible = true;
          print("**** $_isVisible down");
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButtonAnimator: FloatingActionButtonAnimator.scaling,
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButton: _isVisible
          ? FloatingActionButton(
              backgroundColor: Colors.blue,
              elevation: 12,
              onPressed: () {},
            )
          : null,
      bottomNavigationBar: AnimatedContainer(
        duration: Duration(milliseconds: 200),
        height: _isVisible ? 60 : 0.0,
        child: BottomAppBar(
          elevation: 8,
          shape: CircularNotchedRectangle(),
          color: Colors.blue,
          child: Container(
            height: 60,
            child: Row(
              children: <Widget>[Text("data")],
            ),
          ),
        ),
      ),
      body: CustomScrollView(
        controller: _hideButtonController,
        slivers: <Widget>[
          SliverAppBar(
            expandedHeight: kToolbarHeight,
            pinned: true,
            floating: true,
            snap: true,
            flexibleSpace: FlexibleSpaceBar(
              title: Text(widget._workout.name),
            ),
          ),
          SliverList(
            delegate: SliverChildBuilderDelegate(
                buildSliverListItem, childCount: exercises.length
                ),
          ),
        ],
      ),
    );
  }

  Widget buildSliverListItem(BuildContext context, int index) {
    return Center(
      child: ListTile(
        title: Text(exercises[index].name),
      ),
    );
  }
}

我该如何解决?我测试了将 BottomAppBar 放入 Stack 中,但没有用。

最佳答案

输出:

flutter - 隐藏具有相同动画的 bottomAppBar 和 FloatingActionButton-LMLPHP

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  bool _visible = true;
  Duration _duration = Duration(milliseconds: millis);

  @override
  Widget build(BuildContext context) {
    screenHeight = MediaQuery.of(context).size.height;
    return Scaffold(
      appBar: AppBar(),
      floatingActionButton: FloatingActionButton(onPressed: _animate),
      floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
      floatingActionButtonAnimator: MyAnimator(),
      bottomNavigationBar: AnimatedContainer(
        duration: _duration,
        height: _visible ? height : 0,
        child: BottomAppBar(color: Colors.red, shape: CircularNotchedRectangle()),
      ),
    );
  }

  void _animate() async {
    _visible = !_visible;
    setState(() {});
  }
}

double value = 0, screenHeight, height = 100;
int millis = 5000;

class MyAnimator extends FloatingActionButtonAnimator {
  @override
  Offset getOffset({Offset begin, Offset end, double progress}) {
    Offset offset = Offset(begin.dx, begin.dy);

    if (begin.dy >= (screenHeight - kToolbarHeight).round()) {
      value++;
      double max = (millis + 80) / 60;
      var other = (56 * value * height / 100) / max;
      offset = Offset(begin.dx, begin.dy + other);
    }

    return offset;
  }

  @override
  Animation<double> getRotationAnimation({Animation<double> parent}) => parent;

  @override
  Animation<double> getScaleAnimation({Animation<double> parent}) => parent;
}

关于flutter - 隐藏具有相同动画的 bottomAppBar 和 FloatingActionButton,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56765478/

10-11 19:51