I have started learning flutter and I have problem adjusting the 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.
The list can scroll all the items in android emulator but in IOS simulator, the last item is cut off by navigation bar.
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.
this is my home_page.dart file
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);
}
});
}
}