Last active
December 27, 2019 19:53
-
-
Save Archakov06/31e638e4318ef12ae49697d1433ed24c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Это состояние | |
class Home extends StatefulWidget { | |
final List<Word> words; | |
Home({this.words}); | |
@override | |
State<StatefulWidget> createState() { | |
return _Home(); | |
} | |
} | |
class _Home extends State<Home> { | |
Future<Database> database; | |
List<Word> words = List<Word>(); | |
List<Word> reservedWords = List<Word>(); | |
int downloaded = 0; // Сколько загружено в байтах | |
int total = 0; // Сколько всего байтов | |
// ... | |
Future<void> downloadFile() async { | |
final String dbUrl = 'http://212.183.159.230/5MB.zip'; // Скачиваю тестовый файл | |
Dio dio = Dio(); | |
try { | |
var temp = await getTemporaryDirectory(); | |
await dio.download(dbUrl, "${temp.path}/db.zip", | |
onReceiveProgress: (_downloaded, _total) { | |
// Во время скачивания файла, происходит обновление состояния | |
// а именно этих двух переменных, которые позже будут отображаться в диалоге | |
setState(() { | |
this.downloaded = _downloaded; | |
this.total = _total; | |
}); | |
}); | |
} catch (e) { | |
print(e); | |
} | |
print("Download completed"); | |
} | |
@override | |
initState() { | |
super.initState(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return Column( | |
children: <Widget>[ | |
// ... | |
Expanded( | |
child: ListView.builder( | |
itemBuilder: (BuildContext context, int index) => WordCard( | |
// ... | |
onTap: () { | |
this.downloadFile(); | |
showDialog( | |
context: context, | |
builder: (BuildContext context) { | |
return StatefulBuilder(builder: (context, setState) { | |
return Center( | |
child: Dialog( | |
shape: RoundedRectangleBorder( | |
borderRadius: BorderRadius.circular(6), | |
), | |
backgroundColor: Colors.white, | |
child: Container( | |
height: 160, | |
child: Padding( | |
padding: EdgeInsets.all(30), | |
child: Column( | |
crossAxisAlignment: CrossAxisAlignment.start, | |
children: <Widget>[ | |
Text('Загрузка базы слов...', | |
style: TextStyle( | |
fontSize: 18, | |
fontWeight: FontWeight.w800)), | |
SizedBox(height: 30), | |
LinearProgressIndicator( | |
backgroundColor: Colors.blue.shade100, | |
valueColor: AlwaysStoppedAnimation<Color>( | |
Colors.blue.shade400, | |
), | |
value: 0, | |
), | |
SizedBox(height: 15), | |
// Ниже я беру глобальные переменные downloaded и total | |
// и пытаюсь их вывести в этом окне при каждом setState, который | |
// происходит внутри функции downloadFile | |
Row( | |
mainAxisAlignment: | |
MainAxisAlignment.spaceBetween, | |
children: <Widget>[ | |
Text(this.downloaded.toString() + '%', | |
style: TextStyle( | |
fontWeight: FontWeight.w600)), | |
Text(this.total.toString() + ' / 100', | |
style: TextStyle( | |
fontWeight: FontWeight.w600)), | |
], | |
) | |
], | |
), | |
), | |
), | |
), | |
); | |
}); | |
}, | |
); | |
}), | |
itemCount: words.length, | |
), | |
) | |
], | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment