简体   繁体   English

Flutter AnimatedList ListTile 的最后一项与 CupertinoTabBar 重叠

[英]Flutter AnimatedList last item of ListTile is overlapped by CupertinoTabBar

I have started learning flutter and I have problem adjusting the UI.我已经开始学习颤振,但在调整 UI 时遇到了问题。 I have three BottomNavigationBarItem and in the first tab, I created AnimatedList and populate data from word generator once the plus button is clicked.我有三个BottomNavigationBarItem ,在第一个选项卡中,我创建了AnimatedList并在单击加号按钮后从单词生成器填充数据。 The list can scroll all the items in android emulator but in IOS simulator, the last item is cut off by navigation bar.该列表可以滚动android模拟器中的所有项目,但在IOS模拟器中,最后一个项目被导航栏截断。

I have tried to set margin/padding but I have to set very big number to see the gap between navigation bar and last item.我试图设置边距/填充,但我必须设置非常大的数字才能看到导航栏和最后一项之间的差距。 I also tried to wrap with container and set padding to it but I feel like I'm doing the wrong way.我也尝试用容器包裹并为其设置填充,但我觉得我做错了。 在IOS中,最后一项被导航栏截断

this is my home_page.dart file这是我的 home_page.dart 文件

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

import 'package:english_words/english_words.dart';
import 'package:hello_world/screens/favourite_word_page.dart';
import 'dart:developer';

import 'package:hello_world/screens/popular_page.dart';

class HomePage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _RandomWordsState();
}

enum PAGE_INDEX { HOME, POPULAR, ACCOUNT }

class _RandomWordsState extends State<HomePage> {
  final GlobalKey<AnimatedListState> _listKey = GlobalKey();
  final _suggestion = new List<WordPair>();
  final _savedWords = new Set<WordPair>();
  final _biggerFont = const TextStyle(fontSize: 18.0);
  int _selectedIndex = 0;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Getting Started Testing"),
        actions: <Widget>[
          new IconButton(
            icon: const Icon(Icons.list),
            onPressed: _goToFavourite,
          ),
          new IconButton(
            icon: const Icon(Icons.add),
            onPressed: _addWords,
          ),
        ],
      ),
      body: CupertinoTabScaffold(
        tabBar: CupertinoTabBar(
          items: const <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              title: Text("Home"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.whatshot),
              title: Text("Popular"),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.account_circle),
              title: Text("Account"),
            ),
          ],

          currentIndex: _selectedIndex,
        ),
        tabBuilder: (BuildContext context, int index) {
          switch (index) {
            case 0:
              return CupertinoTabView(
                defaultTitle: "Home",
                builder: (context) => _buildSuggestion(),
              );
              break;
            case 1:
              return CupertinoTabView(
                defaultTitle: "Popular",
                builder: (context) => PopularPage(),
              );
              break;
            default:
          }
        },
      ),
    );
  }

  Widget _buildSuggestion() {
    //_suggestion.addAll(generateWordPairs().take(10));
    print("suggestion list count : " + _suggestion.length.toString());
    return Container(
      margin: const EdgeInsets.only(bottom: 45),
      child: AnimatedList(
        key: _listKey,
        padding: EdgeInsets.all(8.0),
        initialItemCount: _suggestion.length,
        itemBuilder:
            (BuildContext context, int itemIndex, Animation animation) {
          return _buildRow(_suggestion[itemIndex], animation, itemIndex);
        },
      ),
    );
  }

  Widget _buildRow(WordPair suggestion, Animation animation, [int index]) {
    print((index + 1).toString() +
        " suggestion name: " +
        suggestion.asPascalCase);

    final bool isAlreadySaved = _savedWords.contains(suggestion);

    return SlideTransition(
      position: Tween<Offset>(
        begin: new Offset(1, 0),
        end: new Offset(0, 0),
      ).animate(animation),
      child: Card(
        child: ListTile(
          title: new Text(
            suggestion.asPascalCase,
            style: _biggerFont,
          ),
          subtitle: Text((index + 1).toString()),
          trailing: new Icon(
            isAlreadySaved ? Icons.favorite : Icons.favorite_border,
            color: isAlreadySaved ? Colors.red : null,
          ),
          onTap: () => _saveAsFavourite(isAlreadySaved, suggestion),
        ),
      ),
    );
  }

  void _saveAsFavourite(bool isAlreadySaved, WordPair suggestion) {
    setState(() {
      if (isAlreadySaved) {
        _savedWords.remove(suggestion);
      } else {
        _savedWords.add(suggestion);
      }
    });
  }

  void _goToFavourite() {
    Navigator.of(context).push(
      MaterialPageRoute(
        builder: (context) => FavouriteWordsPage(
              favouriteWords: _savedWords,
            ),
      ),
    );
  }

  void _addWords() {
    setState(() {
      _suggestion.addAll(generateWordPairs().take(10));
      print("list count: " + _suggestion.length.toString());

      for (var i = 0; i < 10; i++) {
        _listKey.currentState.insertItem(i);
      }
    });
  }

}

You can use SafeAra to avoid this overlap.您可以使用SafeAra来避免这种重叠。 Wrap your whole list in a SafeArea widget.将整个列表包装在 SafeArea 小部件中。 If you just want to avoid overlapping with the tabbar and nothing else, use如果您只想避免与选项卡栏重叠而别无其他,请使用

SafeArea(
      bottom: true, 
      top: false, 
      left: false, 
      right: false, 
      child: child ) //your list

I had the same issue, as above answer, you can wrap your screen with SafeArea, or I just found that if you set background color to your CupertinoTabBar, screen will not overlap with tab.我有同样的问题,如上面的答案,你可以用 SafeArea 包裹你的屏幕,或者我刚刚发现如果你为 CupertinoTabBar 设置背景颜色,屏幕将不会与选项卡重叠。

CupertinoTabBar(
          backgroundColor: Colors.red, // add this 

          items: <BottomNavigationBarItem>[
            BottomNavigationBarItem(
              icon: Icon(Icons.home),
              // title: Text('Home'),
            ),
            BottomNavigationBarItem(
              icon: Icon(Icons.person),
            ),
          ],
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM