当
调用
s
cr
ollController.isAttached时,它在第一帧之前是始终false的。解决方法是在第一帧之后检查isAttached属性,代码示例如下:
import 'package:flutter/material.dart';
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
class _MyHomePageState extends State<MyHomePage> {
late final ScrollController _scrollController;
bool _isControllerAttached = false;
@override
void initState() {
super.initState();
_scrollController = ScrollController();
WidgetsBinding.instance?.addPostFrameCallback((timeStamp) {
setState(() {
_isControllerAttached = _scrollController.hasClients;
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView.builder(
controller: _scrollController,
itemBuilder: (context, index) => ListTile(
title: Text('Item $index'),
itemCount: 100,
bottomNavigationBar: _isControllerAttached
? const BottomNavigationBar(items: <BottomNavigationBarItem>[])
: null,