我在具有Bloc状态管理的TabBarView内有一个PageView。第一个选项卡具有一个PageView,该页面绑定到bloc中的列表,当屏幕打开时,当我滚动到该PageView的另一个页面时,没有任何警报,但是当我切换到另一个选项卡并返回到具有PageView的第一个选项卡时,当我滚动到PageView的另一个Page时,控制台会在此消息下面发出多次警报。
Flutter Doctor一切正常。该应用程序除了显示这些消息外没有崩溃,这些消息是在我滑动页面时发生的,但是仅在切换选项卡并返回到第一个选项卡之后。
════════基础库捕获到异常Exception ══════════════════ 查找停用的小部件的祖先是不安全的。
import 'package:flutter/material.dart';
import '../bloc.dart';
import '../models.dart';
class Bookshelf extends StatefulWidget {
@override
_BookshelfState createState() => _BookshelfState();
}
class _BookshelfState extends State<Bookshelf>
with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
AnimationController _controller;
Animation<double> _animation;
PageController _pageController;
final GlobalKey<ScaffoldState> scaffoldKey2 = new GlobalKey<ScaffoldState>();
@override
bool get wantKeepAlive => true;
@override
void initState() {
super.initState();
_controller = AnimationController(
duration: Duration(milliseconds: 300),
lowerBound: 0.0,
upperBound: 1.0,
vsync: this,
);
_animation = CurvedAnimation(
parent: _controller,
curve: Curves.easeIn,
);
_pageController = PageController(viewportFraction: 0.9);
}
@override
Widget build(BuildContext context) {
BlocProvider.of(context).hasOnboarded.listen((hasOnboarded) {
if (hasOnboarded)
_controller.forward();
else
_controller.reverse();
});
/// scroll position
_pageController.addListener(() {
BlocProvider.of(context).setScrollPosition(
_pageController.offset / _pageController.position.maxScrollExtent,
);
});
return Container(
alignment: Alignment.bottomRight,
child: SizeTransition(
sizeFactor: _animation,
axis: Axis.horizontal,
axisAlignment: -1.0,
child: Container(
alignment: Alignment.bottomCenter,
child: Container(
padding: EdgeInsets.only(top: 48.0, bottom: 28.0),
height: MediaQuery.of(context).size.height * .72,
child: TabBarView(children: [
StreamBuilder(
stream: BlocProvider.of(context).books,
initialData: <Book>[],
builder: (context, AsyncSnapshot<List<Book>> snapshot) {
/// update on scroll
_pageController.addListener(() {
if (snapshot.data.isNotEmpty)
BlocProvider.of(context).setColor(
ColorTransition(
colors:
snapshot.data.map((book) => book.color).toList(),
offset: _pageController.offset,
maxExtent: _pageController.position.maxScrollExtent,
),
);
});
return PageView(
controller: _pageController,
children: snapshot.data
.map((book) => MyBook(book: book))
.toList(),
);
},
),
Container(
child: Center(
child: Text('primeiro'),
),
),
Container(
child: Center(
child: Text('segundo'),
),
),
]),
),
),
),
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_pageController.dispose();
_controller.dispose();
}
}
class MyBook extends StatelessWidget {
final Book book;
MyBook({this.book});
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.symmetric(vertical: 24.0, horizontal: 10.0),
decoration: BoxDecoration(
color: Colors.grey[100],
borderRadius: BorderRadius.circular(12.0),
boxShadow: <BoxShadow>[
BoxShadow(
color: Colors.grey[600],
blurRadius: 12.0,
spreadRadius: -2.0,
offset: Offset(0.0, 5.0),
)
],
),
child: ClipRRect(
borderRadius: BorderRadius.circular(12.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
SizedBox(height: 10.0),
Center(
child: Image(
image: book.hero,
height: 180,
fit: BoxFit.fitHeight,
),
),
SizedBox(height: 12.0),
Container(
padding: EdgeInsets.symmetric(horizontal: 20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(
book.title,
style:
TextStyle(fontSize: 24.0, fontWeight: FontWeight.w500),
),
SizedBox(height: 18.0),
Text(
'Por ${book.author}',
style:
TextStyle(fontSize: 12.0, fontWeight: FontWeight.w500),
),
SizedBox(height: 18.0),
Text(
'O livro apresenta uma dinamica muito importante do direito de empresas.',
style:
TextStyle(fontSize: 14.0, fontWeight: FontWeight.w500),
),
SizedBox(height: 18.0),
Center(
child: RaisedButton(
padding: EdgeInsets.symmetric(
vertical: 14.0, horizontal: 20.0),
onPressed: () => print("read ${book.title}"),
color: book.color,
child: Text(
"LER O LIVRO",
style: TextStyle(color: Colors.white),
),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(99.9),
),
),
),
],
),
)
],
),
),
);
}
}