Skip to content

Instantly share code, notes, and snippets.

@CaiJingLong
Created July 22, 2020 05:25
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 CaiJingLong/62ba16767748970bdfbe6d254b9ddc00 to your computer and use it in GitHub Desktop.
Save CaiJingLong/62ba16767748970bdfbe6d254b9ddc00 to your computer and use it in GitHub Desktop.
The calc for binary
// 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: BitMoveExample(),
);
}
}
class BitMoveExample extends StatefulWidget {
@override
BitMoveExampleState createState() => BitMoveExampleState();
}
class BitMoveExampleState extends State<BitMoveExample> {
int number = 1;
String radix() {
return number.toRadixString(2).padLeft(32, '0');
}
@override
Widget build(BuildContext context) {
final str = radix();
return Scaffold(
appBar: AppBar(),
body: Column(
children: <Widget>[
Row(
children: <Widget>[
for (var i = 0; i < 16; i++) _buildCheck(i, str),
],
),
Row(
children: <Widget>[
for (var i = 16; i < 32; i++) _buildCheck(i, str),
],
),
Row(
children: [
FlatButton(
child: Text('<<'),
onPressed: () {
setState(() {
number = number << 1;
});
},
),
FlatButton(
child: Text('>>'),
onPressed: () {
setState(() {
number = number >> 1;
});
},
),
],
),
Text(number.toString()),
],
),
);
}
_buildCheck(int i, String str) {
final s = str[i];
return Expanded(
child: InkWell(
onTap: () {
final tar = s == '0' ? '1' : '0';
String bitNumber = '';
if (i == 0) {
bitNumber = '$tar${str.substring(1)}';
} else if (i == str.length - 1) {
bitNumber = '${str.substring(0, str.length - 1)}$tar';
} else {
bitNumber = '${str.substring(0, i)}$tar${str.substring(i + 1)}';
}
setState(() {
number = int.parse(bitNumber, radix: 2);
});
},
child: Text(s),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment