Skip to content

Instantly share code, notes, and snippets.

@nosmirck
Created March 8, 2020 02:41
Show Gist options
  • Save nosmirck/6e28039c4b9398aa144833b785c14ca1 to your computer and use it in GitHub Desktop.
Save nosmirck/6e28039c4b9398aa144833b785c14ca1 to your computer and use it in GitHub Desktop.
Auto-generated Widgets
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
debugShowCheckedModeBanner: false,
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> {
int _counter = 0;
var _generatedWidgets = <Widget>[];
bool _isGenerating = false;
void _toggleGenerator() {
_isGenerating = !_isGenerating;
_generator();
}
void _generator() async {
while (_isGenerating) {
await Future.delayed(Duration(seconds: 1));
setState(() {
_generatedWidgets.add(Text("${_counter++} I'm a generated Widget!"));
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: ListView(children: <Widget>[
Text(
'There are $_counter widgets generated',
),
..._generatedWidgets
]),
floatingActionButton: FloatingActionButton(
onPressed: _toggleGenerator,
tooltip: 'Generate',
child: Icon(Icons.add),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment