SHARE
TWEET

Untitled

a guest Jun 24th, 2019 45 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Models/team.dart
  2.  
  3. import 'package:flutter/foundation.dart';
  4.  
  5. class Team {
  6.   Team({ @required this.name, this.score = 0 });
  7.  
  8.   String name;
  9.   int score;
  10. }
  11.  
  12. // screens/game_setup.dart
  13.  
  14. import 'dart:async';
  15. import 'package:flutter/material.dart';
  16. import 'package:game_score/models/team.dart';
  17.  
  18. class _TeamTileState extends State<_TeamTile> {
  19.   bool _editing = false;
  20.   TextEditingController _textFieldController;
  21.   FocusNode _focusNode;
  22.  
  23.   @override
  24.   void initState() {
  25.     super.initState();
  26.     _textFieldController = TextEditingController.fromValue(TextEditingValue(text: widget.team.name));
  27.     _focusNode = FocusNode();
  28.     _focusNode.addListener(() {
  29.       if (!_focusNode.hasFocus) {
  30.         _toggleEditing();
  31.         widget.setTeamName(_textFieldController.text);
  32.       }
  33.     });
  34.   }
  35.  
  36.   void _focusOnNode() {
  37.     Timer(const Duration(milliseconds: 100),
  38.             () => FocusScope.of(context).requestFocus(_focusNode));
  39.   }
  40.  
  41.   Widget _buildShowTile() {
  42.     return GestureDetector(
  43.         onTap: () {
  44.           _toggleEditing();
  45.           _focusOnNode();
  46.         },
  47.         child: Text(widget.team.name)
  48.     );
  49.   }
  50.  
  51.   Widget _buildEditTile() {
  52.     return TextField(controller: _textFieldController, focusNode: _focusNode);
  53.   }
  54.  
  55.   void _toggleEditing() {
  56.     setState(() {
  57.       _editing = !_editing;
  58.     });
  59.   }
  60.  
  61.   @override
  62.   Widget build(BuildContext context) {
  63.     return Column(
  64.         children: <Widget>[
  65.           ListTile(
  66.               title: _editing ? _buildEditTile() : _buildShowTile()
  67.           ),
  68.           Divider()
  69.         ]
  70.     );
  71.   }
  72. }
  73.  
  74. class _TeamTile extends StatefulWidget {
  75.   _TeamTile({ @required this.team, @required Function setTeamName }) : this._setTeamName = setTeamName;
  76.   final Function _setTeamName;
  77.   final Team team;
  78.  
  79.  
  80.   @override
  81.   State<StatefulWidget> createState() {
  82.     return _TeamTileState();
  83.   }
  84.  
  85.   void setTeamName(String name) {
  86.     _setTeamName(team, name);
  87.   }
  88. }
  89.  
  90.  
  91. class GameSetup extends StatelessWidget {
  92.   GameSetup({ this.teams, this.setTeamName });
  93.  
  94.   List<Team> teams;
  95.   Function setTeamName;
  96.  
  97.   @override
  98.   Widget build(BuildContext context) {
  99.     return Scaffold(
  100.         appBar: AppBar(title: Text("Game Score App")),
  101.         body: ListView.builder(itemCount: 3, itemBuilder: (builderContext, i) {
  102.           if (i < 2) {
  103.             return _TeamTile(team: teams[i], setTeamName: setTeamName);
  104.           } else {
  105.             return ListTile(
  106.                 onTap: () {
  107.                   print("Starts a new game!");
  108.                 },
  109.                 title: Center(
  110.                     child: Text("Start new game",
  111.                         style: TextStyle(color: Colors.green)
  112.                     )
  113.                 )
  114.             );
  115.           }
  116.         })
  117.     );
  118.   }
  119. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top