Your First Flutter App: Polishing the App

Apr 12 2022 Dart 2.14.1, Flutter 2.5, Visual Studio Code

Part 3: Style the App

23. Style Buttons

Lesson Complete

Play Next Lesson
Next
Save for later
About this episode
See forum comments
Cinema mode Mark as Complete Download course materials
Previous episode: 22. Style Text Next episode: 24. Style the Slider

This video was last updated on Apr 12 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

At this point, Bullseye is starting to look great, but we still need to add some styling to the buttons. In the previous episode, we styled Text widgets by creating style classes that we applied to the widgets.

import 'package:flutter/material.dart';
class StyledButton extends StatelessWidget {
  const StyledButton({ Key? key }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      
    );
  }
}
final IconData icon;
final GestureTapCallback onPressed;
const StyledButton({Key? key, required this.icon, required this.onPressed})
      : super(key: key);
    return RawMaterialButton(
      fillColor: Colors.black,
      splashColor: Colors.redAccent)
    return RawMaterialButton(
      fillColor: Colors.black, // old
      splashColor: Colors.redAccent, // old
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Icon(
          icon,
          color: Colors.white,
        ),
      ),
    );
    return RawMaterialButton(
      fillColor: Colors.black,
      splashColor: Colors.redAccent,
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Icon(
          icon,
          color: Colors.white,
        ),
      ), // old code
      onPressed: onPressed,
      shape: const CircleBorder(side: BorderSide(color: Colors.white)),
    );
import 'styled_button.dart';
  @override
  Widget build(BuildContext context) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.center,
      children: <Widget>[ // old code
        StyledButton(
            icon: Icons.refresh,
            onPressed: () {
            onStartOver();
            },
        ),
Padding(
    padding: const EdgeInsets.only(left: 32, right: 32.0),
    child: Column(
    children: <Widget>[
        Text('Round:', style: LabelTextStyle.bodyText1(context)),
        Text('$round', style: ScoreNumberTextStyle.headline4(context)),
    ],
    ),
), // old code
StyledButton(
    icon: Icons.info,
    onPressed: () {},
),
import 'package:flutter/material.dart';
class HitMeButton extends StatelessWidget {
  const HitMeButton({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
final String text;
final GestureTapCallback onPressed;
const HitMeButton({Key? key, required this.text, required this.onPressed})
      : super(key: key);
return RawMaterialButton(
    fillColor: Colors.red[700],
    splashColor: Colors.redAccent,
    child: Padding(
        padding: const EdgeInsets.all(14.0),
        child: Text(
            text,
            maxLines: 1,
            style: const TextStyle(
                color: Colors.white,
                fontWeight: FontWeight.bold,
                letterSpacing: 2.0,
            ),
        ),
    ),
 @override
  Widget build(BuildContext context) {
    return RawMaterialButton(
      fillColor: Colors.red[700],
      splashColor: Colors.redAccent,
      child: Padding(
        padding: const EdgeInsets.all(14.0),
        child: Text(
          text,
          maxLines: 1,
          style: const TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.bold,
            letterSpacing: 2.0,
          ),
        ),
      ), // old code
      onPressed: onPressed,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.circular(12.0),
        side: const BorderSide(
          color: Colors.white,
        ),
      ),
    );
  }
import 'hit_me_button.dart';
HitMeButton(
    text: 'HIT ME',
    onPressed: () {
        _showAlert(context);
    },
),
import 'styled_button.dart';
var okButton = StyledButton(
    icon: Icons.close,
    onPressed: () {
    Navigator.of(context).pop();
    setState(() {
        _model.totalScore += _pointsForCurrentRound();
        _model.target = Random().nextInt(100) + 1;
        _model.round += 1;
    });
    },
);