Skip to content

Instantly share code, notes, and snippets.

@LuongPV
Last active July 20, 2021 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuongPV/66cbbaa98ef5c86b52984031bb2ff72e to your computer and use it in GitHub Desktop.
Save LuongPV/66cbbaa98ef5c86b52984031bb2ff72e to your computer and use it in GitHub Desktop.
Flutter issues share
import 'dart:async';
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key? key, this.title}) : super(key: key);
final String? title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<Widget> _widgets = [MyStatefulWidget()];
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: ListView.separated(
itemCount: _widgets.length,
itemBuilder: (_, index) {
return _widgets[index];
},
separatorBuilder: (BuildContext context, int index) {
return SizedBox(
width: 5,
);
},
),
),
floatingActionButton: TextButton(
onPressed: () {
setState(() {
// Insert first in the list, hope it's just need to rebuild only the first item, the remains are unchanged
_widgets.insert(0, MyStatefulWidget());
});
},
child: Container(
child: Text(
'Press',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
padding: EdgeInsets.all(10),
decoration: BoxDecoration(
color: Colors.red,
),
),
),
);
}
}
class MyStatefulWidget extends StatefulWidget {
MyStatefulWidget({Key? key}) : super(key: key);
@override
State<StatefulWidget> createState() {
print('>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> createState');
return _MyStatefulState();
}
}
class _MyStatefulState extends State<MyStatefulWidget> {
StreamSubscription? countdownSubscription;
int countdownTime = 30;
@override
void initState() {
super.initState();
initTimeoutUI();
}
void initTimeoutUI() {
countdownSubscription = Future.delayed(Duration(seconds: 1)).asStream().listen((value) {
if (countdownTime > 0) {
setState(() {
countdownTime--;
});
}
initTimeoutUI();
});
}
@override
void dispose() {
super.dispose();
countdownSubscription?.cancel();
}
@override
Widget build(BuildContext context) {
return Container(
width: 100,
height: 100,
child: Center(child: Text('countdown: $countdownTime')),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment