Create by https://gist.github.com/CaiJingLong/a299d15b92edc2d09a8a06cf6dd5a1b0
Created
July 22, 2020 05:25
-
-
Save CaiJingLong/62ba16767748970bdfbe6d254b9ddc00 to your computer and use it in GitHub Desktop.
The calc for binary
Created with <3 with dartpad.dartlang.org.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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