From a465387c6f460e48559415d5a59930e2c724b4b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Harrault?= <benoit@harrault.fr> Date: Fri, 9 Apr 2021 09:59:56 +0200 Subject: [PATCH] Add pick random letter feature --- lib/main.dart | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/main.dart b/lib/main.dart index cb9ad15..9d5e0a8 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'dart:math'; void main() { runApp(MyApp()); @@ -21,12 +22,21 @@ class MyHomePage extends StatefulWidget { MyHomePage({Key key, this.title}) : super(key: key); final String title; + final Random _rnd = Random(); + final String _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State<MyHomePage> { + String _randomLetter = ''; + + void _pickRandomLetter() { + setState(() { + _randomLetter = widget._chars[widget._rnd.nextInt(widget._chars.length)]; + }); + } @override Widget build(BuildContext context) { @@ -39,11 +49,19 @@ class _MyHomePageState extends State<MyHomePage> { mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[ Text( - 'X', + 'Lettre aléatoire :', + ), + Text( + '$_randomLetter', + style: Theme.of(context).textTheme.headline4, ), ], ), ), + floatingActionButton: FloatingActionButton( + onPressed: _pickRandomLetter, + child: Icon(Icons.shuffle), + ), ); } } -- GitLab