Я разработал процедуру выбора и поиска предметов. Все работает нормально, но SnakBar не отображается. Понятия не имею почему. Кто-нибудь может мне помочь?
Это код с SnackBar:
floatingActionButton: FloatingActionButton(
onPressed: () async {
if (response["result"] == "ok") {
final snackBar = SnackBar(
content: Text("Músicas adicionadas com sucesso"),
duration: Duration(seconds: 2),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Some code to undo the change.
},
),
);
},
tooltip: 'Salvar Músicas',
child: Icon(
Icons.check,
),
backgroundColor: Colors.orange,
),
Это полный код:
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:mysongs_v2/global.dart';
import 'package:mysongs_v2/models/playlistsong.dart';
import 'package:mysongs_v2/models/song.dart';
class SelectSongScreen extends StatefulWidget {
final int playlistId;
final String playlistName;
//constructor
SelectSongScreen({this.playlistId, this.playlistName}) : super();
@override
State<StatefulWidget> createState() {
return _SelectSongScreenState();
}
}
class _SelectSongScreenState extends State<SelectSongScreen> {
TextEditingController searchController = new TextEditingController();
int userId = 1;
var selectedSong = new Song();
var songsSelected = new List<String>();
var songsSelectedId = new List<String>();
bool _isSelected = true;
bool processing = false;
bool isLoaded = false;
onSearchTextChanged(String text) async {
isLoaded = false;
if (text.isEmpty) {
setState(() {});
return;
}
setState(() {});
}
Container _searchBar() {
var _result = new Container(
color: COLOR_MAIN,
child: new Padding(
padding:
const EdgeInsets.only(top: 1.0, bottom: 1.0, left: 2.0, right: 2.0),
child: new Card(
child: new ListTile(
leading: new Icon(Icons.search),
title: new TextField(
controller: searchController,
decoration: new InputDecoration(
hintText: 'Search', border: InputBorder.none),
onChanged: onSearchTextChanged,
),
trailing: new IconButton(
icon: new Icon(Icons.cancel),
onPressed: () {
searchController.clear();
onSearchTextChanged('');
},
),
),
),
),
);
return _result;
}
List<Container> _buildListItemsFromSongs(List<Song> songs) {
int index = 0;
return songs.map((song) {
_isSelected = false;
for (int i = 0; i <= this.songsSelected.length - 1; i++) {
if (songsSelected[i] == song.name) {
_isSelected = true;
}
}
var _container = Container(
child: Column(
children: <Widget>[
ListTile(
selected: true,
enabled: true,
leading: _isSelected
? Icon(
Icons.check,
size: 40,
color:
(_isSelected ? Colors.green[700] : Colors.green[500]),
)
: Icon(
Icons.add_circle,
size: 40,
color:
(_isSelected ? Colors.green[700] : Colors.green[500]),
),
title: Text(song.name,
style: TextStyle(
color: Colors.black, fontWeight: FontWeight.bold)),
subtitle:
Text(song.singer, style: TextStyle(color: Colors.black)),
onTap: () {
isLoaded = true;
print("You tapped to TILE ${song.name}");
setState(() {
selectedSong = song;
var _pos = this.songsSelected.indexOf(song.name);
if (_pos == -1) {
this.songsSelected.add(song.name);
} else {
this.songsSelected.removeAt(_pos);
}
//processing = true;
});
},
),
new Divider(
height: 20.0,
)
],
),
);
index = index + 1;
return _container;
}).toList();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
leading: IconButton(
icon: Icon(
Icons.arrow_back,
),
onPressed: () {
Navigator.of(context).pop();
Navigator.of(context).pop();
},
),
backgroundColor: COLOR_MAIN,
title: Column(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text("Adicionar Músicas ao playist"),
Text(
widget.playlistName,
style: TextStyle(fontSize: 16.0),
)
],
),
),
body: new FutureBuilder(
future: searchSongsCheckPlaylist(http.Client(), widget.playlistId,
searchController.text, isLoaded),
builder: (BuildContext context, AsyncSnapshot<List> snapshot) {
if (!snapshot.hasData) return new Container();
List<Song> songs = new List();
songs = snapshot.data;
if (songs.length == 0 || songs[0].name.isEmpty) {
return new Column(
children: <Widget>[
_searchBar(),
new Expanded(
child: new ListView(
children: <Widget>[
Container(
//color: Colors.grey[500],
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: new Container(
child: new Icon(
Icons.search,
color: Colors.grey[500],
size: 260.0,
//semanticLabel: 'Text to announce in accessibility modes',
),
),
),
),
Container(
child: new Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
children: <Widget>[
new Text(
'Você pode pesquisar por nome da MÚSICA',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.grey[500]),
),
new Text(
'ou por nome do ARTISTA',
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 20.0,
color: Colors.grey[500]),
),
],
),
),
),
],
),
),
],
);
} else {
return new Column(
children: <Widget>[
_searchBar(),
//Container(child: Icon(Icons.add),),
new Expanded(
child: ListView(
children: _buildListItemsFromSongs(songs),
),
),
],
);
}
}),
floatingActionButton: FloatingActionButton(
onPressed: () async {
if (response["result"] == "ok") {
final snackBar = SnackBar(
content: Text("Músicas adicionadas com sucesso"),
duration: Duration(seconds: 2),
action: SnackBarAction(
label: 'Undo',
onPressed: () {
// Some code to undo the change.
},
),
);
},
tooltip: 'Salvar Músicas',
child: Icon(
Icons.check,
),
backgroundColor: Colors.orange,
),
);
}
}